Sub topic 1
How an Arduino Talks to Your Computer
Prerequisites
This chapter assumes that you can create and upload an Arduino sketch, and that you know the roles of setup() and loop(). You should also understand variables and basic C/C++ statements.
An Arduino and a computer exchange data as a sequence of bytes. A byte is a number from 0 to 255 that can represent part of a character, a number, or a message. This exchange is called serial communication because the bits are sent one after another over a communication channel.
On a classic Arduino Uno, the sketch uses a UART (Universal Asynchronous Receiver/Transmitter). A UART converts bytes into timed electrical signals and converts incoming signals back into bytes. The Uno's USB interface contains a separate USB-to-serial chip. The computer communicates over USB, while the sketch communicates with the UART.
The USB cable therefore provides two connected links:
The connection usually carries two independent streams:
- The transmit stream sends bytes from the Arduino to the computer.
- The receive stream sends bytes from the computer to the Arduino.
The streams are asynchronous. Both sides agree to use the same timing rules instead of sharing a clock signal. The most important timing rule is the baud rate, which is the approximate number of signal changes sent per second. A common setting is 9600 baud.
Choosing a communication path
Different Arduino boards implement the computer connection differently. An Uno commonly uses its hardware serial pins and a USB-to-serial interface. Some newer boards have native USB hardware, while others provide more than one serial port.
| Board or connection | Typical sketch interface | Important detail |
|---|---|---|
| Arduino Uno USB port | `Serial` | USB usually shares the UART on pins 0 and 1 |
| External UART module | `Serial` or a software serial port | Connect transmit to receive and share ground |
| Board with native USB | `Serial` | USB and UART pins may be separate |
When using an Uno, pins 0 and 1 are also used during uploading and by the USB serial interface. Avoid connecting another device to those pins while uploading unless you understand how the signals interact.
Starting the link
Call Serial.begin() in setup() to configure the serial interface. The argument selects the baud rate. The computer application must use the same rate.
void setup() {
Serial.begin(9600);
Serial.println("Arduino is ready");
}
void loop() {
Serial.println("Working");
delay(1000);
}After uploading, open the Arduino IDE Serial Monitor and select 9600 baud. You should see Arduino is ready, followed by Working once each second.
The monitor may reset the board when it opens the port. That is why the startup message can appear when the window opens rather than immediately after the upload.
A mismatch has a recognizable result. If the sketch uses 9600 but the monitor uses another rate, the received bytes are sampled at the wrong times. The monitor usually displays garbled symbols. The data may still be physically present, but the receiver cannot decode it correctly.
Serial communication is useful because it makes invisible program behavior visible. A sketch can report which branch it entered, show a sensor value, or confirm that a command arrived. The same channel can carry input in the other direction, allowing the computer to control the sketch.
Exercise
An Uno prints unreadable characters in the Serial Monitor. The sketch calls Serial.begin(9600). What should you check first?
Show answer
Check that the Serial Monitor is connected to the correct board port and is set to 9600 baud. Also check for another device interfering with pins 0 and 1.