Introduction
An Arduino has no screen. It runs your sketch perfectly happily whether or not it is doing what you meant, and gives you nothing back. That is what makes a first bug so frustrating: the LED is off, and you have no way of asking why.
Serial.print() is the way in. It sends text back up the USB cable to your laptop, where the Serial Monitor shows it. Suddenly you can see the value of a variable, or whether a line ran at all.
Two things have to line up before a single character appears:
Serial.begin(9600)insetup(), which opens the connection.- The same number chosen in the Serial Monitor's baud dropdown.
Get the second one wrong and you do not get silence — you get a stream of nonsense characters, which is its own useful clue.
This is not only a beginner's tool. Printing what a program is actually doing, rather than what you assume it is doing, is how experienced people debug too. It is the one instrument you always have.
What you will be able to do
By the end of this lesson you can:
- Open a serial connection with
Serial.begin()and match the Monitor's baud rate. - Print text and numbers with
Serial.print()andSerial.println(). - Say exactly what the extra
lndoes. - Build a readable line by combining several prints.
- Show a number in decimal, hexadecimal or binary.
- Use prints to find out why a sketch is not doing what you expected.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring in this lesson. The USB cable that uploads your sketch is also the wire the messages come back along.
→ Serial.begin and println — the same ground at a slower pace. → Debugging with Prints — a whole lesson of finding faults this way.
Opening the connection
void setup() {
Serial.begin(9600);
Serial.println("Ready");
}
void loop() {
}
Upload it, then open the Serial Monitor: Tools → Serial Monitor, or Ctrl+Shift+M. Check the dropdown at the bottom right says 9600 baud.
You should see Ready once. It appears once because setup() runs once.
Baud is simply how many bits per second the two ends have agreed on. 9600 is the usual choice for beginners. The number in your sketch and the number in the dropdown must be identical — nothing checks this for you.
print or println
Serial.print("Sensor"); // stays on this line
Serial.println(" ready"); // ends the line
println is print plus "move to the next line". That is the entire difference.
Use print when you are building one line out of several pieces, and println for the last piece:
Serial.print("Count: ");
Serial.print(7);
Serial.println(" items");
Output:
Challenges
Challenge 1
A startup banner and a counter.
In setup(), print three lines: the sketch name, your name, and Ready.
Then in loop(), print a number that goes up by one every second:
Count: 1
Count: 2
Count: 3
You will need one variable to remember the count. Declare it above setup() so it survives from one pass of loop() to the next:
int count = 0;
Inside the loop, add one to it with count = count + 1;.
Do not forget the delay(1000), or it will count to several thousand before you can read it.
Log in to ask for the answer.
Challenge 2
Make it unreadable, then fix it.
Start from your counter.
- Change every
Serial.printtoSerial.println. Upload, look, and write down what happens. - Change every one to
Serial.printinstead. Upload and look again. - Put it back to a mix that reads properly.
Now extend the line so it reads exactly:
Count: 7 (time: 7 seconds)
The number appears twice, and there are two spaces before the bracket.
Log in to ask for the answer.
Challenge 3
One number, three ways.
Print the number 255 on a single line, in decimal, hexadecimal and binary, labelled:
255 = FF = 11111111
Use Serial.print() with a second argument for the last two, and Serial.println() only for the very end.
Then do the same for 10 and for 64. Look at the binary for 64 — what is unusual about it? Write one sentence.
Log in to ask for the answer.
Extra challenge
Find the fault without changing what it does.
This sketch is supposed to blink the built-in LED once a second. It does not. The LED looks like it is on all the time.
const int LED_PIN = 13;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, HIGH);
delay(1000);
}
Do not fix it yet. First add Serial.begin() and print lines that show which digitalWrite just ran, so the Serial Monitor tells you what the LED should be doing.
Run it. The output proves the fault before you have read the code carefully.
Only then, fix it.
Think about it: you found that bug by making the sketch say what it was doing. What could you not have found this way? Prints show you values and order — but they cost time, and a print inside a very fast loop changes the very thing you are trying to measure.
Log in to ask for the answer.