Your First Project: A Motion-Activated Light
A complete capstone project combining a PIR motion sensor, GPIO input, and an LED output — tie together nearly everything this section covered.
You'll need: a PIR (passive infrared) motion sensor module, an LED and resistor, jumper wires, a breadboard, and your Pi.
A PIR sensor is a digital-output sensor (Lesson 31's sensor categories cover this type) — it outputs HIGH when it detects motion in its field of view, and LOW otherwise, needing no ADC or special protocol, just a plain GPIO input read exactly like the button from Lesson rpi-10.
import RPi.GPIO as GPIO
import time
PIR_PIN = 27
LED_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIR_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)
print("Warming up sensor...")
time.sleep(30) # PIR sensors need a settling period after power-on
print("Ready.")
try:
while True:
if GPIO.input(PIR_PIN):
print("Motion detected -- light on")
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(8) # stay on briefly after motion stops
else:
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(0.2)
except KeyboardInterrupt:
GPIO.cleanup()The 30-second warm-up delay isn't arbitrary — most PIR modules genuinely need this settling time after power-on to calibrate against the room's baseline infrared level, and will report false triggers if read too early. Once ready, the loop reads the sensor exactly like the button lesson, but rather than reacting only to the instant of a press, it holds the LED on for a further 8 seconds after the last detected motion — a simple, readable way to avoid the light flickering off the instant you briefly stop moving.
From here, natural extensions combine lessons you've already completed: wire the LED through the relay from Lesson rpi-17 to switch a real lamp instead, add the Flask server from Lesson rpi-19 to check the light's status remotely, or set this script to launch automatically at boot using Lesson rpi-18's systemd service — a genuinely complete, deployable project built entirely from pieces this course already gave you.