Students open the serial line in setup(), print a knob's reading every loop and watch it change, build lines with print and println, meet C++'s "words" + number trap (it moves along the text instead of joining), fix a missing Serial.begin, and print a percentage with map.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
SlidesDownload Present from the browser, or export to PowerPoint or PDF.
WorksheetDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
I can print a value to the serial monitor.
I can use print and println to build a line.
I can use printing to find out what my sketch sees.
Timing
Predict and Try 5 min · Worked example 9 min · Vary 8 min · Tinkercad 8 min · Practise and Make 10 min
Before class
The board behaves like the real one: nothing prints without Serial.begin, and "words" + a number moves along the text (pointer arithmetic), printing the end of the words or nonsense. A real board would print whatever happens to be in memory; ours prints a few shaded blocks.
The baud rate (9600) must match the monitor's setting on real hardware; the board here ignores it.
Running the lesson
Predict: ask why it is not 'level'.
Find the mistake: show that Python would join them; C++ doesn't.
Practise, debugging: link back to the night light.
The hard part (on purpose)
Python habits: + to join text. Show what the board really prints, then the two-line fix.
When students get stuck
What you will see
A question to ask
The fix
Nothing prints.
Which line opens the line to the computer?
Serial.begin(9600); in setup().
All the numbers run together on one line.
Which call starts a new line?
println.
Answers to the checks
what appears?: The knob is halfway. loop() runs Serial.println(level); after level = analogRead(A0);. What does the serial monitor show? 512 (halfway on 0 to 1023), one line every time loop() runs. println prints the variable's value, not its name.
Try: watch the monitor: Upload and run, then turn the knob and watch the monitor.
Each line is the reading at that moment: the monitor shows what the board sees.
Build a line: Which pair of lines prints Level: 512 on one line, then starts a new line? Serial.print("Level: "); Serial.println(level); print stays on the line; println ends it.
Find the mistake: Sam's monitor shows bits of Level: and then nonsense instead of Level: 512. Click the line where Sam's sketch goes wrong. line 9 Line 9: in C++, "Level: " + level doesn't join them (as Python would); it moves level characters along the text and prints what is there. Use Serial.print("Level: "); then Serial.println(level);
Add a label: Change the sketch so each line reads Level: and then the number.
Serial.print("Level: "); then Serial.println(level);
Nothing printed: This sketch prints nothing. Fix it.
Serial.begin(9600); was missing from setup(). The line to the computer must be opened first.
Print and println: loop() is Serial.print(1); Serial.print(2); Serial.println(3);. What does the monitor show after two loops? 123 on one line, then 123 on the next print, print, println: one line of 123 per loop.
Debugging: A night light comes on in daylight. What is the most useful first step? Print the reading and compare it with the threshold Printing the value shows whether the reading or the threshold is the problem.
print a percentage: Print the knob as a percentage, 0 to 100, one number per line.
map(level, 0, 1023, 0, 100) turns 0–1023 into 0–100.
Solutions
One possible solution for each task. Other correct programs are fine.
Vary: change one thing › Add a label
Open task: any program that does what the step asks.
Vary: change one thing › Nothing printed
Open task: any program that does what the step asks.
print a percentage
Open task: any program that does what the step asks.
Stretch and support
Stretch: Print Level: 512 (dark) or Level: 512 (light) depending on a threshold.
Support: A card: print = same line, println = new line, begin first.
Exit ticket
Write the two lines in loop() that print Temp: and then the value of temp on one line.
Serial.print("Temp: "); Serial.println(temp);
What each step is for
what appears?: Committing to what println(level) shows tests the new move: the value is sent, not the variable's name.
Try: watch the monitor: Running it and turning the knob settles the prediction: the monitor tracks the reading.
Worked example: print and println: Tracing print and println line by line models how lines are built; Sam's + is planted after the method, with what the real board does.
Vary: change one thing: Adding a label shows print and println together; removing Serial.begin shows why it is in setup().
Build it in Tinkercad: Tinkercad's serial monitor is the one students will use for the real board, so they practise opening and reading it.
printing: Predicting monitor output from print and println, and using printing to debug.
print a percentage: Printing a calculated value (with map) combines a variable, a calculation and println: the monitor then shows the knob as people think of it.
Where it leads
Next: Outputs that vary (analogWrite and PWM).
Code: micro:bit and the computer uses the same serial idea from MakeCode.