Created on: 2 August 2012
Updated on: 16 January 2023
How to use the Arduino Uno USB/serial port for beginners in electronics. The Arduino Uno can send data (such as a text message) to a PC or computer over a 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 firstly how to send data from an Arduino to a computer, and then how to receive data from a computer using the USB/serial port.
How to send data or a message from an Arduino Uno to a computer or PC via the USB/serial port. In the video below, a sketch is first loaded to an Arduino Uno that causes it to continually send a message over its serial USB port. The video then shows the Serial Monitor window opened in the Arduino IDE and the message from the Arduino board being received in the bottom serial monitor window of the IDE.
How to receive a message from a computer or PC on an Arduino Uno on the USB/serial port. The video below first shows that a sketch is loaded to an Arduino Uno board. Afterwards, the Arduino IDE Serial Monitor window is opened. When a keyboard character is sent to the Arduino from the Serial Monitor window, the sketch echoes, or sends back, the typed character as part of a message. This causes the message and character to be displayed in the bottom part of the Serial Monitor window.
Complete tutorial 3 - Starting with Arduino before attempting this tutorial.
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 of this beginner's electronics course.
Copy the serial_tx_msg Arduino sketch below and paste it into a new window of the Arduino IDE. The sketch is used to send data to a PC from an Arduino Uno.
/*-------------------------------------------------------------- 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 toolbar button at the top right of the Arduino IDE.
The serial monitor window should display a new "Hello, world!" message every second. Note that the TX LED on the Arduino board lights up when the text message is transmitted by the Arduino.
Copy the serial_rx_msg sketch below and paste it into the Arduino IDE. The sketch is used to send data from a PC to an Arduino Uno board.
/*-------------------------------------------------------------- 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. Open the Serial Monitor window and enter a text character or sentence in the top field of the Serial Monitor window. Press the Enter keyboard key 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 on the Arduino board 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.