Lesson plan

Robots in Python on the Pi lesson plan

Build an exploring rover on a Raspberry Pi 4 in Python: gpiozero Robot and the L298N underneath, sonar and line sensors, a state machine, a flight recorder, and an autonomous bot that runs a course on its own.

4 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

  • Drive the rover with gpiozero, then open up the motor driver underneath: H-bridge, encoder and PWM.
  • Sonar for obstacles and infrared for the line, each turned into a number the rover steers by.
  • States and transitions that never freeze, and a log of every run.
  • One autonomous rover that follows, waits, searches, finishes and fails safe.

Materials for the whole unit

  • Line tracker, 5 channel
  • Motor driver module, L298N
  • Raspberry Pi 4 Model B
  • Resistor x 2
  • Robot car chassis, 2WD
  • 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 1 h 20 min

Make it move

Drive the rover with gpiozero, then open up the motor driver underneath: H-bridge, encoder and PWM.

LessonTimeWhat students do
gpiozero RobotMeet the rover: a Pi 4, an L298N and a two-wheel chassis. Drive it with gpiozero's Robot: forward, backward, spin on the spot, curve at part speed, and set both wheels at once with a value pair to drive a square.40 min
  1. Run it: roll out
  2. Reverse
  3. Spin on the spot
  4. Part speed and curves
  5. Drive a square
Motor driversOpen up the L298N: drive one wheel with three plain pins, reverse it through the H-bridge, measure how far it rolls with the wheel's encoder, set the speed with PWM on the enable pin, and see that gpiozero's Motor is exactly those three pins.40 min
  1. Run it: one wheel by hand
  2. Reverse through the H-bridge
  3. How far does it roll?
  4. Speed with PWM
  5. It was a Motor all along

Materials

  • Line tracker, 5 channel
  • Motor driver module, L298N
  • Raspberry Pi 4 Model B
  • Resistor x 2
  • Robot car chassis, 2WD
  • Ultrasonic ranger

Check for understanding

Each student finishes "Motor drivers" 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: "It was a Motor all along".

Chapter 2. About 1 h 20 min

Sense the world

Sonar for obstacles and infrared for the line, each turned into a number the rover steers by.

LessonTimeWhat students do
Distance sensingGive the rover eyes: read the HC-SR04 with DistanceSensor, time its echo by hand to see where the number comes from, stop before a wall, slow down in proportion to the gap, and back away and turn when blocked.40 min
  1. Run it: how far?
  2. Where the number comes from
  3. Stop before the wall
  4. Slow down smoothly
  5. Back away and turn
Line sensingRead five infrared line sensors as a pattern, turn the pattern into the tape's position in millimeters, steer with if and else, then steer smoothly in proportion to the error, and stop at the finish bar.40 min
  1. Run it: five eyes on the floor
  2. Where is the line?
  3. Steer: if and else
  4. Steer in proportion
  5. The finish bar

Materials

  • Line tracker, 5 channel
  • Motor driver module, L298N
  • Raspberry Pi 4 Model B
  • Resistor x 2
  • Robot car chassis, 2WD
  • Ultrasonic ranger

Check for understanding

Each student finishes "Line sensing" 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: "The finish bar".

Chapter 3. About 1 h 20 min

Think and remember

States and transitions that never freeze, and a log of every run.

LessonTimeWhat students do
A state machineOrganize a robot's behavior as states and transitions: explore, avoid on a timer without freezing, back up when too close, then turn the whole thing into a table where a new behavior, coming home to base, is one more row.40 min
  1. Run it: one state
  2. A second state: AVOID
  3. A timer that does not freeze
  4. Too close: BACKUP
  5. The machine as a table
Logging a runMake the rover keep a flight recorder: measure distance from its wheel encoders, write a timestamped CSV while it drives, work out its speed from the log, flag the odd moments with the logging module, and save a JSON summary.40 min
  1. Run it: odometry
  2. A flight recorder: CSV
  3. Speed from the log
  4. Levels: logging what matters
  5. A summary to share

Materials

  • Line tracker, 5 channel
  • Motor driver module, L298N
  • Raspberry Pi 4 Model B
  • Resistor x 2
  • Robot car chassis, 2WD
  • Ultrasonic ranger

Check for understanding

Each student finishes "Logging a run" 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 summary to share".

Chapter 4. About 50 min

Boss

One autonomous rover that follows, waits, searches, finishes and fails safe.

LessonTimeWhat students do
Boss: an autonomous Pi botPut it all together in one autonomous rover: follow the tape with a proportional controller, wait for obstacles with hysteresis, search for a lost line, stop at the finish bar with a mission report, and fail safe with a timeout and try/finally.50 min
  1. Run it: the mission skeleton
  2. WAIT, with hysteresis
  3. SEARCH for a lost line
  4. DONE, with a mission report
  5. Fail safe
  6. Run the course

Materials

  • Line tracker, 5 channel
  • Motor driver module, L298N
  • Raspberry Pi 4 Model B
  • Resistor x 2
  • Robot car chassis, 2WD
  • Ultrasonic ranger

Check for understanding

Each student finishes "Boss: an autonomous Pi bot" 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: "Run the course".

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.