Introduction
The Raspberry Pi is an affordable, powerful mini-computer perfect for beginners in electronics, IoT, and embedded programming. In 2026, with the Raspberry Pi 5 and the latest OS versions, it remains the go-to choice for projects like a home server, robot, or connected sensor. This tutorial guides you step-by-step to install Raspberry Pi OS, set up the device in headless mode (no screen needed), enable WiFi and SSH, and run your first Python program to blink an LED via GPIO.
Why does it matter? Unlike a regular PC, the Pi excels at lightweight, low-power tasks with direct GPIO access in Python. You'll save hours of debugging with these tested commands. At the end, you'll have an operational Pi ready for extensions like Home Assistant or a Kubernetes cluster. Estimated time: 30 minutes. Ready to dive into embedded systems?
Prerequisites
- A Raspberry Pi (model 4 or 5 recommended)
- 16 GB+ microSD card (class 10)
- USB-C power supply 5V/3A
- Ethernet cable (optional for testing)
- Computer with Raspberry Pi Imager
- LED + 220Ω resistor + jumper wires (for GPIO)
- Basic Linux terminal knowledge
Step 1: Flash Raspberry Pi OS
Download and use Raspberry Pi Imager to write the OS to the SD card. Select Raspberry Pi OS (64-bit), enable SSH (user: pi, password: raspberry), and configure WiFi in advanced settings. Insert the card into the Pi and plug in the power. The Pi will boot in 1-2 minutes.
Verify SSH Connection
#!/bin/bash
# Find the Pi's IP via router or nmap
nmap -sn 192.168.1.0/24 | grep Raspberry
# Connect via SSH (replace IP_PI with the found IP)
ssh pi@IP_PI
# Once connected, change the password
passwd
# Update the system
sudo apt update && sudo apt upgrade -y
sudo rebootThis script scans the network to find the Pi's IP, then starts the SSH connection. Change the default password immediately for security. The update prevents bugs; the reboot applies changes without manual interruption.
Step 2: Configure raspi-config
Once connected via SSH, use raspi-config to enable hardware interfaces like GPIO, I2C, or SPI.
Run raspi-config
#!/bin/bash
sudo raspi-config
# In the interface:
# 1 System Options > S5 Password > change
# 3 Interface Options > I2 SSH > Yes
# 3 Interface Options > I4 I2C > Yes (optional)
# 3 Interface Options > I5 SPI > Yes (optional)
# 6 Localisation Options > L4 WLAN Country
# Finish and rebootraspi-config is the official tool for configuring the Pi without a GUI. Enable SSH if not already done via Imager. Select your WiFi country to comply with radio regulations. Reboot required to apply changes.
Step 3: Install Python Dependencies
For GPIO projects, install Python 3 and RPi.GPIO or gpiozero (recommended for beginners).
Install gpiozero
#!/bin/bash
sudo apt install python3-gpiozero python3-pip -y
sudo pip3 install gpiozero
# Verify installation
python3 -c "from gpiozero import LED; print('GPIOZero OK')"
# Enable GPIO in user mode (no sudo)
sudo raspi-config nonint do_gpio 0
sudo rebootgpiozero simplifies GPIO with an intuitive API. The nonint option skips the graphical interface. After reboot, any user can control GPIO without sudo, reducing security risks.
First Script: Blink an LED
from gpiozero import LED
from time import sleep
# LED on GPIO 18 (physical pin 12)
led = LED(18)
try:
while True:
led.on()
sleep(0.5)
led.off()
sleep(0.5)
except KeyboardInterrupt:
led.off()
print("\nLED turned off.")
# Wiring: LED Anode -> 220Ω Resistor -> GPIO18
# Cathode -> GND (pin 6)This complete script blinks an LED on GPIO18 every 500ms. Use Ctrl+C to stop cleanly. gpiozero handles cleanup; always add a resistor to protect the LED (like a fuse). Test with jumper wires.
Step 4: Advanced Script with Button
Add a button to toggle the LED, learning GPIO inputs.
LED with Push Button
from gpiozero import LED, Button
from signal import pause
led = LED(18)
button = Button(2) # GPIO2, physical pin 3
# Toggle LED on each press
def toggle():
led.toggle()
button.when_pressed = toggle
print("Press the button (GPIO2). Ctrl+C to quit.")
pause()
# Button wiring: One leg -> GPIO2, other -> GND (pin 9)
# Internal pull-up enabled by defaultThe button on GPIO2 toggles the LED without polling (event-driven). pause() keeps the script running. No external pull-up needed thanks to gpiozero. Ideal for interactive prototypes.
Start on Boot
#!/bin/bash
sudo nano /etc/rc.local
# Add before 'exit 0':
python3 /home/pi/blink.py &
# Better: with systemd
sudo nano /etc/systemd/system/blink.service
# Service content:
[Unit]
Description=Blink LED
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
# Enable
sudo systemctl daemon-reload
sudo systemctl enable blink.service
sudo systemctl start blink.serviceAutostart via systemd is robust and handles crashes (Restart=always). Avoid outdated rc.local. Check with systemctl status blink.service. Perfect for always-on IoT servers.
Best Practices
- Always use resistors on LEDs/relays to avoid overheating GPIO (max 16mA per pin).
- Headless mode for energy savings: no HDMI screen.
- Regular backups:
sudo dd if=/dev/mmcblk0 of=pi-backup.img. - Security: Disable root login (
sudo passwd -l root), use SSH keys. - Moderate overclocking: Via raspi-config, but monitor temperature with
vcgencmd measure_temp.
Common Errors to Avoid
- GPIO permissions: 'Permission denied' error? Enable user GPIO in raspi-config, avoid sudo everywhere.
- IP not found: Check WiFi country code; use Ethernet for debugging.
- LED not blinking: Physical pin vs GPIO numbering (GPIO18 = pin 12). Check wiring with
gpio readall. - Boot loop: Corrupted SD card; re-flash with Imager checksum verification.
Next Steps
Master the Pi with advanced projects: DHT22 sensors, Pi camera, Docker on Pi. Check the official documentation and our Learni training courses on IoT and embedded systems. Join the r/raspberry_pi Reddit community for ideas.