02. Raspberry Pi Setup

02. Raspberry Pi Setup

Raspberry Pi is a single-board computer designed for educational and project purposes, providing GPIO pins suitable for IoT development. This lesson covers Raspberry Pi models, OS installation, initial configuration, and GPIO basics.


1. Raspberry Pi Model Selection

1.1 Main Model Comparison

Model CPU RAM Features Use Cases
Raspberry Pi 4B Cortex-A72 (4-core, 1.5GHz) 2/4/8GB LPDDR4 USB 3.0, Gigabit Ethernet, Dual 4K display General projects, AI inference, media center
Raspberry Pi 3B+ Cortex-A53 (4-core, 1.4GHz) 1GB LPDDR2 USB 2.0, 802.11ac Wi-Fi, Bluetooth 4.2 Basic IoT projects, learning
Raspberry Pi Zero 2 W Cortex-A53 (4-core, 1GHz) 512MB LPDDR2 Micro USB, Mini HDMI, Compact size Wearables, compact projects
Raspberry Pi Pico RP2040 (Cortex-M0+, dual-core) 264KB SRAM Microcontroller (not Linux), MicroPython Real-time control, low power projects

Selection Guidelines: - General Projects: Raspberry Pi 4B (2GB or more) - Budget/Learning: Raspberry Pi 3B+ - Size-Critical Projects: Raspberry Pi Zero 2 W - Real-Time Control: Raspberry Pi Pico (requires different development approach)

1.2 Required Peripherals

Component Purpose Recommendation
Power Supply Pi 4: 5V 3A USB-C, Pi 3: 5V 2.5A Micro USB Use official adapter
microSD Card Operating system storage 16GB or more, Class 10 or UHS-I
HDMI Cable Display output Micro HDMI (Pi 4), Standard HDMI (Pi 3)
Keyboard/Mouse Initial setup Wired/Wireless both work
Case Protective enclosure Recommend active cooling for Pi 4

2. Raspberry Pi OS Installation

2.1 Raspberry Pi Imager Installation

Download: https://www.raspberrypi.org/software/

Install Raspberry Pi Imager:

# Linux (Ubuntu/Debian)
sudo apt update
sudo apt install rpi-imager

# macOS (Homebrew)
brew install --cask raspberry-pi-imager

# Windows: Download installer from website

2.2 OS Image Writing

  1. Launch Raspberry Pi Imager
  2. Select OS: Raspberry Pi OS (64-bit) recommended
  3. "Raspberry Pi OS (64-bit)": Full desktop environment
  4. "Raspberry Pi OS Lite (64-bit)": Server (CLI only)
  5. Select Storage: Choose microSD card
  6. Advanced Options (gear icon): Configure initial settings
  7. Enable SSH: Check
  8. Set username/password: e.g., username pi, password raspberry
  9. Configure Wi-Fi: SSID and password
  10. Set locale: Timezone and keyboard layout
  11. Write: Wait 5-10 minutes for completion

2.3 First Boot

  1. Insert microSD card into Raspberry Pi
  2. Connect power
  3. Wait 1-2 minutes (first boot takes time for initialization)
  4. Desktop will launch (if desktop version)

3. Initial Configuration

3.1 SSH Connection

Find IP address:

# Method 1: On the Pi itself
hostname -I

# Method 2: From another computer on same network
# Linux/Mac
arp -a | grep raspberry

# Or use nmap
sudo nmap -sn 192.168.1.0/24

Connect via SSH:

ssh pi@192.168.1.100  # Replace with your Pi's IP

3.2 System Update

# Package list update
sudo apt update

# Package upgrade (takes 10-30 minutes on first run)
sudo apt upgrade -y

# Reboot
sudo reboot

3.3 raspi-config

raspi-config is a configuration tool for Raspberry Pi.

sudo raspi-config

Common Settings:

