Sub topic 4
Seeing Values with the Serial Monitor
Assume you can open, verify, and upload an Arduino sketch, and that you know the roles of setup() and loop(). This chapter focuses on serial output: sending text and numbers from the board to the Arduino IDE so you can inspect program behavior.
What the Serial Monitor does
The Serial Monitor is a text window that receives data from the Arduino through the USB connection. Your sketch sends that data with the Serial object.
Before sending anything, initialize serial communication in setup():
Serial.begin(9600);The number 9600 is the communication speed, measured in bits per second. The Serial Monitor must use the same speed. If the speeds differ, the displayed text usually appears as unreadable characters.
The most common output methods are:
| Method | Result |
|---|---|
| `Serial.print(value)` | Writes a value without moving to a new line |
| `Serial.println(value)` | Writes a value and starts a new line |
| `Serial.print("text")` | Writes a string of text |
| `Serial.println(number)` | Writes a number followed by a new line |
Print variables while a sketch runs
Printing a variable lets you observe values that are otherwise invisible. This example reads an analog input and prints the raw reading:
const byte sensorPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
Serial.print("Sensor reading: ");
Serial.println(reading);
delay(500);
}Upload the sketch, open the Serial Monitor, and select 9600 baud. The board prints one reading every half second. On many Arduino boards, an analog input returns a value from 0 to 1023, depending on the input voltage.
The text inside quotation marks is a string literal. The variable reading is printed without quotation marks, so the Serial Monitor displays its current numeric value rather than the word reading.
You can print several values on one line:
Serial.print("Raw: ");
Serial.print(reading);
Serial.print(" doubled: ");
Serial.println(reading * 2);The arithmetic expression is evaluated before its result is printed.
Use serial output to inspect decisions
Serial output is useful when a condition does not behave as expected. Print both the input value and the decision made by the program:
const byte sensorPin = A0;
const int threshold = 600;
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
bool aboveThreshold = reading > threshold;
Serial.print("Reading = ");
Serial.print(reading);
Serial.print(", above threshold = ");
Serial.println(aboveThreshold ? "yes" : "no");
delay(500);
}The comparison reading > threshold produces a Boolean value. A Boolean is a data type with either true or false. The conditional operator, written condition ? value1 : value2, selects one of two values.
Trace program flow
You can also print messages when the program enters a section of code:
void setup() {
Serial.begin(9600);
Serial.println("Starting sketch");
}
void loop() {
Serial.println("Beginning loop");
int value = analogRead(A0);
if (value > 600) {
Serial.println("High input");
} else {
Serial.println("Low input");
}
delay(1000);
}These messages reveal whether a branch runs and how often loop() repeats. Avoid printing continuously at very high speed. Serial transmission is relatively slow, and excessive output can make timing less predictable.
The data path
Common errors
- Wrong baud rate: Set the Serial Monitor to the same rate passed to
Serial.begin(). - Missing initialization: Call
Serial.begin()insetup()before printing. - No line breaks: Use
println()when each message should appear on its own line. - Misleading labels: Include units or clear names, such as
Temperature: 23 C. - Too much output: Print at a controlled interval while debugging timing-sensitive code.
Exercise
A sketch prints Serial.println("count"); repeatedly, but you want to see the value of the variable count. What should you change?
Show answer
Remove the quotation marks around the variable: Serial.println(count);. Quotation marks would print the literal word count.