Understanding Arduino Serial Communication

Serial communication and debugging are essential to working with Arduino boards. Simply put, serial communication is a method that the board uses to communicate with other devices – such as another computer, a peripheral, etc. When we discuss Arduino serial communication, it’s also necessary to distinguish between its physical components (the Arduino’s serial ports, for example) and its software components (using the serial monitor in the Arduino IDE).

Arduino Serial Port

Every Arduino board has at least one serial port, and many have additional built-in serial ports (Serial1, Serial2, etc.). Other boards, such as the Leonardo, also have serial communication in the form of a USB) port. In USB-enabled boards, we refer to the USB as Serial. The other standard serial ports begin with “Serial1” and number progressively from there.

The port on your board called Serial (Serial1 if you have USB) communicates via pins 0 and 1 (RX and TX, respectively). Keep in mind that if you use these pins for serial data transmission, they will be unavailable for digital I/O. To use the extra serial pins (pins 18 and 19 on the Arduino Due, for instance), to communicate with your PC, make sure you have a USB-to-serial adapter, as the extra pins are not connected to the board’s built-in adapter.

The Arduino website has a detailed breakdown of the various serial ports and required adapters and connections by model on its website.

Arduino Serial Example Code

If you still need more serial ports than the built-in ones provided by your board, you have another option. The Arduino Software Serial library will allow you to use other digital pins, supplemented by software that replaces the RX and TX functions, to expand your board’s serial connectivity.

To better understand the idea, we can look at Tom Igoe’s public domain example code here:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); 
// RX, TX

voidsetup() {
  
// Open serial communications and wait for port to open:
  
Serial.begin(57600);
  
while (!Serial) {
    ; 
// wait for serial port to connect. Needed for native USB port only
  }


  
Serial.println("Goodnight moon!");

  
// set the data rate for the SoftwareSerial port
  mySerial.
begin(4800);
  mySerial.
println("Hello, world?");
}

voidloop() { // run over and over
  
if (mySerial.available()) {
    
Serial.write(mySerial.read());
  }
  
if (Serial.available()) {
    mySerial.
write(Serial.read());
  }
}

This code creates a virtual serial port using pins 10 and 11 as the RX and TX ports, respectively.

Arduino Serial Monitor Communication

Once you connect your device to a PC running the Arduino IDE software, you can use the serial monitor to communicate with the board. Just select “Serial Monitor” from the Tools menu (or click the little magnifying glass) to open the serial monitor. Use the same baud rate from the call to begin serial communication in your code.

The serial monitor displays any information that the Arduino outputs to the serial port, so it’s essential for debugging purposes. When your sketch or program isn’t executing as expected, you can use serial outputs in your code. Those output messages on the serial monitor allow you to assess the state of the program to figure out where the error is.

Conclusion

Understanding the Arduino serial ports and Arduino software serial is important for a number of basic functions like connecting your board to a personal computer, connecting to other peripheral devices, and successfully debugging problematic or faulty code. For more information on serial functions, software serial, and more, consult the Arduino database.

参阅相关产品

A000066 | Arduino Uno Rev3

Arduino Corporation Embedded System Development Boards and Kits 查看

 

最新消息

Sorry, your filter selection returned no results.

请仔细阅读我们近期更改的隐私政策。当按下确认键时,您已了解并同意艾睿电子的隐私政策和用户协议。

本网站需使用cookies以改善用户您的体验并进一步改进我们的网站。此处阅读了解关于网站cookies的使用以及如何禁用cookies。网页cookies和追踪功能或許用于市场分析。当您按下同意按钮,您已经了解并同意在您的设备上接受cookies,并给予网站追踪权限。更多关于如何取消网站cookies及追踪的信息,请点击下方“阅读更多”。尽管同意启用cookies追踪与否取决用户意愿,取消网页cookies及追踪可能导致网站运作或显示异常,亦或导致相关推荐广告减少。

我们尊重您的隐私。请在此阅读我们的隐私政策。