1.10 Motion Detection
A PIR (Passive Infrared) sensor is like an electronic “heat vision” detector! It doesn’t emit anything - instead, it “watches” for changes in infrared heat patterns. When a warm object (like a person or pet) moves through its field of view, it detects the heat signature change and triggers an alert.
You’ll find PIR sensors in automatic lights, security systems, and smart home devices. They’re perfect for detecting “something moved” rather than “something is there.”
Component List
Raspberry Pi Pico W x1
MicroUSB cable x1
830 Tie-Points Breadboard x1
Jumper Wire Several
PIR Motion Sensor Module x1
Component knowledge
PIR Motion Sensor Module
How motion detection works: - No movement: PIR sees steady heat pattern → GP14 reads LOW (0V) - Motion detected: PIR detects heat change → GP14 reads HIGH (3.3V)
The PIR module has built-in processing - it only triggers when it detects a change in heat patterns, not just the presence of warm objects.
Note
The PIR module has two potentiometers: one adjusts sensitivity, the other adjusts detection distance. To make the PIR module work better, you need to turn both of them counterclockwise to the end.
Connect
Code
Note
Open the
1.10_motion_detection.inofile under the path ofUltimate-Starter-Kit-for-Pico-W\Arduino\1.Projector copy this code into Thonny, then click “Run Current Script” or simply press F5 to run it.Or copy this code into Arduino IDE.
Don’t forget to select the board(Raspberry Pi Pico) and the correct port before clicking the Upload button.
After running the code, wave your hand or walk in front of the PIR sensor. You’ll see “MOTION DETECTED!” alerts with detection counters, timestamps, and status updates. The system will also tell you when motion stops and the area is clear again.
The following is the program code:
/*
Motion Detection Alert System
Uses a PIR sensor to detect motion and provides
alert messages with detection counting.
*/
// Pin definition and constants
const int PIR_SENSOR_PIN = 14; // PIR sensor connected to pin 14
const int DETECTION_DELAY = 200; // delay between readings in milliseconds
// Variables for motion tracking
bool motionDetected = false; // current motion state
bool lastMotionState = false; // previous state for change detection
int detectionCount = 0; // total number of detections
unsigned long lastDetectionTime = 0; // timestamp of last detection
void setup() {
// Configure PIR sensor pin as input
pinMode(PIR_SENSOR_PIN, INPUT);
// Initialize serial communication
Serial.begin(115200);
// Display system startup message
showStartupMessage();
}
void loop() {
// Check for motion detection
checkMotionSensor();
// Wait before next reading
delay(DETECTION_DELAY);
}
// Function to display startup information
void showStartupMessage() {
Serial.println("=== Motion Detection System ===");
Serial.println("PIR sensor monitoring active");
Serial.println("Waiting for motion...");
Serial.println("==============================");
Serial.println();
}
// Function to monitor PIR sensor and detect motion changes
void checkMotionSensor() {
// Read current PIR sensor state
motionDetected = digitalRead(PIR_SENSOR_PIN);
// Check if motion was just detected (state change from no motion to motion)
if (motionDetected && !lastMotionState) {
// Record detection time and increment counter
lastDetectionTime = millis();
detectionCount++;
// Trigger motion alert
triggerMotionAlert();
}
// Check if motion stopped (state change from motion to no motion)
if (!motionDetected && lastMotionState) {
Serial.println("Motion stopped - area clear");
Serial.println();
}
// Update last state for next comparison
lastMotionState = motionDetected;
}
// Function to handle motion detection alert
void triggerMotionAlert() {
Serial.println(">>> MOTION DETECTED! <<<");
Serial.println("Alert: Movement in monitored area");
// Show detection statistics
Serial.print("Detection #");
Serial.println(detectionCount);
Serial.print("Time: ");
Serial.print(lastDetectionTime);
Serial.println(" ms");
Serial.println("Status: ACTIVE");
Serial.println();
}
Phenomenon