Cheat Sheet Library

Raspberry Pi OS & GPIO Cheat Sheet

For Raspberry Pi OS (Bookworm, Debian-based) on Pi 3/4/5/Zero 2. For general Linux commands see the Bash and apt sheets.

  • 26 entries
  • 7 sections
  • Updated
  • Click a command to see all its options

26 entries

On this page

Setup #

TaskHow
Flash the SD cardRaspberry Pi Imager: set hostname, user, Wi-Fi and enable SSH in OS customisation
Connect (or use the IP address)
Config tool (interfaces, locale, boot, expand filesystem)
Update
Find the IP
Reboot / shut down /

Pi-Specific Commands #

CommandAction
GPIO header map in the terminal
CPU temperature
0x0 = OK; anything else = under-voltage / overheating
Exact Pi model
Read GPIO 17 state
/ dlGPIO 17 as output, drive high / low
Scan the I²C bus for devices
Take a photo (camera module)
Record 10 s of video

Important Files #

FileUse
/boot/firmware/config.txtHardware settings (overlays, display, overclock)
/boot/firmware/cmdline.txtKernel boot options
dtparam=i2c_arm=on (config.txt)Enable I²C (or use raspi-config)
dtoverlay=w1-gpio (config.txt)1-Wire sensors (DS18B20) on GPIO 4

GPIO Header (BCM numbering) #

FunctionBCM GPIOPhysical pin
3.3 V power—1, 17
5 V power—2, 4
Ground—6, 9, 14, 20, 25, 30, 34, 39
I²C SDA / SCL2 / 33 / 5
UART TX / RX14 / 158 / 10
SPI MOSI / MISO / SCLK / CE010 / 9 / 11 / 819 / 21 / 23 / 24
Hardware PWM12, 13, 18, 1932, 33, 12, 35

⚠ GPIO pins use 3.3 V logic and are not 5 V tolerant. Use a level shifter or a voltage divider for 5 V signals.

Python GPIO (gpiozero) #

python3 -m venv --system-site-packages ~/venv   # access the preinstalled gpiozero/lgpio
source ~/venv/bin/activate
from gpiozero import LED, Button, PWMLED, DistanceSensor
from signal import pause

led = LED(17)            # BCM numbering
button = Button(27)      # internal pull-up enabled by default

button.when_pressed = led.on
button.when_released = led.off

fade = PWMLED(18)
fade.pulse()             # breathing effect

pause()                  # keep the script running

On the Pi 5, the old RPi.GPIO library does not work. Use gpiozero (or lgpio).

Run a Script at Boot (systemd) #

# /etc/systemd/system/myapp.service
[Unit]
Description=My app
After=network-online.target

[Service]
User=milan
WorkingDirectory=/home/milan/myapp
ExecStart=/home/milan/venv/bin/python main.py
Restart=always

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
journalctl -u myapp -f        # logs

Tips #

  • Use a good power supply (official 27 W USB-C for the Pi 5). Many "random" crashes are under-voltage.
  • Make a backup image of the SD card once everything works.
  • Remote desktop: enable VNC in raspi-config, or use Raspberry Pi Connect.