Sub topic 11
Printing to the Serial Monitor
This chapter assumes you have installed the Arduino IDE, selected a connected board and port, and uploaded at least one sketch. You should also know that setup() runs once and loop() repeats. The Serial Monitor is a tool in the Arduino IDE that displays text sent from the board over its USB serial connection.
Start a serial connection
A serial connection sends data one bit after another between two devices. In this example, the board sends text to your computer.
Call Serial.begin() in setup() to start the connection:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Hello, Arduino!");
delay(1000);
}The number 9600 is the baud rate. It describes the communication speed in symbols per second. The Serial Monitor must use the same baud rate. If the rates do not match, the monitor usually shows unreadable characters.
After uploading this sketch:
- Open the Serial Monitor from the Arduino IDE.
- Set its baud-rate control to
9600. - Confirm that a new line appears about once per second.
Serial.println() prints the text and then moves to a new line. Serial.print() prints text without adding a new line.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Seconds since start: ");
Serial.println(millis() / 1000);
delay(1000);
}millis() returns the number of milliseconds since the board started running the sketch. Dividing by 1000 converts that value to seconds. The expression is calculated before the number is printed.
Understand when output appears
The board restarts when a new sketch is uploaded. It also commonly restarts when the Serial Monitor opens. Because Serial.println() is inside loop(), output continues after the connection starts.
A message printed in setup() might appear only once:
void setup() {
Serial.begin(9600);
Serial.println("Sketch started");
}
void loop() {
delay(1000);
}If the monitor opens after that message was sent, you may not see it. The board does not automatically replay old serial data. Reset the board or upload the sketch again to produce the startup message another time.
Match the settings
The Serial Monitor includes settings that affect how it communicates with the sketch.
| Setting | Purpose | Example |
|---|---|---|
| Baud rate | Sets the communication speed | `9600` |
| Line ending | Adds characters after typed input | No line ending |
| Port | Selects the connected board | `/dev/cu.usbmodem...` |
For output-only sketches, the line-ending setting usually does not matter. It matters when your sketch reads text from the computer with functions such as Serial.read() or Serial.readStringUntil().
Some boards expose more than one serial interface. Always open the monitor for the port selected for the board. If no output appears, check the selected port, the baud rate, and whether the sketch actually calls Serial.println().
> callout tip > Serial output is useful for observing values while a sketch runs. Remove or reduce frequent diagnostic messages when timing matters, because sending text takes time.
Use serial output for diagnosis
Print labels with values so that the output is easy to interpret:
const int sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin);
Serial.print("Sensor value: ");
Serial.println(sensorValue);
delay(500);
}This pattern helps you check whether a sensor is connected and whether its readings change. Keep the label and value on one line when you plan to read the output manually.
Exercise
You upload a sketch that uses Serial.begin(9600), but the Serial Monitor shows strange symbols. What should you check first?
Show answer
Set the Serial Monitor baud rate to 9600. The monitor and the sketch must use the same baud rate.