Menu Option Description
1 System Options S3 Password Change password
1 System Options S4 Hostname Set device name
3 Interface Options I2 SSH Enable/disable SSH
3 Interface Options I3 VNC Enable VNC (remote desktop)
3 Interface Options I4 SPI Enable SPI interface (for sensors)
3 Interface Options I5 I2C Enable I2C interface (for sensors)
5 Localisation Options L1 Locale Set language/region
5 Localisation Options L2 Timezone Set timezone
6 Advanced Options A1 Expand Filesystem Expand to full SD card space

After configuration, select Finish and reboot if prompted.

3.4 Useful System Commands

# System information
uname -a                    # Kernel version
cat /proc/cpuinfo           # CPU information
cat /proc/meminfo           # Memory information
vcgencmd measure_temp       # Current temperature

# Network information
ifconfig                    # Network interface info
iwconfig                    # Wireless interface info
ping -c 4 8.8.8.8          # Test internet connectivity

# Disk usage
df -h                       # Disk space usage
du -sh /home/pi/*          # Folder size

# Process monitoring
top                         # CPU/memory usage (real-time)
htop                        # Enhanced process viewer (install: sudo apt install htop)

4. GPIO Basics

4.1 GPIO Pin Layout

Raspberry Pi 4B/3B+ uses a 40-pin header.

3V3  (1)  (2)  5V
GPIO2 (3)  (4)  5V
GPIO3 (5)  (6)  GND
GPIO4 (7)  (8)  GPIO14 (TXD)
GND   (9)  (10) GPIO15 (RXD)
GPIO17 (11) (12) GPIO18 (PWM0)
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
GPIO0 (27) (28) GPIO1
GPIO5 (29) (30) GND
GPIO6 (31) (32) GPIO12 (PWM0)
GPIO13 (33) (34) GND
GPIO19 (35) (36) GPIO16
GPIO26 (37) (38) GPIO20
GND  (39) (40) GPIO21

Pin Types: - GPIO: Digital input/output pins - 3V3: 3.3V power (max 50mA total) - 5V: 5V power (max depends on power supply) - GND: Ground - Special Functions: I2C (GPIO2/3), SPI (GPIO9/10/11), UART (GPIO14/15), PWM (GPIO12/13/18/19)

4.2 GPIO Numbering Schemes

There are two numbering schemes:

  1. BCM (Broadcom SOC channel): Uses GPIO numbers (e.g., GPIO17, GPIO27)
  2. BOARD (Physical pin): Uses physical pin numbers (e.g., Pin 11, Pin 13)

Example: - Physical Pin 11 = GPIO17 (BCM) - Physical Pin 13 = GPIO27 (BCM)

Most Python libraries use BCM numbering by default.

Circuit: - LED anode (+) → GPIO17 (Pin 11) - LED cathode (-) → 220Ω resistor → GND (Pin 9)

import RPi.GPIO as GPIO
import time

# Pin setup
LED_PIN = 17  # GPIO17 (Physical Pin 11)

GPIO.setmode(GPIO.BCM)           # Use BCM numbering
GPIO.setup(LED_PIN, GPIO.OUT)    # Set as output

try:
    while True:
        GPIO.output(LED_PIN, GPIO.HIGH)  # LED ON
        print("LED ON")
        time.sleep(1)

        GPIO.output(LED_PIN, GPIO.LOW)   # LED OFF
        print("LED OFF")
        time.sleep(1)
except KeyboardInterrupt:
    print("\nProgram stopped by user")
finally:
    GPIO.cleanup()  # Reset GPIO state

Run:

python3 led_blink.py

5. Python Environment Setup

5.1 Python Version Check

Raspberry Pi OS includes Python 3 by default.

python3 --version  # Example output: Python 3.9.2

5.2 pip Installation

# Update pip
sudo apt install python3-pip

# Verify installation
pip3 --version

5.3 Virtual Environment Setup

Use virtual environments to avoid package conflicts.

# Install venv
sudo apt install python3-venv

# Create virtual environment
python3 -m venv ~/iot_env

# Activate
source ~/iot_env/bin/activate

# Deactivate
deactivate

5.4 Common Library Installation

# GPIO control
pip3 install RPi.GPIO
pip3 install gpiozero

# Sensor libraries
pip3 install adafruit-circuitpython-dht  # DHT11/22 temperature/humidity sensor
pip3 install adafruit-blinka              # CircuitPython compatibility layer

# Communication
pip3 install paho-mqtt    # MQTT client
pip3 install requests     # HTTP client

# Image processing
pip3 install opencv-python
pip3 install pillow

# Machine learning
pip3 install tflite-runtime  # TensorFlow Lite runtime

6. System Management

6.1 Automatic Startup Settings

Using systemd:

Create service file:

sudo nano /etc/systemd/system/iot_app.service
[Unit]
Description=IoT Application
After=network.target

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/iot_project
ExecStart=/usr/bin/python3 /home/pi/iot_project/main.py
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

Enable and start service:

sudo systemctl daemon-reload
sudo systemctl enable iot_app.service
sudo systemctl start iot_app.service

# Check status
sudo systemctl status iot_app.service

# View logs
sudo journalctl -u iot_app.service -f

6.2 Automatic Reconnection Settings

WiFi Auto-Reconnect:

Add to /etc/wpa_supplicant/wpa_supplicant.conf:

network={
    ssid="YourSSID"
    psk="YourPassword"
    id_str="home"
    priority=1
}

network={
    ssid="BackupSSID"
    psk="BackupPassword"
    id_str="backup"
    priority=2
}

Network monitoring script:

import subprocess
import time

def check_internet():
    try:
        subprocess.check_call(['ping', '-c', '1', '8.8.8.8'],
                            stdout=subprocess.DEVNULL,
                            stderr=subprocess.DEVNULL)
        return True
    except subprocess.CalledProcessError:
        return False

def restart_network():
    print("Network disconnected. Restarting...")
    subprocess.call(['sudo', 'systemctl', 'restart', 'networking'])

while True:
    if not check_internet():
        restart_network()
    time.sleep(60)  # Check every 60 seconds

7. Summary

Completed Tasks

  • Raspberry Pi Model Selection: Understand different models and selection criteria
  • OS Installation: Write Raspberry Pi OS to SD card using Raspberry Pi Imager
  • Initial Configuration: SSH connection, system update, raspi-config settings
  • GPIO Basics: Pin layout, numbering schemes (BCM/BOARD)
  • LED Blink Test: Basic GPIO output control
  • Python Environment: Virtual environment and library installation
  • System Management: Systemd service setup, automatic startup

Next Steps

Next Lesson Topic Content
03. Python GPIO Control GPIO programming with Python RPi.GPIO and gpiozero library usage, LED/button control, sensor integration
04. WiFi Networking Network programming basics Python socket programming, HTTP client, network device communication
05. BLE Connectivity Bluetooth Low Energy communication BLE protocol basics, sensor data collection via BLE

Hands-On Exercise

  1. LED Control:
  2. Connect LED to GPIO17
  3. Implement blinking pattern (0.5 second intervals)
  4. Add variable speed control

  5. Button Input:

  6. Connect button to GPIO27 (with pull-down resistor)
  7. Print message when button pressed
  8. Toggle LED on button press

  9. Temperature Monitoring:

  10. Use vcgencmd measure_temp to read CPU temperature
  11. Log to file every 5 minutes
  12. Send alert if temperature exceeds 70°C

Troubleshooting

Issue Possible Cause Solution
Cannot SSH connect Incorrect IP, SSH not enabled Check IP with hostname -I, enable SSH in raspi-config
Insufficient power warning Power supply insufficient Use official 5V 3A adapter
GPIO not working Wrong pin number, incorrect permissions Check BCM vs BOARD numbering, run with sudo
SD card wear Frequent writes Use log rotation, write to RAM disk (tmpfs)

References

to navigate between lessons