All paths

Robots in Python on the Pi

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.

Raspberry Pi 4 Model BFree7 lessons5 h4 chapters2,063 XPCoding · Intermediate+150 XP

0 of 7 done

What you will build

3 projects, built step by step

7 lessons in 4 chapters, about 5 hours on a simulated Raspberry Pi 4 Model B. Free, in your browser, every step checked by the simulator.

Chapter 1 project

Motor drivers

Open 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.

Open it in the simulator
Chapter 2 project

Line sensing

Read 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.

Open it in the simulator
Chapter 3 project

Logging a run

Make 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.

Open it in the simulator
The map

Chapter by chapter

Each lesson opens when the one before it is done, and each chapter ends in a project.

Chapter 1

Make it move

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

  1. Open205 XP

    gpiozero Robot

    Meet 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.

    0 of 5 steps

  2. ProjectLocked410 XP

    Motor drivers

    Open 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.

    0 of 5 steps

5 questions, graded as you go.
Chapter 2

Sense the world

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

  1. Locked205 XP

    Distance sensing

    Give 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.

    0 of 5 steps

  2. ProjectLocked410 XP

    Line sensing

    Read 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.

    0 of 5 steps

5 questions, graded as you go.
Chapter 3

Think and remember

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

  1. Locked205 XP

    A state machine

    Organize 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.

    0 of 5 steps

  2. ProjectLocked410 XP

    Logging a run

    Make 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.

    0 of 5 steps

5 questions, graded as you go.
Chapter 4

Boss

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

  1. Locked218 XP

    Boss: an autonomous Pi bot

    Put 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.

    0 of 6 steps

4 questions, graded as you go.
Syllabus

Every lesson in the course

All 7 lessons, in order, each with what it covers. Any of them opens in the editor right away.

Chapter 1

Make it move

  1. 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.
  2. 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.
Chapter 2

Sense the world

  1. 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.
  2. 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.
Chapter 3

Think and remember

  1. 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.
  2. 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.
Chapter 4

Boss

  1. 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.
Kits

Build it on your bench too

Every lesson runs in the simulator with nothing to buy. When you want the real thing, these kits have the parts.

Have a kit already?

Find your kit

Pick the starter kit you own and run every lesson in its manual with the same parts on the same pins.

See every kit
Questions

About Robots in Python on the Pi

Is Robots in Python on the Pi free?

Yes. All 7 lessons of Robots in Python on the Pi are free and run in your browser, with nothing to install and no parts to buy before you start.

Do I need a real Raspberry Pi 4 Model B?

No. Every lesson runs on a simulated Raspberry Pi 4 Model B with its parts on the canvas, and the simulator checks each step as you go. When you want to build it for real, the same wiring and code work on a real board.

How long does Robots in Python on the Pi take?

About 5 hours: 7 lessons in 4 chapters, each one a few short steps. Your progress is kept, so you can stop after any lesson and pick up where you left off.

What will I build?

Each chapter ends in a project that puts it together: Motor drivers, Line sensing and Logging a run.

Where do I start?

With "gpiozero Robot", and each lesson builds on the one before it. New to circuits? Electronics basics is the course that starts from zero.