Session 3: Variables and Serial output
Learning outcomes
- Declare, assign and print variables with Serial.print and Serial.println
- Use const for pin numbers and fixed values
- Explain global and local scope in a sketch
- Predict a sketch's Serial output before running it
Kit and components
- Uno, USB cable and lab PC
- Serial Monitor at 9600 baud
Pre-reading about 20 min
Students read these on their phones before class. They are listed on the student Before class page.
Session plan
- 0:0010 minRecap
Quiz: setup, loop and upload
- 0:1025 minDemo
Variables change, constants do not
Live-code a counter printed from loop(). Students predict each line before you run it. Then move the declaration inside loop() and show the counter reset every pass.
- 0:3525 minBuild
Worked sketch on the board
Students upload the lesson's worked sketch, change one constant and one variable, and record the new Serial output in the logbook.
- 1:0045 minCode Along
Variables
Pairs share a PC for the first exercise, then work individually.
- 1:4515 minWrap-up
Predict the output
Project a six-line sketch. Students write its Serial output on paper before you run it.
Checkpoint: ready to move on?
Students can declare an int and a const, print a labelled value on one line, and explain why a variable declared inside loop() resets every pass.
Extension for fast finishers
Catch-up path
Repeat the first variables exercise with the hints open, concentrating on print versus println.
Homework and practice
Code Along 2.3 and 2.4.
Facilitator notes
- Beginners mistype more than they misunderstand. Let the compiler messages teach and avoid fixing code for students.
- Code Along compares Serial output exactly - capital letters and spaces matter.