Raspberry Pi course: learn GPIO with Python, step by step
18 guided lessons that take you from a blinking LED to sensors, displays and an RFID lock. You wire each circuit on a breadboard and write the Python yourself, in the browser. A ✔ Check button tests your wiring and your code after every lesson and tells you what to fix.
Course 1: Raspberry Pi with Python
Eleven lessons on the Raspberry Pi 4, the Raspberry Pi 5 and the Raspberry Pi Zero 2 W, from the first LED to an alarm with two sensors.
Lesson 2: Inputs
Button and LED in Python
Read a push button with a pull-up resistor, and make a decision with if and else.
Lesson 3: The gpiozero library
Button and LED with gpiozero
Do the same job with LED and Button objects on the Raspberry Pi 5, where RPi.GPIO does not work.
Lesson 4: Events
Doorbell with a button and a buzzer
Wait for a press and a release instead of checking in a loop, and count with a variable.
Lesson 5: I²C and displays
Message and timer on an LCD
Send text to a 16 × 2 LCD over two wires, and place it with the cursor.
Lesson 6: Analog input and PWM
Dim an LED with a knob and an MCP3008
Read a knob through the MCP3008 chip, because a Raspberry Pi has no analog inputs, and dim an LED.
Lesson 7: 1-Wire sensors
Digital thermometer with a DS18B20
Read a DS18B20 temperature sensor and compare the reading with two limits.
Lesson 8: OLED displays
Thermometer on an OLED display
Draw the temperature on a 128 × 64 OLED with busio and adafruit_ssd1306, and redraw it every second.
Lesson 9: SPI and identity
RFID door lock in Python
Read the number of an RFID tag and decide who may pass.
Lesson 10: Motors
Stepper motor in Python
Turn a stepper motor by switching its four coils in order, and turn it back by reversing the order.
Lesson 11: Final build
Tank alarm with two sensors
Use an ultrasonic sensor and a float switch together, so the alarm sounds only when both agree.
Course 2: Raspberry Pi Pico with MicroPython
Seven lessons on the Raspberry Pi Pico, Pico 2, Pico W and Pico 2 W, from the first LED to web pages over Wi-Fi and a thermometer with its own display.
Lesson 2: Sequences
Traffic light in MicroPython
Run three LEDs in a fixed order with the right times.
Lesson 3: Analog input and PWM
Dim an LED with a knob in MicroPython
Read a knob with the ADC and set the brightness of an LED.
Lesson 5: Wi-Fi and web pages
Phone-controlled LED
Join Wi-Fi with network.WLAN and switch an LED from a phone through a small web server.
Lesson 6: Live readings over Wi-Fi
Knob on a web page
Put a knob reading into a web page, read again for every visitor.
Lesson 7: Final build
Thermometer on an OLED display
Read a DS18B20 sensor and draw the temperature on an OLED display.
- What the Raspberry Pi course is
- What the Raspberry Pi course is for
- How the course is organised
- How to use this page
- Key words
- Where to start
What the Raspberry Pi course is
The Raspberry Pi course is a set of guided lessons about controlling electronics from a program. Each lesson is one small project: a circuit to wire on a breadboard and a program to write. The lessons run in the Science Tinker microcontroller simulator, which copies the pins, the Python libraries and the error messages of the real boards, so nothing has to be bought or installed. After each lesson a Check button tests the wiring and the code, and says what to fix if something is wrong.
The course teaches physical computing. This means writing programs that sense and change the real world through the pins of a board: reading a button, a knob or a temperature, and switching a light, a buzzer or a display. A Raspberry Pi is a good board for this, because the same Python language that is taught in school can reach every pin.
What the Raspberry Pi course is for
- Learning Python with a purpose. A loop or an
ifmakes more sense when it blinks a light or opens a lock. - Getting ready for a real board. The wiring and the code in each lesson work the same on real hardware, so mistakes are made here first, where nothing can be damaged.
- Teaching a class. A teacher can set the whole course as assignments and see who has passed each lesson.
- Learning how devices talk. The lessons use the three common links between a board and its parts: I²C, SPI and 1-Wire.
- Building toward projects. A thermometer, a door lock and a tank alarm are small versions of real products.
How the course is organised
There are two courses. Raspberry Pi with Python uses the Raspberry Pi 4, the Raspberry Pi 5 and the Raspberry Pi Zero 2 W. These are small computers that run Linux, and their pins are controlled with the RPi.GPIO or gpiozero library. Raspberry Pi Pico with MicroPython uses the Pico and the Pico 2. These are microcontroller boards with no operating system, and their pins are controlled with the machine module.
Inside a course the lessons are in teaching order, and each one adds one new idea. Outputs come before inputs. Digital signals, which are only HIGH or LOW, come before analog signals, which can be any voltage. Simple parts come before parts that need a library. The last lesson of each course is a final build that joins several ideas.
Each lesson card names the board, the parts and the level. The parts and boards link to their pages in the electronic components atlas, where each one is explained in full.
How to use this page
- Choose a course. If you have never used a Raspberry Pi, start with lesson 1 of Raspberry Pi with Python.
- Press Start lesson in the Studio. The parts for the lesson are already in the tray, and the goal is shown above the breadboard.
- Wire the circuit, write the missing lines of the program and press Run to try it.
- Press ✔ Check. If the lesson is not complete, read the message, fix that one thing and check again.
- Come back to this page and take the next lesson. If you are a teacher, use Set course for a class to give every lesson to your students at once.
Key words
- GPIO
- General-purpose input and output: a pin that a program can set HIGH or LOW, or read.
- Library (in programming)
- Code written by someone else that a program can import and use, such as gpiozero.
- gpiozero
- A Python library for the pins of a Raspberry Pi, with ready-made objects such as LED and Button.
- MicroPython
- A small version of the Python language made to run on microcontrollers.
- Pull-up resistor
- A resistor that joins an input pin to the supply voltage, so the pin reads HIGH when nothing else drives it.
- Pulse-width modulation
- Switching a pin on and off quickly so that the fraction of time it is on sets the average power (PWM).
- Analog-to-digital converter
- A circuit that measures a voltage and gives it as a whole number (ADC).
- Physical computing
- Writing programs that sense and change the real world through sensors, lights, motors and other parts.
- I²C
- Inter-Integrated Circuit: a link that joins a board to many chips with two wires, a data wire (SDA) and a clock wire (SCL). Each chip has its own address.
- SPI
- Serial Peripheral Interface: a link in which a controller sends a clock and exchanges data one bit at a time with a chip it has selected.
- 1-Wire
- A link that carries data to and from a sensor on a single wire, which needs a pull-up resistor.
See every key word in the glossary
Where to start
Take these in order. The first three are enough to make your first working circuit.
- Blink an LED in Python: lesson 1: switch a pin on and off.
- Button and LED in Python: lesson 2: read an input.
- Button and LED with gpiozero: lesson 3: the same job with a newer library.
- Raspberry Pi 4 Model B: read about the board the course starts on.
- Blink an LED in MicroPython: the first lesson of the Pico course.
For teachers: set a course for your class
You can give a whole course to a class in one step. Sign in, open one of your classes and press Set a whole course. Each lesson becomes one assignment, in teaching order. Students join with a class code and a nickname: they need no email and no password. Your table shows who has started, who has passed the ✔ Check and who has handed in, and you can open a copy of any student's circuit and code.
Parts for a real board
The simulator needs no hardware. To build the same lessons on a desk, these are the parts each course uses. Follow a link to see how a part works and how to connect it.
- Raspberry Pi with Python: Raspberry Pi 4 Model B, Raspberry Pi 5, Raspberry Pi Zero 2 W; LED, push button, piezo buzzer, LCD 16×2 with I²C backpack, analog-to-digital converter, potentiometer, digital temperature sensor, OLED display, RFID reader, stepper motor, ultrasonic distance sensor, float switch; a breadboard, jumper wires and resistors (220 Ω or 330 Ω for each LED, 4.7 kΩ for the DS18B20).
- Raspberry Pi Pico with MicroPython: Raspberry Pi Pico, Raspberry Pi Pico 2, Raspberry Pi Pico W, Raspberry Pi Pico 2 W; LED, potentiometer, LCD 16×2 with I²C backpack, OLED display, digital temperature sensor; a breadboard, jumper wires and resistors (220 Ω or 330 Ω for each LED, 4.7 kΩ for the DS18B20).
A Raspberry Pi 4, 5 or Zero 2 W also needs a power supply and a microSD card with Raspberry Pi OS. A Pico needs only a micro USB cable. The pins of every Raspberry Pi work at 3.3 V: never connect 5 V to a GPIO pin.
Questions students and teachers ask
What is the Raspberry Pi course on Science Tinker?
The Raspberry Pi course is a free set of 18 guided lessons that run in the Science Tinker microcontroller simulator. It has two parts: 11 lessons in Python on the Raspberry Pi 4, Raspberry Pi 5 and Raspberry Pi Zero 2 W, and 7 lessons in MicroPython on the Raspberry Pi Pico and Pico 2. Each lesson is one project with a goal, starter code and a ✔ Check button that tests the wiring and the code.
Do I need a real Raspberry Pi to take the course?
No. Every lesson of the Raspberry Pi course runs in the browser, in a simulator that copies the pins, the Python libraries and the error messages of the real boards. Nothing is installed and no account is needed. If you have a real board, the same wiring and the same code work on it.
In what order should I do the Raspberry Pi lessons?
Do the lessons in the order shown, because each one uses what the lesson before it taught. Outputs come first, then inputs, then the gpiozero library, events, a display, analog input, a temperature sensor, an RFID reader and a final build with two sensors.
What is the difference between a Raspberry Pi and a Raspberry Pi Pico?
A Raspberry Pi 4, 5 or Zero 2 W is a small computer that runs the Linux operating system, and its pins are controlled from Python with a library such as gpiozero. A Raspberry Pi Pico is a microcontroller board: it has no operating system and runs one MicroPython program as soon as it gets power. The Pico has three analog inputs; the Raspberry Pi computers have none.
Should I learn RPi.GPIO or gpiozero?
Learn both, as the course does. RPi.GPIO is the older library and many books and web pages still use it, but it does not work on a Raspberry Pi 5. gpiozero works on every Raspberry Pi computer, and its LED and Button objects make programs shorter. The course starts with RPi.GPIO on the Raspberry Pi 4 and moves to gpiozero on the Raspberry Pi 5.
Why does a Raspberry Pi need an MCP3008 chip to read a knob?
The pins of a Raspberry Pi 4, 5 or Zero 2 W are digital: each one reads only HIGH or LOW. A knob (a potentiometer) gives a voltage anywhere between 0 V and 3.3 V. The MCP3008 is an analog-to-digital converter chip that measures this voltage and sends it to the Pi as a number over the SPI pins.
Can a teacher set the Raspberry Pi course for a whole class?
Yes. A teacher signs in, opens a class on the classes page and presses Set a whole course. Every lesson becomes one assignment, in order. Students join with a class code and a nickname, with no email or password, and the teacher sees who has passed each lesson.