The overview, with examples running, is on the board page.

The Raspberry Pi 4, Pi 5 and Zero 2 W

A Raspberry Pi's 40-pin header, driven by your main.py in Python with gpiozero or RPi.GPIO.

What it is

Three boards with the same 40-pin header: the Raspberry Pi 4 Model B, the Raspberry Pi 5 and the Raspberry Pi Zero 2 W. On a desk each runs Linux and your Python program runs on it. Here there is no Linux: your main.py runs in a Python engine in the page (MicroPython), and its gpiozero and RPi.GPIO drive the header in the circuit. The full contract is docs/raspberry-pi.md.

Writing the program

Pick the board and the editor gives it a main.py. Run starts it, as python3 main.py does on a Pi. print() goes to the Serial Monitor, what you type there goes to input(), and Ctrl-C stops the program with KeyboardInterrupt.

from gpiozero import LED
from time import sleep

led = LED(17)
while True:
    led.toggle()
    sleep(0.5)

time.sleep(1) is one simulated second, so a blink, a debounce time or an echo pulse measures the same as on a real board.

The header

Pins are named by their BCM numbers, the numbers gpiozero uses: GPIO17 is physical pin 11. RPi.GPIO takes either, after GPIO.setmode(GPIO.BCM) or GPIO.setmode(GPIO.BOARD).

Physical pins What
1, 17 3V3, 3.3 V
2, 4 5V, from the supply
6, 9, 14, 20, 25, 30, 34, 39 GND
the other 28 GPIO 0 to 27, 3.3 V logic

GPIO 2 and 3 (pins 3 and 5) have the board's 1.8 kΩ pull-ups, so they read high with nothing on them. A pin is not 5 V tolerant: an HC-SR04's echo needs a divider, and the Circuit Doctor says so.

I2C and SPI

The header's I2C bus (SDA on GPIO 2, pin 3; SCL on GPIO 3, pin 5) is /dev/i2c-1, and SPI0 (SCLK GPIO 11, MOSI GPIO 10, MISO GPIO 9, CE0 GPIO 8, CE1 GPIO 7) is /dev/spidev0.0 and /dev/spidev0.1, as on Raspberry Pi OS with both turned on in raspi-config. smbus, smbus2 and spidev talk to the parts on them, and gpiozero's MCP3008 reads a potentiometer through the converter:

from smbus2 import SMBus

with SMBus(1) as bus:
    print(hex(bus.read_byte_data(0x76, 0xD0)))   # a BME280 answers 0x60

The bus runs at Raspberry Pi OS's default 100 kHz, on the simulated clock, so a call takes as long as its bits do. An address nobody answers raises OSError: [Errno 121] Remote I/O error, as on a Pi.

Pi 5 and RPi.GPIO

RPi.GPIO does not work on a Pi 5, here or on a real one: the first GPIO.setup raises RuntimeError: Cannot determine SOC peripheral base address. Use gpiozero, which works on all three boards.

What is not simulated

  • Linux, the desktop, the shell and sudo.
  • pip and apt: only the modules that come with the board are there.
  • Networking: no Wi-Fi, Ethernet, Bluetooth or socket.
  • The camera, the display, HDMI and audio.
  • Clock stretching on I2C (no simulated part does it), the I2C speed setting (dtparam=i2c_arm_baudrate), and SPI1 and the other buses an overlay would turn on.
  • threading. gpiozero's callbacks, blink() and PWM work without it.