Sub topic 1
How an Arduino talks to your computer
Prerequisites
This chapter assumes you can already do the following:
- Write and upload a basic sketch with
setup()andloop()from the Arduino IDE. - Use variables, data types, and
ifstatements in C/C++. - Read an analog pin with
analogReadand write a pin withdigitalWrite.
You do not need any electronics knowledge beyond that. This chapter covers the connection itself. Later chapters cover the functions you call to use it.
Why a board needs a channel to the computer
An Arduino has no screen. When a sketch runs, you cannot see its variables, which branch of an if it took, or what a sensor is reporting. The board does have a link to your computer, though, and the sketch can send data through it. The Arduino IDE shows that data in a window called the Serial Monitor.
This one channel gives you three things:
- Display: show sensor readings and results as text.
- Debugging: print messages that show which code ran and what values it used.
- Control: send commands from the computer to the board.
The rest of the tutorial builds on this channel. This chapter explains how it works, so later problems such as garbled text or a missing port make sense.
What "serial" means
Serial communication sends data one bit at a time over a single wire, in order. A bit is a single 0 or 1. The alternative is parallel communication, which uses many wires to send several bits at once. Serial needs fewer wires and is simpler to build, which is why it is common between small devices.
To send a byte (8 bits), a serial link sends the bits one after another. A character such as A is stored as a number (65 in ASCII), and that number is sent as one byte. The text Hi is two bytes. A newline, which ends a line of text, is also a byte.
UART: the hardware inside the chip
Most Arduino boards have a UART inside the microcontroller. UART stands for Universal Asynchronous Receiver/Transmitter. It is a hardware block that converts bytes into a stream of bits on one pin and does the reverse on another pin.
On the Arduino Uno, the UART uses two pins:
| Pin | Name | Direction |
|---|---|---|
| 0 | RX | Receives data into the board |
| 1 | TX | Transmits data out of the board |
Two details matter here:
- Crossed wires: one device's TX connects to the other device's RX, and the reverse. Both devices must also share a ground.
- Asynchronous: there is no separate clock wire. Both sides agree on a speed in advance, and each one times the bits on its own. That agreed speed is the baud rate, covered in the next chapter.
What a byte looks like on the wire
The UART does not send the 8 data bits alone. The default Arduino setting, called 8N1, wraps each byte in extra bits:
- The line idles at a high voltage.
- A start bit pulls the line low. This tells the receiver that a byte is coming.
- Eight data bits follow, least significant bit first.
- No parity bit is used (the "N" in 8N1).
- A stop bit returns the line high (the "1" in 8N1).
That makes 10 bits on the wire for every byte of data. This is why the speed in bytes per second is about one tenth of the speed in bits per second. At 9600 bits per second, you get roughly 960 bytes per second. A line such as Hello plus a carriage return and newline is 7 bytes, so it takes about 7 ms to send.
From the UART to your computer over USB
Your computer does not have an RX and TX pin that you wire to the board. It talks to the board over USB. Something has to translate between USB and the UART signals. How that works depends on the board.
| Board type | How USB is handled |
|---|---|
| Uno R3 | A second chip on the board (an ATmega16U2) converts USB to UART signals and passes them to the main chip. |
| Many clones and the Nano | A USB-to-serial chip such as the CH340 or FT232 does the same job. |
| Leonardo, Micro, and many newer boards | The main chip has USB built in. It appears as a virtual serial port. |
On your computer, all of these show up as a serial port. A serial port is a name the operating system gives to the connection. Examples are COM3 on Windows, /dev/ttyACM0 or /dev/ttyUSB0 on Linux, and /dev/cu.usbmodem14101 on macOS. A program such as the Serial Monitor opens this port and reads or writes bytes through it.
The same USB cable also uploads your sketches. The IDE uses the same port for uploading and for the Serial Monitor, so only one program can use the port at a time. If the Serial Monitor is open when you upload, the IDE usually handles it, but a different program holding the port can block the upload.
Pins 0 and 1 are shared
On an Uno, the USB chip is wired to the same RX and TX pins (0 and 1) that you can see on the header. Two consequences follow:
- Avoid using pins 0 and 1 for other purposes while you use
Serial. Outside signals on those pins can corrupt the data. - If you connect another device to pins 0 and 1, it can interfere with uploads and with the Serial Monitor.
Some boards have more than one UART. The Mega has Serial, Serial1, Serial2, and Serial3. In this tutorial, Serial always means the connection to your computer.
What the sketch sees
Inside your sketch, the UART has two buffers, which are small areas of memory that hold bytes in transit:
- A transmit buffer holds bytes you have printed but the UART has not sent yet.
- A receive buffer holds bytes that have arrived but your sketch has not read yet.
The UART works in the background using interrupts, so the sketch does not have to time each bit. When you call Serial.print, the bytes are copied into the transmit buffer and your code continues. The UART sends them at the agreed speed while your sketch does other work. If you print faster than the link can send, the buffer fills and the call waits. That waiting is one reason heavy printing slows a sketch down, and a later chapter covers it in detail.
Incoming bytes work the same way. They land in the receive buffer, and the sketch reads them later with functions such as Serial.available() and Serial.read(). If you do not read them in time, the buffer fills and new bytes are lost.
The reset when you open the port
On an Uno, opening the Serial Monitor makes the board restart. The USB chip toggles the board's reset line when a program opens the port. This is also how uploads work, since the board must restart into the bootloader (the small program that receives new sketches).
The effect is that your sketch begins from the top of setup() each time you open the Serial Monitor. Any message printed in setup() appears in the monitor after the reset. If you open the monitor late, you do not miss it, because the board restarts. On boards with native USB, no reset happens, and messages printed in setup() can be sent before the monitor is open and be lost.
A first working example
This sketch uses everything above. It starts the serial connection, prints one line, and then prints the elapsed time in milliseconds once per second. The next chapter explains Serial.begin(9600) in detail. For now, the point is that the same speed must be set on both ends.
void setup() {
Serial.begin(9600);
Serial.println(F("Setup complete"));
}
void loop() {
Serial.println(millis());
delay(1000);
}To run it:
- Select your board and port in the IDE, and upload the sketch.
- Open the Serial Monitor from the Tools menu or with its toolbar icon.
- Set the speed in the monitor to 9600 baud.
You should see Setup complete, and then a number that grows by about 1000 each line. The first value after the message is close to 0, because the board restarted when the monitor opened. If you see strange characters instead, the speeds do not match.
Common misunderstandings
- "The Serial Monitor talks to the sketch directly." It does not. It only reads and writes bytes on the serial port. The sketch sees bytes in its receive buffer, with no knowledge of the window.
- "Print is instant." Each byte takes time to send. At 9600 baud, a long line takes many milliseconds.
- "USB is the serial link." USB carries the data between the computer and the board, but the sketch's
Serialobject sits on top of a UART-style interface. The USB chip hides the difference. - "I can use pins 0 and 1 freely." On boards where they connect to the USB chip, other uses can break the connection.
Summary of the model
Keep this model in mind for the rest of the tutorial:
- Your sketch writes bytes into a transmit buffer.
- The UART sends them one bit at a time, using start and stop bits and an agreed speed.
- A USB chip, or the microcontroller itself, passes them to the computer, which exposes a serial port.
- The Serial Monitor reads that port and shows the bytes as text.
- Text you type goes the other way, into the receive buffer, where your sketch can read it.
Exercise
A sketch runs on an Uno with Serial.begin(9600). About how long does it take to send the 7-byte line "Hello" followed by a carriage return and a newline, and why is it not simply 7 × 8 bits ÷ 9600?
Show answer
It takes about 7 ms. Each byte is sent in 8N1 format, which uses 10 bits on the wire: 1 start bit, 8 data bits, and 1 stop bit. So 7 bytes is 70 bits, and 70 ÷ 9600 is about 0.0073 seconds, or 7.3 ms. Counting only the 8 data bits (56 bits) would give about 5.8 ms and would miss the start and stop bits.
Exercise
You open the Serial Monitor on an Uno and see "Setup complete" even though you opened the monitor several seconds after the upload finished. Why does the message still appear?
Show answer
Opening the serial port toggles the board's reset line through the USB chip, so the board restarts and runs setup() again. The message is printed after the monitor is open. A board with native USB, such as the Leonardo, does not reset this way, so a message printed early in setup() can be lost.