Lesson plan

Python on the Pi's pins lesson plan

Drive real parts from Python with gpiozero: LEDs and PWM, buttons and events, buzzer signals and tunes, servos, a sonar and a motion sensor, then build a desk alarm as a state machine.

3 chapters, 7 lessons, about 5 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Pins high and low, PWM, and signals you can see and hear.
  • Pulses that set an angle, echoes that measure, and heat that moves.
  • Sensing, timing and state in one build: the desk alarm.

Materials for the whole unit

  • Half breadboard
  • Hobby servo
  • LED x 2
  • Motion sensor
  • Pushbutton
  • Raspberry Pi 4 Model B
  • Raspberry Pi 5
  • Resistor x 3
  • Traffic light module
  • Ultrasonic ranger

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 2 h 20 min

Outputs

Pins high and low, PWM, and signals you can see and hear.

LessonTimeWhat students do
LEDs and PWMDrive the launch pad lights with gpiozero: on and off, a countdown that blinks, blink() running in the background, PWM for brightness, the duty cycle worked out in milliseconds, and pulse() for a breathing engine glow.50 min
  1. Run it: a pin you control
  2. A countdown
  3. Blink in the background
  4. Dim it with PWM
  5. Count the milliseconds
  6. Breathing engines
Buttons and eventsRead a pushbutton three ways: polling is_pressed in a loop, counting presses by watching for the change, and letting gpiozero call your functions with when_pressed and when_held; then build a reaction timer.45 min
  1. Run it: polling
  2. Count the presses
  3. Events: let it call you
  4. Hold to abort
  5. A reaction timer
Buzzers and tunesMake ship alarms with gpiozero: a Buzzer on and off, beep() in the background, Morse code sent from a dict, the math that turns note names into frequencies, and a rising tune on a TonalBuzzer, with the circuit's LED standing in for the buzzer so you can see every signal.45 min
  1. Run it: a buzzer is a pin
  2. Beep in the background
  3. Morse code
  4. Notes are numbers
  5. A tune

Materials

  • Half breadboard
  • LED x 2
  • Pushbutton
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Buzzers and tunes" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A tune".

Chapter 2. About 1 h 55 min

Motion and sensing

Pulses that set an angle, echoes that measure, and heat that moves.

LessonTimeWhat students do
ServosAim an antenna dish with a hobby servo: Servo min and max, why its default pulses miss this servo's range, AngularServo with the right pulse widths, angles typed in the Serial Monitor and checked first, and a sweep that ends with detach().35 min
  1. Run it: pulses become angles
  2. The right pulse widths
  3. Aim it by hand
  4. Sweep the sky
Distance sensorBuild a sonar with an HC-SR04 and gpiozero's DistanceSensor: read the distance, move the target and see it change, light a warning when something comes within 20 cm, draw a bar graph that grows as it nears, and work out the echo times from the speed of sound, in air and under the sea.45 min
  1. Run it: a ping and an echo
  2. Move the target
  3. Too close!
  4. A bar graph
  5. The physics of the echo
Motion sensorWatch an airlock with a PIR sensor: wait for motion and stillness, switch to when_motion events so the program is free, log every visit with a time, and add a cooldown so one person does not raise ten alerts.35 min
  1. Run it: wait for a warm body
  2. Events, not waiting
  3. A log with times
  4. A cooldown

Materials

  • Half breadboard
  • Hobby servo
  • LED
  • Motion sensor
  • Raspberry Pi 4 Model B
  • Raspberry Pi 5
  • Resistor x 3
  • Ultrasonic ranger

Check for understanding

Each student finishes "Motion sensor" on their own, without opening the hints. The simulator checks 4 of its 4 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A cooldown".

Chapter 3. About 50 min

Project

Sensing, timing and state in one build: the desk alarm.

LessonTimeWhat students do
Project: a desk alarmThe finale: a drawer alarm as a state machine. Arm it from the Serial Monitor, let the drawer switch trigger an entry delay, sound a flashing siren when time runs out, stand it down with a secret knock on the same switch, and keep a timed log of what happened.50 min
  1. Run it: states
  2. The drawer opens
  3. The siren
  4. The secret knock
  5. A timed log
  6. Make it yours

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Project: a desk alarm" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it yours".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.