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

RuleWhy it matters
GPIO is 3.3V logicMany 5V modules can damage pins
Use resistors with LEDs and buttonsPrevents overcurrent
Do not drive motors directly from GPIOUse a transistor, driver board, or HAT
Share a common ground between Pi and external circuitsSignals need a common reference
Double-check pin numberingBCM and board numbering are different

BCM vs Physical Pin Numbers

Numbering styleExampleBest use
BCMGPIO17, GPIO27Software and most tutorials
PhysicalPin 11, Pin 13Wiring 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

InterfaceBest for
GPIO digital input/outputLEDs, buttons, relays
I2CSensors, OLED displays, real-time clocks
SPIFast displays, ADCs, some sensors
UARTSerial modules, GPS, microcontroller communication
1-WireTemperature 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:

  1. enable I2C
  2. install tools
  3. confirm the device appears on the bus
  4. 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

DisplayGood for
Small OLEDStatus text, tiny dashboards
HDMI monitorKiosk dashboards or local desktop
Official touchscreenWall panels, appliance-style UI
E-paperUltra-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

SymptomLikely cause
Sensor not detectedWrong address, wrong protocol, loose wiring
Random rebootsWeak power supply or electrical noise
Servo acts strangelyUnderpowered or sharing a bad ground
Values look impossibleWiring mistake, wrong pull-up, bad calibration
Works once then failsHot-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.