Sub topic 2
Starting serial with `Serial.begin` and choosing a baud rate
Assume you already know the Arduino setup() and loop() functions, can upload a sketch, and understand variables and if statements. This chapter focuses on opening a serial connection between an Arduino and a computer.
How serial communication works
Serial communication sends data one bit at a time in a sequence. An Arduino can use a UART, or Universal Asynchronous Receiver-Transmitter, to exchange these bits with a computer.
On an Uno, the USB connection usually passes through a USB-to-serial chip. Your sketch sends bytes to the board's serial hardware, and the chip forwards those bytes to the computer. The Arduino IDE's Serial Monitor then displays the bytes as text.
A serial connection needs two main settings:
- The correct serial port, which identifies the connected board.
- The correct baud rate, which is the signaling speed in bits per second.
Starting a connection with `Serial.begin`
Call Serial.begin() in setup(), because setup() runs once after reset or upload. Its argument selects the baud rate.
const unsigned long BAUD_RATE = 9600;
void setup() {
Serial.begin(BAUD_RATE);
Serial.println("Serial connection started");
}
void loop() {
Serial.print("Milliseconds: ");
Serial.println(millis());
delay(1000);
}This sketch starts communication at 9600 baud and prints the elapsed time once each second. The millis() function returns the number of milliseconds since the board started running the sketch.
Common baud rates include 9600, 19200, 57600, and 115200. A higher rate can transmit output faster, but both sides must use the same setting.
Matching the Serial Monitor
After uploading the sketch:
- Select the board's port in the Arduino IDE.
- Open the Serial Monitor.
- Select
9600 baudfrom the monitor's speed menu. - Press the board's reset button if the startup message is no longer visible.
The Serial Monitor may reset some boards when it opens. This can cause the startup message to appear again.
| Sketch setting | Serial Monitor setting | Result |
|---|---|---|
| `Serial.begin(9600)` | `9600 baud` | Readable output |
| `Serial.begin(9600)` | `115200 baud` | Garbled output |
| `Serial.begin(115200)` | `115200 baud` | Readable output |
Choosing a baud rate
Use 9600 baud for simple status messages and early experiments. It is slow, but it works reliably on many boards and connections.
Use 115200 baud when a sketch produces frequent output, such as fast sensor readings. Printing faster does not automatically make the sketch faster. Serial transmission takes time, and excessive output can fill the transmit buffer and delay other work.
On some native-USB boards, Serial uses USB communication rather than a traditional UART. The selected baud rate may not control the physical USB signaling in the same way. Still, selecting the same rate in the sketch and Serial Monitor is a clear convention and remains important when using a hardware UART or USB-to-serial adapter.
Exercise
A sketch contains Serial.begin(115200), but the Serial Monitor displays unreadable symbols. What should you check first?
Show answer
Check that the Serial Monitor is connected to the correct port and is set to 115200 baud. Also verify that another program is not using the port.