Communicating with an Arduino Over Serial

From Learning

Jump to: navigation, search


What is Serial Communication?

Serial communication just means that only one bit of information is sent a time (as opposed to parallel). Lots of devices communicate serially, but usually the protocol RS-232 is what is implied when talking about serial ports and communication. Most computers don't have serial ports, but use a USB-to-serial driver to send and receive serial communication over USB. This is how the Arduino communicates with a computer and other devices.

The Arduino serial port or UART (universal asynchronous receiver transmitter) is on digital pins 0 (RX) and 1 (TX) which receive and send data respectively. They have corresponding LEDs labeled RX and TX that blink when data passes - this is noticeable when a new sketch is loaded onto the board. The Arduino Mega has 3 additional serial ports. When using the serial port, the corresponding pins can't be used for other purposes.

When serial data is sent out, it's not sent to a particular device. So whatever is listening to a serial port connected to the Arduino serial port will receive the messages. The Arduino will also receive whatever is sent to its serial port and won't know what device sent that message unless that's included in the message.

How to Send and Receive Serial Data with the Arduino Programming Environment

The Arduino programming environment has a built in serial reader and writer that makes it easier to debug and develop your Arduino code. An Arduino board needs to be plugged into your computer in order for the serial monitor to function. Open the serial monitor by clicking on the button in the upper right of your Arduino window.

Arduino serial.png

The serial monitor will print out what it receives on the serial port. You can also send serial messages via the serial port by the using the text box in the monitor. Be sure that the number in the drop-down menu of the serial monitor matches what you put in Serial.begin(). It's important to note that each time you open the serial monitor, you restart the sketch on the board.

Serial monitor.png

How to Send Serial Data from the Arduino Board

To send data over the serial port on the Arduino, use the Serial object to start the serial communication at a particular baud (rate of data). 9600 is usually the baud used unless otherwise indicated.

Once the Serial object has started, then the println and print methods are used to print information to the serial port. println includes a line break at the end of what you print while print does not.

void setup() {

How to Receive Serial Data on the Arduino Board

To receive serial data, you need to start the serial connection as you would to send serial data. Then inside loop(), check if any serial data has arrived with Serial.available(). If data has arrived and is available, store that data in a variable. You'll notice that the ASCII value for each character you type is printed out.

int incomingByte = 0;   // for incoming serial data
void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
void loop() {
        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte =;
                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);

See Also

Wikipedia on Serial Port

Wikipedia on RS-232

Wikipedia on UART

Arduino Reference for Serial

Wikipedia on ASCII

  • This page was last modified on 18 March 2012, at 05:16.
  • This page has been accessed 24,876 times.