Introduction
The Raspberry Pi is an affordable, powerful microcomputer perfect for beginners in electronics, IoT, and embedded programming. In 2026, with models like the Pi 5 and Zero 2W, it's still the top choice for projects like home automation, robots, or personal servers. This tutorial guides you step by step to install Raspberry Pi OS, configure your device, and build your first project: blinking an LED via GPIO with Python.
Why does it matter? Unlike a regular PC, the Pi has built-in GPIO pins for direct hardware interaction, like sensors or motors. You'll master the basics in 30 minutes with fully functional code. By the end, you'll have a setup ready for advanced projects like a web server or voice assistant. No fluff—just actionable, copy-paste commands.
Prerequisites
- A Raspberry Pi (Model 4, 5, or Zero recommended)
- microSD card ≥16 GB (class 10)
- USB-C power supply 5V/3A
- HDMI/micro-HDMI cable + screen (or SSH access)
- Computer with Raspberry Pi Imager (download from raspberrypi.com)
- LED, 220Ω resistor, jumper wires (for the GPIO project)
- Basic terminal knowledge (Linux/Mac/Windows with WSL)
Prepare the microSD Card
#!/bin/bash
# On Linux/Mac: replace /dev/sdX with your SD card (use lsblk or diskutil list)
# WARNING: Verify the disk to avoid erasing your data!
disk=/dev/sdX
# Unmount partitions
sudo umount ${disk}*
# Flash Raspberry Pi OS Lite (download the image first from raspberrypi.com)
# Replace 'raspios_lite.img' with your image filename
sudo dd if=raspios_lite.img of=${disk} bs=4M status=progress conv=fsync
# Sync and eject
sudo sync
sudo eject ${disk}This bash script flashes Raspberry Pi OS onto the SD card using dd. It unmounts partitions to prevent errors and uses bs=4M for faster flashing. Common pitfall: misidentifying the disk (/dev/sdX), which wipes your data—always double-check with lsblk.
First Boot and WiFi Configuration
Insert the SD card into the Pi, connect power, HDMI, and keyboard. The Pi boots in about 30 seconds. Use raspi-config to enable SSH and set up WiFi. Edit wpa_supplicant.conf for headless access (no screen needed).
Configure WiFi and SSH
# Create wpa_supplicant.conf at the root of the boot partition
cat > /boot/firmware/wpa_supplicant.conf << EOF
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="YOUR_WIFI"
psk="YOUR_PASSWORD"
}
EOF
# Enable SSH
sudo touch /boot/firmware/ssh
# Reboot
sudo rebootThis code sets up WiFi and SSH for headless booting. Place it on the boot partition before inserting the SD card. Replace SSID and PSK; country=US avoids regulatory issues. Common pitfall: forgetting sudo touch /boot/ssh blocks remote access.
Update the System
#!/bin/bash
sudo apt update
sudo apt upgrade -y
sudo apt install python3-pip python3-gpiozero git vim -y
sudo raspi-config nonint do_ssh 0 # Enable SSH if not already done
sudo rebootUpdates all packages and installs gpiozero for easy Python GPIO control (simpler than RPi.GPIO). nonint automates raspi-config. Run via SSH after boot. Common pitfall: skipping the reboot prevents libraries from loading.
Understanding GPIO
GPIO (General Purpose Input/Output) pins—40 of them on the Pi—let you connect LEDs, buttons, and sensors. Pin 17 is the LED output, pin 2 is 5V, pin 6 is GND. Use gpiozero: a simple abstraction that handles PWM and interrupts.
First Script: Blinking LED
from gpiozero import LED
from time import sleep
led = LED(17) # GPIO17
try:
while True:
led.on()
sleep(0.5)
led.off()
sleep(0.5)
except KeyboardInterrupt:
led.off()
print("LED éteinte.")This script makes an LED on GPIO17 blink every 1 second (on/off). gpiozero.LED simplifies things—no pinMode setup needed. Wire LED+ through resistor to pin 17, LED- to GND. Ctrl+C stops it cleanly. Common pitfall: reversed LED polarity means no light.
Advanced Script: LED with Button
from gpiozero import LED, Button
from signal import pause
led = LED(17)
button = Button(2, pull_up=True) # GPIO2, internal pull-up
button.when_pressed = led.on
button.when_released = led.off
print("Appuyez sur le bouton pour allumer/éteindre la LED")
pause() # Infinite loop without blockingAdds a button on GPIO2: press to turn LED on, release to turn off. pull_up=True uses internal pull-up—no external needed. pause() keeps the script running. Great for interactivity. Common pitfall: no pull-up causes floating inputs and false triggers.
Run at Boot
#!/bin/bash
# Copy script to systemd service
sudo cp blink.py /home/pi/
sudo chown pi:pi /home/pi/blink.py
cat > /etc/systemd/system/blink.service << EOF
[Unit]
Description=LED Blink
After=multi-user.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/blink.py
WorkingDirectory=/home/pi
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable blink.service
sudo systemctl start blink.serviceCreates a systemd service to run blink.py at boot. Restart=always restarts on crash. Check with systemctl status blink. Common pitfall: wrong absolute path causes silent failure.
Best Practices
- Always use resistors (220Ω) to limit LED/GPIO current (max 16mA per pin).
- Pull-up/pull-down for stable inputs, via
gpiozero. - Systemd services for autostart, not crontab.
- Logs:
journalctl -u blink.servicefor debugging. - Backup SD regularly with
ddorrsync.
Common Errors to Avoid
- GPIO as root:
gpiozerorequirespiuser; sudo breaks PWM. - Undervoltage: Pi brownouts cause freezes—use official 5V/3A supply.
- BCM vs Board pins:
gpiozerouses BCM numbering (17), not physical. - Overclock without cooling: Heat leads to throttling; check
vcgencmd measure_temp.
Next Steps
- GPIO docs: gpiozero docs
- IoT projects: Docker on Pi, Home Assistant
- Advanced hardware: DHT22 sensors, L298N motors
- Check out our Learni IoT and Raspberry Pi courses.