Electronics, GPIO, and Sensors
Golden Rule
Raspberry Pi GPIO is powerful, but 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 transistor, driver board, or HAT |
| Share a common ground between Pi and external circuits | Signals need 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.
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 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 is:
- 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/dashboard -> alert or action
Examples:
- temperature sensor -> time series log -> chart -> heater/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/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 bad ground |
| Values look impossible | wiring mistake, wrong pull-up, bad calibration |
| Works once then fails | hot-plugging mistakes or script leaving pins in 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.
Next Step
Once you are comfortable with the hardware side, move to 06-services-web-apps-and-apis.md to make your Pi projects visible and usable over the network.