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.
On this page
Setup #
| Task | How |
|---|---|
| Flash the SD card | Raspberry 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 #
| Command | Action |
|---|---|
| GPIO header map in the terminal | |
| CPU temperature | |
0x0 = OK; anything else = under-voltage / overheating | |
| Exact Pi model | |
| Read GPIO 17 state | |
/ dl | GPIO 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 #
| File | Use |
|---|---|
/boot/firmware/config.txt | Hardware settings (overlays, display, overclock) |
/boot/firmware/cmdline.txt | Kernel 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) #
| Function | BCM GPIO | Physical pin |
|---|---|---|
| 3.3 V power | — | 1, 17 |
| 5 V power | — | 2, 4 |
| Ground | — | 6, 9, 14, 20, 25, 30, 34, 39 |
| I²C SDA / SCL | 2 / 3 | 3 / 5 |
| UART TX / RX | 14 / 15 | 8 / 10 |
| SPI MOSI / MISO / SCLK / CE0 | 10 / 9 / 11 / 8 | 19 / 21 / 23 / 24 |
| Hardware PWM | 12, 13, 18, 19 | 32, 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.
sshLog in to another computer securely. ssh [options] [user@]host [command]
| Option | What it does |
|---|---|
-p 2222 | Port (default 22) |
-i ~/.ssh/key | Use a specific private key |
-l user | User name (same as user@host) |
-J jump@bastion | Go through a jump host |
-L 8080:localhost:80 | Local port forward: your 8080 → remote 80 |
-R 9000:localhost:3000 | Remote port forward: server 9000 → your 3000 |
-D 1080 | SOCKS proxy on local port 1080 |
-N | No remote command (just tunnels) |
-f | Go to the background after logging in |
-t | Force a terminal (for ssh host -t htop) |
-A | Forward your SSH agent (use with care) |
-X / -Y | Forward graphical apps (X11) |
-C | Compress traffic (slow connections) |
-o ServerAliveInterval=60 | Any config option on the command line |
-F file | Use another config file |
-q | Quiet |
-v / -vv / -vvv | Debug output (connection problems) |
-4 / -6 | IPv4 / IPv6 only |
In a session: Enter then ~. kills a frozen connection, ~? shows all escape keys.
raspi-configRaspberry Pi configuration tool. sudo raspi-config (menu) or sudo raspi-config nonint … (scripts)
| Usage | What it does |
|---|---|
sudo raspi-config | Menus: interfaces, localisation, boot, display, performance |
nonint do_i2c 0 | Enable I²C (1 = disable) |
nonint do_spi 0 | Enable SPI |
nonint do_ssh 0 | Enable SSH |
nonint do_vnc 0 | Enable VNC |
nonint do_serial_hw 0 | Enable the serial port hardware |
nonint do_hostname pi4 | Change the host name |
nonint do_expand_rootfs | Use the whole SD card |
nonint do_boot_behaviour B1 | Boot to console (B3 = desktop) |
aptalso: apt-getInstall and update software on Debian, Ubuntu, Mint and Raspberry Pi OS. sudo apt [options] command [packages]
| Command / option | What it does |
|---|---|
update | Refresh the package lists (do this first) |
upgrade | Upgrade installed packages |
full-upgrade | Upgrade, allowing packages to be added or removed |
install pkg… | Install (./file.deb installs a local file with dependencies) |
reinstall pkg | Reinstall |
remove pkg | Remove, keep config files |
purge pkg | Remove including config files |
autoremove | Remove dependencies that are no longer needed |
search word | Search packages |
show pkg | Package details |
list --installed / --upgradable | Installed / upgradable packages |
edit-sources | Edit the repository list |
-y | Answer "yes" to every question |
--no-install-recommends | Skip recommended extras (smaller installs) |
-s, --simulate | Show what would happen, change nothing |
--fix-broken | Repair broken dependencies (apt --fix-broken install) |
--only-upgrade pkg | Upgrade one package, don't install new ones |
-t bookworm-backports | Install from a specific release / repo |
-q | Less output |
hostnameShow or set the computer name. hostname [options]
| Option | What it does |
|---|---|
hostname | The host name |
-I | All IP addresses of this machine (Linux) |
-i | IP address the name resolves to |
-f | Full domain name (FQDN) |
-s | Short name |
sudo hostnamectl set-hostname pi4 | Change it permanently (systemd) |
shutdownalso: reboot poweroffShut down or restart the computer (needs sudo).
| Usage | What it does |
|---|---|
shutdown now | Power off now (-h / -P mean the same) |
shutdown -r now | Restart now (same as reboot) |
shutdown +10 | Power off in 10 minutes |
shutdown 22:30 | Power off at a set time |
shutdown -r +5 "Updating" | Restart in 5 minutes with a message to logged-in users |
shutdown -c | Cancel a scheduled shutdown |
reboot / poweroff | Restart / power off immediately |
pinoutShow the GPIO header of this Pi in the terminal (from gpiozero).
| Option | What it does |
|---|---|
| (none) | Board picture + pin table for the Pi you're on |
-c, --color / -m, --monochrome | With / without colours |
-r a020d3, --revision | Show a different board revision |
-x, --xyz | Open pinout.xyz in the browser |
vcgencmdQuery the Pi's firmware. vcgencmd command [args]
| Command | What it does |
|---|---|
measure_temp | CPU temperature |
get_throttled | 0x0 = fine; bit 0 = under-voltage now, 1 = frequency capped, 2 = throttled, 3 = soft temp limit; bits 16–19 = happened since boot |
measure_clock arm | CPU clock speed in Hz (core, v3d…) |
measure_volts core | Voltage |
get_mem arm / get_mem gpu | Memory split |
get_config int | All numeric config.txt settings in use |
version | Firmware version |
display_power 0 / 1 | Turn the HDMI output off / on |
commands | List every command |
catPrint files (or join them). cat [options] file…
| Option | What it does |
|---|---|
-n | Number every line |
-b | Number non-empty lines only |
-s | Squeeze multiple empty lines into one |
-A | Show invisible characters: tabs as ^I, line ends as $ |
-E | Show $ at every line end (spot trailing spaces) |
-T | Show tabs as ^I |
cat a b > ab | Join files into a new one |
cat >> notes.txt | Type text and append it (end with Ctrl + D) |
pinctrlRead and set GPIO pins directly (Raspberry Pi OS Bookworm; replaces raspi-gpio). pinctrl [get | set] …
| Usage | What it does |
|---|---|
pinctrl / pinctrl get | State of every pin |
pinctrl get 17 | State of GPIO 17 (BCM number) |
pinctrl set 17 op dh | Output, drive high |
pinctrl set 17 op dl | Output, drive low |
pinctrl set 27 ip pu | Input with pull-up (pd = pull-down, pn = none) |
pinctrl set 18 a5 | Alternate function 5 |
pinctrl funcs 18 | Which functions a pin supports |
-p | Use physical pin numbers instead of BCM |
pinctrl help | Full help |
i2cdetectScan an I²C bus for devices (enable I²C first in raspi-config). i2cdetect [options] bus
| Option | What it does |
|---|---|
-y 1 | Scan bus 1 without the "are you sure" question |
-l | List the available I²C buses |
-a | Scan all addresses, including reserved ones |
-r | Probe with "read byte" commands |
-q | Probe with "quick write" commands |
-F 1 | Show what the bus adapter supports |
Numbers in the grid are device addresses in hex (76 = BME280, 3c = SSD1306 display…).
rpicam-stillTake photos with a camera module (Bookworm; older systems: libcamera-still). rpicam-still [options] -o file
| Option | What it does |
|---|---|
-o photo.jpg | Output file |
-t 2000 | Preview time in ms before the shot (default 5000) |
--immediate | Take the picture right away |
-n, --nopreview | No preview window (SSH / headless) |
--width 1920 --height 1080 | Resolution |
-e png | Encoding: jpg, png, bmp, yuv420 |
-q 90 | JPEG quality |
--hflip / --vflip / --rotation 180 | Mirror / flip / rotate |
--timelapse 5000 -o img%04d.jpg | Timelapse: a photo every 5 s (combine with -t) |
--shutter 20000 | Shutter time in µs |
--gain 2 | Analogue gain |
--awb daylight | White balance (auto, indoor, cloudy…) |
--autofocus-mode auto | Autofocus (Camera Module 3) |
--camera 1 | Second camera (Pi 5 with two cameras) |
--list-cameras | Show connected cameras and modes |
rpicam-vidRecord or stream video (Bookworm; older systems: libcamera-vid). rpicam-vid [options] -o file
| Option | What it does |
|---|---|
-t 10000 | Record for 10 s (-t 0 = forever) |
-o video.h264 | Output file (.mp4 works with --codec libav) |
--width 1920 --height 1080 | Resolution |
--framerate 30 | Frames per second |
--codec h264 | h264, mjpeg, yuv420 or libav (mp4, mkv…) |
-b 8000000 | Bitrate in bits/s |
-n | No preview window |
--inline | Repeat stream headers (needed for streaming) |
--listen -o tcp://0.0.0.0:8888 | Stream over TCP (open in VLC: tcp/h264://pi-ip:8888) |
--segment 60000 -o vid%04d.h264 | Split into 1-minute files |
--hflip / --vflip | Mirror / flip |
Nothing on this sheet matches “”.