Sub topic 6
The USB-to-serial bridge and the Serial Monitor
This chapter assumes you know what a UART is at a basic level (two wires, TX and RX, sending bytes one bit at a time), that you have uploaded a sketch from the Arduino IDE, and that you know the setup()/loop() structure. It covers how the Serial Monitor can talk to a chip that has no USB hardware, and what goes wrong when the pieces disagree.
Why a bridge exists
The ATmega328P on an Uno has a hardware UART but no USB controller. A computer, though, only offers USB ports. A second chip on the board closes the gap. On the Uno R3 this is an ATmega16U2. Many clones use a CH340 or CP2102 instead, and older boards used an FTDI chip.
The bridge does two jobs:
- It presents itself to the computer as a USB device, usually a virtual serial port (COM3 on Windows,
/dev/ttyACM0or/dev/ttyUSB0on Linux,/dev/cu.usbmodem...on macOS). - It copies bytes between that virtual port and the UART pins of the main microcontroller.
The bridge does not interpret the data. It does not know whether you are sending text, numbers or sensor packets. It only moves bytes in both directions.
The Serial object and pins 0 and 1
In a sketch, Serial is the object that controls the hardware UART. Pin 0 is RX (receive) and pin 1 is TX (transmit). These pins are wired to the bridge chip, which is why the board is labeled RX←0 and TX→1.
Three consequences follow:
- While you use
Serial, pins 0 and 1 are busy. Connecting an LED or a button to them can corrupt uploads and serial output. - The pins are shared. Any other device attached to pins 0 and 1 (a Bluetooth module, for example) receives everything the sketch prints, and can interfere with what the computer sends.
- The bridge also drives the reset line during uploads, which the next chapter covers.
The two small LEDs marked TX and RX near the USB connector blink when the bridge sees traffic. They are a quick way to check whether data moves in either direction.
Printing values
This sketch reads A0, converts the 10-bit reading to volts, and prints it twice a second.
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(A0);
float volts = raw * (5.0 / 1023.0);
Serial.print(raw);
Serial.print(" -> ");
Serial.print(volts, 2);
Serial.println(" V");
delay(500);
}After uploading, open Tools > Serial Monitor and set the baud rate menu to 9600. You will see lines such as 512 -> 2.50 V. With A0 unconnected, the values wander because the pin floats, so connect it to 5V, GND, or a potentiometer wiper to get stable readings.
How the calls behave:
Serial.begin(9600)sets the UART speed and enables the hardware. Call it once, insetup().Serial.print()sends text without a line ending.Serial.println()adds a carriage return and a newline (two bytes,\r\n).- The second argument of
Serial.print(volts, 2)is the number of decimal places for a float. For an integer it can select a base, as inSerial.print(255, HEX). Serial.print(65)sends the characters6and5.Serial.write(65)sends the single byte 65, which a terminal shows asA.
Baud rate
The baud rate is the number of signal changes per second on the line. For this UART, one baud equals one bit per second. Both ends must use the same rate because there is no separate clock wire. The receiver samples the line at timed intervals, and if its timing differs from the sender's, it reads the wrong bits.
A typical frame at the default 8N1 setting (8 data bits, no parity, 1 stop bit) is 10 bits per byte. At 9600 baud, that is about 960 bytes per second. At 115200 baud it is about 11,520 bytes per second.
| Symptom | Likely cause |
|---|---|
| Garbled characters such as `ÿ¿` or `` | Monitor baud rate differs from `Serial.begin()` |
| Nothing appears | Wrong port selected, `Serial.begin()` missing, or the sketch has not started |
| Output appears but stops after a while | Another device is on pins 0 and 1, or the board is resetting |
| Text appears twice, or lines run together | Both a line-ending setting and `println()` are adding newlines, or neither is |
Two details about the numbers. The Serial Monitor's baud rate setting configures only the computer side of the virtual port. The sketch chooses the board side. The bridge chip follows whichever rate the computer opens the port with, so changing the monitor setting changes the whole link, as long as the sketch matches.
Also, at 16 MHz the ATmega328P cannot make every baud rate exactly. It divides its clock by an integer, so some rates carry a small timing error. 9600 and 115200 work reliably on the Uno. Rates like 230400 or 250000 can show more errors, so test them before relying on them.
Sending data to the board
The monitor has an input box and a line-ending menu (No line ending, Newline, Carriage return, Both NL & CR). Whatever you type is sent, followed by the chosen ending. The sketch reads bytes from a receive buffer.
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Send 1 to turn the LED on, 0 to turn it off.");
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == '1') {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("LED on");
} else if (c == '0') {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("LED off");
}
}
}Serial.available() returns the number of bytes waiting. Serial.read() removes one byte and returns it, or returns -1 if none is waiting. The sketch ignores any other character, including the newline the monitor adds, so it works with any line-ending setting.
The receive buffer on the Uno holds 64 bytes. If the sketch does not read fast enough, new bytes are discarded. A long delay() in loop() is the usual cause. The transmit side also has a 64-byte buffer. Serial.print() returns quickly while space remains, but once the buffer is full it blocks until bytes have been sent. Printing a lot at 9600 baud can therefore slow your loop.
Reset on connect
On a standard Uno, opening the Serial Monitor resets the board. The computer toggles the DTR line when it opens the port, and the bridge passes that signal through a capacitor to the reset pin of the ATmega328P. The sketch restarts from the beginning each time. The consequences:
- Variables return to their initial values when you open the monitor.
- Startup messages in
setup()appear every time you open it. - A sketch can seem to "lose" state, when it was simply restarted.
Boards with native USB, such as the Leonardo, behave differently. Their USB is part of the main chip and the port disappears and reappears during reset. Those boards need while (!Serial); to wait for the monitor, which the Uno does not.
Debugging with Serial
Printing is the main debugging tool on this board. A few habits help:
- Label every value (
Serial.print("raw=")), so lines are readable. - Print only when something changes if the loop runs fast. A flood of output hides the interesting lines and slows the sketch.
- Store constant text in flash with
F(), as inSerial.println(F("Ready")). The Uno has 2 KB of RAM, and many string literals consume it quickly. - Remove or disable debug prints once the sketch works, because each one costs time and memory.
The IDE also includes the Serial Plotter, which graphs numbers sent on the same port. It expects one or more numeric values per line, separated by spaces or commas, so the output of the voltage sketch needs reformatting before it can be plotted.
Exercise
You upload a sketch that calls Serial.begin(115200) and prints "Hello" every second. The Serial Monitor shows unreadable symbols. What is the most likely cause, and what is the fix? Also, why does the sketch print its startup message again each time you reopen the monitor on an Uno?
Show answer
The monitor is set to a different baud rate than the sketch, probably 9600. Set the monitor's baud menu to 115200 so both ends sample the line at the same speed. The startup message repeats because opening the port toggles DTR, which the bridge routes to the ATmega328P's reset pin, so the board restarts and runs setup() again.
Exercise
A sketch calls Serial.print(200) and another calls Serial.write(200). What does each send, and how many bytes?
Show answer
Serial.print(200) sends the three characters 2, 0, 0, which is three bytes (ASCII 50, 48, 48). Serial.write(200) sends one byte with the value 200, which a terminal shows as a symbol or nothing readable, depending on its character set.