Raspberry Pi Reference
Free reference guide: Raspberry Pi Reference
About Raspberry Pi Reference
The Raspberry Pi Reference is a structured, searchable cheat sheet for physical computing and IoT projects using the Raspberry Pi. It covers six categories — GPIO, Camera, Sensor, Communication, Config, and Python — giving hobbyists, makers, engineers, and students a single place to look up any Raspberry Pi API or configuration. Each entry includes a real Python code example, making it practical for wiring up hardware and writing control scripts without switching between documentation pages.
The GPIO section covers the fundamentals of the RPi.GPIO library: setting the pin numbering scheme (BCM vs BOARD), configuring pins as input or output with optional pull-up/pull-down resistors, reading and writing digital states, and generating PWM signals for LED dimming, servo motor control, and buzzer tones. The Camera section covers the modern picamera2 library for capturing still photos, recording H.264 video, and starting live previews. The Sensor section includes ready-to-run examples for popular sensors: DHT11/DHT22 for temperature and humidity, HC-SR04 ultrasonic for distance measurement, MCP3008 SPI ADC for reading analog sensors, BMP280 I2C barometric pressure sensor, and PIR motion detection.
The Communication section covers all major protocols available on the Raspberry Pi: I2C via smbus for communicating with sensors and displays, SPI via spidev for high-speed ADCs and SD cards, UART serial for connecting to microcontrollers and GPS modules, TCP/UDP sockets for network programming, and MQTT for IoT message brokering with paho-mqtt. The Config section covers essential system setup: raspi-config for enabling interfaces (I2C, SPI, Camera), /boot/config.txt for boot-level hardware configuration, the `pinout` command for GPIO layout reference, and `i2cdetect` for scanning connected I2C devices. The Python section covers advanced patterns including interrupt-driven GPIO with `add_event_detect`, the high-level gpiozero library, subprocess for reading system commands, and asyncio for concurrent GPIO handling.
Key Features
- Covers 6 categories: GPIO, Camera, Sensor, Communication, Config, Python
- GPIO: BCM/BOARD pin modes, input/output setup, pull-up/down resistors, PWM (duty cycle control)
- Camera: picamera2 still capture, H.264 video recording, live preview
- Sensors: DHT11/DHT22 (temp/humidity), HC-SR04 (ultrasonic distance), MCP3008 ADC, BMP280 (I2C pressure), PIR motion
- Communication: I2C (smbus), SPI (spidev), UART serial, TCP/UDP socket, MQTT (paho-mqtt)
- Config: raspi-config, /boot/config.txt, pinout command, i2cdetect for device scanning
- Python: GPIO.add_event_detect with callback, gpiozero LED/Button, subprocess, asyncio blink
- 100% client-side — no sign-up, no download, no data sent to any server
Frequently Asked Questions
What is the difference between BCM and BOARD pin numbering on Raspberry Pi?
BCM (Broadcom SOC Channel) numbering refers to the GPIO number assigned by the Broadcom chip — for example, GPIO18 is pin 12 on the physical header. BOARD numbering refers to the physical position of the pin on the 40-pin header, counting from pin 1 (3.3V) at the top left. BCM numbering is the most common in Python code and documentation. Set it with `GPIO.setmode(GPIO.BCM)`. Use the `pinout` command in the terminal to see both numbering schemes on your specific board.
How do I control LED brightness with PWM on Raspberry Pi?
Use `GPIO.PWM(pin, frequency)` to create a PWM object, then `pwm.start(duty_cycle)` to begin output. The duty cycle is a percentage (0-100) of the time the pin is HIGH. A duty cycle of 50 means the LED is on half the time, appearing at half brightness. Change it dynamically with `pwm.ChangeDutyCycle(value)` and stop with `pwm.stop()`. For servo motors, use a 50Hz frequency and vary the duty cycle between about 2.5% (0 degrees) and 12.5% (180 degrees).
How do I read temperature and humidity from a DHT22 sensor?
Install the `adafruit-circuitpython-dht` library and import `adafruit_dht` and `board`. Create a sensor object with `dht = adafruit_dht.DHT22(board.D4)` where D4 corresponds to GPIO4. Read values with `dht.temperature` (in Celsius) and `dht.humidity` (percentage). The DHT22 is more accurate than the DHT11, with ±0.5°C temperature accuracy and ±2-5% humidity accuracy. Note that readings can occasionally fail — wrap reads in a try/except to handle transient errors.
How do I enable I2C on Raspberry Pi and find connected devices?
Enable I2C via `sudo raspi-config` under Interface Options, or add `dtparam=i2c_arm=on` to `/boot/config.txt` and reboot. After enabling, run `sudo i2cdetect -y 1` to scan bus 1 (the standard bus on modern Pi models). Addresses of connected devices will appear in the grid (e.g., 0x48 for a PCF8591 ADC, 0x77 for a BMP280). Use the `smbus` library in Python to communicate: `bus = smbus.SMBus(1)` opens bus 1, then use `read_byte_data` and `write_byte_data` to exchange data.
What is MQTT and how do I use it with Raspberry Pi?
MQTT (Message Queuing Telemetry Transport) is a lightweight publish/subscribe messaging protocol designed for IoT devices. A broker (like Mosquitto) routes messages between publishers and subscribers by topic. Install the `paho-mqtt` library and connect with `client.connect("broker_address", 1883)`. Publish sensor readings with `client.publish("sensor/temperature", "25.3")`. Subscribe to receive commands with `client.subscribe("device/command")`. MQTT is ideal for connecting Raspberry Pi sensors to dashboards, home automation systems, or cloud platforms.
How does the gpiozero library differ from RPi.GPIO?
gpiozero is a higher-level library that provides device abstractions rather than raw pin control. Instead of setting pin modes and reading HIGH/LOW values, you create device objects: `led = LED(17)` and call `led.on()`, `led.off()`, or `led.blink()`. `Button(2)` provides `when_pressed` and `when_released` event callbacks. gpiozero handles the underlying GPIO calls and also supports mock devices for testing without hardware. RPi.GPIO gives lower-level control and is better for custom timing-critical applications.
How do I measure distance with an HC-SR04 ultrasonic sensor?
The HC-SR04 works by sending a short ultrasonic pulse (trigger pin) and measuring how long it takes for the echo to return (echo pin). Send a 10-microsecond HIGH pulse on the TRIG pin with `GPIO.output(TRIG, True); time.sleep(0.00001); GPIO.output(TRIG, False)`. Then measure the time the ECHO pin stays HIGH: record the start time when ECHO goes HIGH, and the end time when it goes LOW. Distance in cm = `(end - start) * 17150` (half the round-trip time × speed of sound ≈ 34300 cm/s).
How do I read an analog sensor on Raspberry Pi (which has no built-in ADC)?
Raspberry Pi GPIO pins are digital-only, so you need an external ADC (Analog-to-Digital Converter) for analog sensors. The MCP3008 is a popular 8-channel 10-bit SPI ADC. Enable SPI via raspi-config, wire the MCP3008 to the SPI pins (MOSI, MISO, SCLK, CE0), and use the `spidev` library. Call `spi.xfer2([1, (8+channel)<<4, 0])` to read a channel and extract the 10-bit value from the response bytes. Alternatively, use the `adafruit-mcp3xxx` CircuitPython library for a simpler interface.