Here’s a clear, no-nonsense path to make a Raspberry Pi control and read simple electronics—safely.
Golden rules (so you don’t fry the Pi)
- GPIO is 3.3 V only. Never put 5 V on a GPIO pin.
- Per-pin current is tiny. Treat it as ≤ 8 mA (absolute max ~16 mA/pin; keep total low). Use transistors/MOSFETs/relay modules for anything more than an LED.
- Share ground. If you power a load from an external supply, connect grounds (Pi GND ↔ external GND).
- Add flyback diodes across coils (relays, solenoids, motors).
Level 1: Blink an LED (output)
Parts: LED, resistor (150–330 Ω), 1× GPIO pin, 1× GND.
Why that resistor?
Example calc: LED Vf≈2.0 V at ~10 mA →
𝑅=(3.3−2.0)/0.010=130 Ω → pick 150 Ω (or safer 220–330 Ω).
Wiring (series): GPIO → resistor → LED anode; LED cathode → GND.
Code (Python, gpiozero):
from gpiozero import LED
from time import sleep
led = LED(17) # BCM pin 17
while True:
led.on(); sleep(0.5)
led.off(); sleep(0.5)
Fade (PWM):
from gpiozero import PWMLED
from time import sleep
led = PWMLED(17)
while True:
for d in [i/100 for i in range(0,101,5)]:
led.value = d; sleep(0.05)
Level 2: Read a button (input)
Parts: Momentary button, 2 wires.
Wiring: One side → GPIO; other side → GND.
Use the Pi’s internal pull-up so idle = 1, pressed = 0.
Code:
from gpiozero import Button, LED
from signal import pause
btn = Button(22, pull_up=True)
led = LED(17)
btn.when_pressed = led.on
btn.when_released = led.off
pause()
Level 3: Drive bigger loads with a transistor or MOSFET
Use this for 5–12 V LED strips, buzzers, small pumps, etc.
NPN transistor example (e.g., 2N2222)
- GPIO → 1 kΩ → base
- Emitter → GND
- Collector → load → +Vext (5–12 V)
- Flyback diode (1N4148/1N400x) across inductive loads (stripe on diode to +Vext)
Logic-level N-MOSFET example (cleaner for >300 mA)
- Use a logic-level part (low Rds(on) at Vgs=3.3 V), e.g., AO3400, IRLZ44N, FQP30N06L (through-hole), etc.
- GPIO → 100 Ω gate resistor; 100 kΩ gate-to-GND pull-down.
- Source → GND; Drain → load → +Vext.
- Flyback diode if the load is inductive.
Code (same as LED):
from gpiozero import LED # can drive a transistor gate
load = LED(17)
load.on()
Tip: For ready-made relay modules, pick 3.3 V-logic-compatible ones or opto-isolated modules that accept a 3.3 V input.
Level 4: Servos, sensors, and smart LEDs
- Hobby servo (5 V): Use an external 5 V supply (shared GND). Control signal from a PWM-capable GPIO.
from gpiozero import Servo
s = Servo(18) # uses PWM
s.value = 0.0 # center; range -1..+1
- I²C/SPI sensors: Enable in raspi-config → Interface Options. Use 3.3 V devices or a level shifter.
- WS2812/Neopixel (5 V): Needs a level shifter (e.g., 74AHCT125/HCT14) for reliable data at 800 kHz; power LEDs from 5 V, share GND.
Quick reference pinout (40-pin models, BCM numbering)
- 3.3 V: pins 1, 17; 5 V: pins 2, 4; GND: pins 6, 9, 14, 20, 25, 30, 34, 39
- Popular GPIOs: 17, 27, 22, 23, 24, 25; PWM-friendly: 12, 13, 18, 19.
Common pitfalls (and fixes)
- Nothing happens: Wrong pin numbering—use BCM numbers consistently.
- Random resets when load switches: You powered the load from the Pi’s 5 V pin. Use a separate supply and share GND.
- GPIO pin died: You drove a motor/relay directly. Always use transistor/MOSFET + diode.
- Flickering WS2812: Add level shifter; add a 330–470 Ω series resistor on DIN; big 1000 µF cap across 5 V/GND at strip input.
Minimal shopping list
- Assorted resistors (150–330 Ω, 1 kΩ, 100 kΩ)
- LEDs, tactile buttons
- 2× NPN (2N2222) and/or logic-level MOSFETs (AO3400/FQP30N06L)
- 1N4148 or 1N4007 diodes (flyback)
- Breadboard + jumpers
- Optional: 1–4-ch relay (3.3 V-logic), level shifter (74AHCT125)
Top comments (0)