
Raspberry Pi is a small, affordable computer that is widely used in IoT, robotics, automation, and education. Python is the most commonly used language for programming Raspberry Pi because of its simplicity and extensive libraries.
What You’ll Learn
 Overview of Raspberry Pi
 Setting up Raspberry Pi
 Installing Python and Essential Libraries
 GPIO (General Purpose Input/Output) with Python
 IoT Applications with Raspberry Pi
 Using Raspberry Pi for AI and ML
 Automating Tasks with Raspberry Pi
1. What is Raspberry Pi?
🔹 A credit-card-sized computer developed by the Raspberry Pi Foundation.
🔹 Runs on Linux-based OS (Raspberry Pi OS, Ubuntu, etc.).
🔹 Has USB ports, HDMI, Wi-Fi, Bluetooth, and GPIO pins for hardware interaction.
🔹 Used for IoT, robotics, home automation, AI, and industrial applications.
2. Setting Up Raspberry Pi
2.1 Required Components
✔ Raspberry Pi Board (Raspberry Pi 4 recommended)
✔ MicroSD Card (16GB or higher)
✔ Power Adapter
✔ Monitor, Keyboard, and Mouse
✔ HDMI Cable
2.2 Installing Raspberry Pi OS
1️⃣ Download Raspberry Pi Imager from official site.
2️⃣ Insert MicroSD card into the PC and open Raspberry Pi Imager.
3️⃣ Select Raspberry Pi OS and flash it to the SD card.
4️⃣ Insert the SD card into Raspberry Pi and power it on.
3. Installing Python and Essential Libraries
3.1 Check Python Version
python3 --version
🔹 Python 3 comes pre-installed on Raspberry Pi OS.
3.2 Install Essential Libraries
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-gpiozero python3-virtualenv -y
🔹 gpiozero: Library for controlling GPIO pins
🔹 virtualenv: Creates virtual environments for projects
4. Working with Raspberry Pi GPIO using Python
4.1 What is GPIO?
General Purpose Input/Output (GPIO) pins allow Raspberry Pi to interact with sensors, LEDs, motors, and other hardware.
4.2 Controlling an LED with Python
4.2.1 Wiring the LED
✔ Connect GPIO 17 to the positive leg of the LED.
✔ Connect the negative leg to a 330-ohm resistor and then to GND.
4.2.2 Writing Python Code
from gpiozero import LED
from time import sleep
led = LED(17)  # GPIO Pin 17
while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)
This code blinks an LED every second.
5. IoT Applications with Raspberry Pi
5.1 Reading Temperature and Humidity (DHT11 Sensor)
5.1.1 Install DHT11 Library
pip install Adafruit_DHT
5.1.2 Python Code for DHT11
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 4  # Connect to GPIO 4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
    print(f'Temperature: {temperature}°C, Humidity: {humidity}%')
else:
    print("Sensor failure. Check wiring!")
This reads temperature and humidity from a DHT11 sensor.
6. Using Raspberry Pi for AI and ML
6.1 Installing TensorFlow for AI Projects
pip install tensorflow
6.2 Running a Basic AI Model on Raspberry Pi
import tensorflow as tf
# Load pre-trained model
model = tf.keras.applications.MobileNetV2(weights="imagenet")
# Print model summary
model.summary()
This loads a pre-trained MobileNetV2 AI model on Raspberry Pi.
7. Automating Tasks with Raspberry Pi
7.1 Automating a Door Lock with Servo Motor
7.1.1 Wiring the Servo Motor
✔ Connect Servo Signal Pin to GPIO 18
✔ Connect VCC to 5V and GND to GND
7.1.2 Writing Python Code
from gpiozero import Servo
from time import sleep
servo = Servo(18)  # GPIO Pin 18
while True:
    servo.min()
    print("Door Locked")
    sleep(2)
    servo.max()
    print("Door Unlocked")
    sleep(2)
This script automates a servo motor to lock and unlock a door.
8. Connecting Raspberry Pi to the Cloud
8.1 Sending Sensor Data to Firebase
8.1.1 Install Firebase Library
pip install firebase-admin
8.1.2 Python Code to Send Data
import firebase_admin
from firebase_admin import credentials, db
cred = credentials.Certificate("path/to/your/firebase.json")
firebase_admin.initialize_app(cred, {"databaseURL": "https://your-database.firebaseio.com/"})
ref = db.reference("/sensor_data")
ref.set({"temperature": 25, "humidity": 60})
print("Data sent to Firebase!")
This uploads temperature and humidity data to Firebase.
