Sub topic 6
The USB-to-Serial Bridge and the Serial Monitor
Assume you know the Arduino IDE and the setup() and loop() functions, and that you can distinguish voltage from current. This chapter explains how an Arduino Uno communicates with a computer through USB.
What the USB-to-serial bridge does
The Uno's main microcontroller is the ATmega328P. It runs your sketch and controls the input and output pins. It does not communicate with a computer through USB directly.
The Uno R3 includes a second microcontroller, commonly an ATmega16U2, that acts as a USB-to-serial bridge. A bridge translates between two communication formats:
- USB, used by the computer
- UART serial data, used by the ATmega328P
The bridge appears to the computer as a serial port. Its name might look like COM3 on Windows or /dev/cu.usbmodem... on macOS.
The USB connection also supplies power to the board. Data and power travel through the same cable, but they perform different jobs.
Serial data in a sketch
A UART connection sends characters one bit at a time. On the Uno, the hardware UART is connected to digital pins 0 and 1:
- Pin 0 is
RX, for receiving data. - Pin 1 is
TX, for transmitting data.
The USB bridge is also connected to these pins internally. When your sketch uses Serial, the bridge carries that data between the ATmega328P and the computer.
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(A0);
Serial.print("A0 = ");
Serial.println(reading);
delay(500);
}Serial.begin(9600) sets the communication speed to 9,600 bits per second. The Serial Monitor must use the same speed. Serial.print() sends text without automatically starting a new line. Serial.println() sends text and then starts a new line.
To view the output:
- Upload the sketch.
- Open the Serial Monitor in the Arduino IDE.
- Select
9600 baud. - Read the values printed by the sketch.
The Serial Monitor can also send characters to the board. Your sketch must call functions such as Serial.available() and Serial.read() to receive and process them.
Uploading and monitoring at the same time
During an upload, the computer sends the compiled sketch through USB. The bridge passes the data to the ATmega328P's bootloader. The bootloader is a small program that receives and writes new sketches.
After the upload finishes, the board resets and starts the new sketch. Opening the Serial Monitor often resets the Uno as well, so the first few messages may appear only after the reset.
The Serial Monitor is useful for observing sensor readings, debugging decisions, and confirming that a program reached a particular point. It is text-based, so it cannot directly display a graph or interpret a sensor protocol unless your sketch formats the data for another tool.
| Setting | Purpose |
|---|---|
| Baud rate | Must match the value passed to `Serial.begin()` |
| Line ending | Controls which characters the monitor sends after your input |
| Port | Selects the connected Arduino board |
Exercise
A sketch uses Serial.begin(9600), but the Serial Monitor is set to 115200 baud. What result should you expect, and how do you correct it?
Show answer
The monitor will usually show unreadable characters because the speeds do not match. Set the Serial Monitor to 9600 baud.