Electronics, GPIO, and Sensors
The wiring and pin-handling habits that keep your Pi alive while you connect it to the physical world.
Golden Rule
Raspberry Pi GPIO is powerful. It is not rugged. Treat the pins carefully.
Safety Rules
| Rule | Why it matters |
|---|---|
| GPIO is 3.3V logic | Many 5V modules can damage pins |
| Use resistors with LEDs and buttons | Prevents overcurrent |
| Do not drive motors directly from GPIO | Use a transistor, driver board, or HAT |
| Share a common ground between Pi and external circuits | Signals need a common reference |
| Double-check pin numbering | BCM and board numbering are different |
BCM vs Physical Pin Numbers
| Numbering style | Example | Best use |
|---|---|---|
| BCM | GPIO17, GPIO27 | Software and most tutorials |
| Physical | Pin 11, Pin 13 | Wiring diagrams |
Pick one style mentally and stay consistent. Software examples usually use BCM numbering.
The Pi's 40-pin header lays out roughly like this (BCM in parentheses):
3V3 (1) (2) 5V
SDA (3) (4) 5V
SCL (5) (6) GND
GPIO4 (7) (8) GPIO14 (TXD)
GND (9) (10) GPIO15 (RXD)
GPIO17(11) (12) GPIO18
GPIO27(13) (14) GND
GPIO22(15) (16) GPIO23
3V3 (17) (18) GPIO24
GPIO10(19) (20) GND
GPIO9(21) (22) GPIO25
GPIO11(23) (24) GPIO8
GND (25) (26) GPIO7
ID_SD(27) (28) ID_SC
GPIO5(29) (30) GND
GPIO6(31) (32) GPIO12
GPIO13(33) (34) GND
GPIO19(35) (36) GPIO16
GPIO26(37) (38) GPIO20
GND (39) (40) GPIO21
Useful Starter Components
- LEDs and resistor pack
- momentary push buttons
- DHT22 or BME280 environmental sensor
- relay module for switching low-duty loads
- small OLED display
- ultrasonic sensor
- servo motor with separate power
Common Interfaces
| Interface | Best for |
|---|---|
| GPIO digital input/output | LEDs, buttons, relays |
| I2C | Sensors, OLED displays, real-time clocks |
| SPI | Fast displays, ADCs, some sensors |
| UART | Serial modules, GPS, microcontroller communication |
| 1-Wire | Temperature probes like DS18B20 |
Enabling Interfaces
Use raspi-config or the equivalent configuration menu when needed:
sudo raspi-config
Enable interfaces such as I2C, SPI, serial, and camera as required.
Example: Reading an I2C Sensor
A common pattern:
- enable I2C
- install tools
- confirm the device appears on the bus
- use a Python library to read it
sudo apt install -y i2c-tools
sudo i2cdetect -y 1
If the address appears, your wiring is probably good.
Sensor Project Pattern
Most useful Pi sensor projects follow this flow:
sensor -> collection script -> local storage -> API or dashboard -> alert or action
Examples:
- temperature sensor -> time series log -> chart -> heater or fan decision
- motion sensor -> event trigger -> camera snapshot -> notification
- soil moisture sensor -> threshold check -> pump relay -> schedule override
Displays and Human Interfaces
Good Display Options
| Display | Good for |
|---|---|
| Small OLED | Status text, tiny dashboards |
| HDMI monitor | Kiosk dashboards or local desktop |
| Official touchscreen | Wall panels, appliance-style UI |
| E-paper | Ultra-low-power status board |
Good Input Options
- push buttons for simple appliances
- rotary encoder for menu control
- touchscreen for kiosk experience
- phone or browser UI for richer controls
Motor and Relay Caution
Motors, solenoids, pumps, and many relays can create electrical noise or draw too much current.
Use:
- dedicated power supplies when appropriate
- flyback protection if the board does not include it
- driver boards or motor controllers
- opto-isolated relay modules when switching external loads
Troubleshooting Hardware Projects
| Symptom | Likely cause |
|---|---|
| Sensor not detected | Wrong address, wrong protocol, loose wiring |
| Random reboots | Weak power supply or electrical noise |
| Servo acts strangely | Underpowered or sharing a bad ground |
| Values look impossible | Wiring mistake, wrong pull-up, bad calibration |
| Works once then fails | Hot-plugging mistakes or script leaving pins in a bad state |
A Strong Beginner Build
Build an environmental monitor with:
- BME280 over I2C
- OLED status display
- Python collection loop
- simple web dashboard
- optional fan or relay output
That one build teaches wiring, buses, software structure, services, and UI in a single project.
Next Steps
Continue to 06-services-web-apps-and-apis.md to make your Pi projects visible and usable over the network.