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.

See related product

A000066 | Arduino Uno Rev3

Arduino Corporation Embedded System Development Boards and Kits View

 

Related news articles

Latest News

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here