Introduction
Serial has been one-way so far: the Arduino talks, you read. The same connection carries traffic the other way, and that turns a sketch into something you can operate.
Three calls do nearly all of it:
Serial.available()— how many characters are waiting. Zero means nothing has arrived.Serial.read()— takes the next character and removes it.Serial.parseInt()— reads a whole number.
The idea that trips everyone is that input arrives one character at a time, and slowly. Type 42 and press enter, and the Arduino may see the 4 before the 2 has even left your laptop. A sketch that reads once and assumes the whole message arrived gets the 4 and nothing else.
So the pattern is always the same: check whether anything is waiting, and only then read. Never read without asking first — Serial.read() on an empty buffer returns -1, and a sketch that ignores that ends up processing a character that never existed.
What you will be able to do
By the end of this lesson you can:
- Check for waiting input with
Serial.available(). - Read a single character with
Serial.read(). - Read a whole number with
Serial.parseInt(). - Say why
-1comes back from an empty buffer. - Explain why input arrives one character at a time.
- Build a simple command menu driven from the Serial Monitor.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. You will type into the Serial Monitor's input box at 9600 baud.
→ Reading from Serial — the same ground with hardware attached.
→ ASCII Basics — why '5' and 5 are different numbers.
Reading one character
void setup() {
Serial.begin(9600);
Serial.println("Type something");
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
Serial.print("got: ");
Serial.println(c);
}
}
Type hi and press enter. You get three lines — h, i, and a newline character. Each character is separate.
Serial.available() is the guard. Without it, Serial.read() returns -1 every time round an empty loop, thousands of times a second.
The newline
Pressing enter sends an extra character. The dropdown at the bottom of the Serial Monitor controls which — usually Newline.
That stray character is behind most "it works but there is an extra blank line" reports. To ignore it:
char c = Serial.read();
if (c == '\n' || c == '\r') {
return; // skip line endings
}
'5' is not 5
Challenges
Challenge 1
Echo, and count.
Echo every character you type back with its ASCII code:
got 'h' (104)
got 'i' (105)
Type hi and press enter. How many lines appear, and why is it more than two?
Then filter out '\n' and '\r' so only real characters are echoed.
Finally, count the characters received since the board started and print the running total.
Log in to ask for the answer.
Challenge 2
Type a number, get an answer.
Read a whole number with parseInt() and print its double, its square, and whether it is even.
you sent 12
double 24
square 144
even yes
Add Serial.setTimeout(50); in setup() and note whether the sketch feels quicker to respond.
Then type a letter instead of a number. What comes back, and how would you tell that apart from somebody genuinely typing 0?
Log in to ask for the answer.
Challenge 3
A command menu.
Build a menu driven by single keystrokes:
| Key | Action |
|---|---|
+ | add 1 to a counter |
- | subtract 1 |
r | reset to 0 |
p | print the counter |
? | list the commands |
Anything else prints unknown command: x.
Use a switch. Make sure pressing enter does not trigger the unknown message.
Log in to ask for the answer.
Extra challenge
A serial-driven blink.
Combine everything: type a number and the built-in LED blinks that many times.
Serial.setTimeout(50)so it responds quickly.- Reject anything below 1 or above 20, with a message saying why.
- Print a countdown as it blinks:
blink 1 of 5,blink 2 of 5. - Use a
forloop for the blinking and a functionvoid blinkTimes(int n)for the work.
While the blinking runs, the sketch is inside delay() and cannot read anything you type. Type during a long blink and see what happens when it finishes.
Think about it: those keystrokes were not lost — they waited in the buffer and arrived all at once. Where were they kept, and what do you think happens if you type far more than the sketch ever reads?
Log in to ask for the answer.