Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.

Tutorial 9: Using the Arduino Serial Port

Created on: 2 August 2012

The Arduino Uno can send data (such as a text message) to the PC over the USB cable. The Arduino IDE has a serial monitor window that can be opened and will receive and display the data sent from the Arduino board. Data can also be sent to the Arduino board from the serial monitor.

This serial communication is very useful for controlling electronics that is connected to (interfaced to) the Arduino board from the PC. It can also be used to debug (find errors in) Arduino programs when writing new programs.

The following videos show what you will achieve in this tutorial.

Transmit a message from the Arduino to the PC:

Can't see the video? View on YouTube →

Receive characters from the PC and transmit a message back to the PC:

Can't see the video? View on YouTube →

Prerequisites

Complete tutorial 3 - Starting with Arduino before attempting this tutorial.

Components

All that is needed is an Arduino Uno board, standard USB cable and PC with the Arduino IDE software installed. You will already have these if you have completed tutorial 3.

Books that may interest you:

C Programming with Arduino Book Ultimate Arduino MEGA 2560 Hardware Manual Ultimage Arduino Uno Hardware Manual

Sending Data to PC from the Arduino

Copy the serial_tx_msg Arduino sketch below and paste it into the Arduino IDE.

/*--------------------------------------------------------------
  Program:     serial_tx_msg
               (serial transmit message)

  Description:  Sends a text message out of the serial (USB)
                port of the Arduino every second.
                
                Use the Arduino Serial Monitor to receive the
                message.
               
  Date:         3 March 2012
 
  Author:       W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Hello, world!");
  delay(1000);
}

Compile the program by clicking the "Verify" button in the Arduino IDE. Upload the program to the Arduino board by clicking the "Upload" button.

Now start the serial monitor by clicking the "Serial Monitor" button in the Arduino IDE. The figure below shows the location of the serial monitor in Arduino IDE version 1.0 (top) and Arduino IDE version 0022 (bottom).

The serial monitor window should display a new "Hello, world!" message every second. Note that the TX LED on the Arduino board lights up.

The Arduino IDE serial monitor button
Arduino IDE Serial Monitor Button

Sending Data from the Arduino to the PC

Copy the serial_rx_msg sketch below and paste it into the Arduino IDE.

/*--------------------------------------------------------------
  Program:     serial_rx_msg
               (serial receive message)

  Description:  Receives a text character from the serial
                (USB) port of the Arduino. Transmits a
                message back to the PC with the received
                character.
                
                Use the Arduino Serial Monitor to transmit and
                receive the message.
               
  Date:         3 March 2012
 
  Author:       W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/

char rx_byte;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    rx_byte = Serial.read();
    Serial.print("You typed: ");
    Serial.println(rx_byte);
  }
}

This program receives data from the PC and then transmits it back to the PC with an additional message. It demonstrates receiving and transmitting data on the Arduino board.

In the Arduino IDE, verify and then upload the serial_rx_msg program to the Arduino board. Start the serial monitor program and enter a text character or sentence in the top field (to the left of the Send button). Click the Send button (or press Enter) to send the character or sentence.

For each character received, the Arduino board will send back "You typed: " and then the character that you typed.

Note that the TX and RX LEDs switch on for a brief moment when clicking the send button. This shows that data was sent and received.

Now that you know how the serial monitor works, we can use it in future Arduino projects.

As an Amazon Associate I earn from qualifying purchases: