Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
đ Ready to explore and create with us? Click [here] and join today!
2.10 Detect Human Movementď
In this lesson, weâll learn how to use a Passive Infrared (PIR) sensor with the Raspberry Pi Pico 2 to detect human movement. PIR sensors are commonly used in security systems, automatic lighting, and other applications where motion detection is required. They detect infrared radiation emitted by warm objects, such as humans or animals, in their field of view.
What Youâll Need
In this project, we need the following components.
Itâs definitely convenient to buy a whole kit, hereâs the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Newton Lab Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
1 |
Circuit Diagram
When the PIR module detects someone passing by, GP14 will be high, otherwise it will be low.
Note
The PIR sensor have two potentiometers:
Sensitivity Adjustment: Controls the range of detection.
Time Delay Adjustment: Controls how long the output remains HIGH after motion is detected.
For initial testing, turn both potentiometers counterclockwise to their minimum positions. This sets the sensor to its most sensitive and shortest delay settings, allowing you to observe immediate responses.
Wiring Diagram
Writing the Code
Note
You can open the file
2.10_detect_human_movement.ino
fromnewton-lab-kit/arduino/2.10_detect_human_movement
.Or copy this code into Arduino IDE.
Select the Raspberry Pi Pico 2 board and the correct port, then click âUploadâ.
const int pirPin = 14; // PIR sensor output pin connected to GP14
int pirState = LOW; // Current state of PIR sensor
int val = 0; // Variable to store the PIR reading
void setup() {
Serial.begin(115200); // Initialize Serial Monitor
pinMode(pirPin, INPUT); // Set the PIR pin as input
Serial.println("PIR Sensor Test");
delay(2000); // Allow the PIR sensor to stabilize
}
void loop() {
val = digitalRead(pirPin); // Read the PIR sensor
if (val == HIGH) {
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
delay(500); // Wait half a second before checking again
}
When the code is running and the Serial Monitor is open:
Move in front of the PIR sensor. The Serial Monitor should display âMotion detected!â
Stop moving or move out of the sensorâs range. After a short delay, the Serial Monitor should display âMotion ended!â
Understanding the Code
Reading the PIR Sensor:
Reads the current state of the PIR sensor. It will be HIGH when motion is detected and LOW when no motion is detected.
val = digitalRead(pirPin);
Detecting Motion:
When motion is detected, and itâs the first detection, it prints âMotion detected!â and updates pirState.
When motion ends, it prints âMotion ended!â and updates pirState.
if (val == HIGH) { if (pirState == LOW) { Serial.println("Motion detected!"); pirState = HIGH; } } else { if (pirState == HIGH) { Serial.println("Motion ended!"); pirState = LOW; } }
Practical Applications
Security Systems: Detect intruders or unauthorized movement.
Automatic Lighting: Turn lights on when motion is detected.
Energy Saving: Power down devices when no movement is detected for a period.
Troubleshooting Tips
False Triggers:
PIR sensors can be sensitive to environmental factors like temperature changes or sunlight.
Avoid pointing the sensor directly at heat sources or windows.
Sensor Not Detecting Motion:
Ensure the sensor has had time to initialize (some sensors require up to 60 seconds).
Adjust the sensitivity potentiometer.
Interference:
Keep the sensor away from electronics that may cause electromagnetic interference.
Conclusion
In this lesson, youâve learned how to use a PIR sensor with the Raspberry Pi Pico to detect human movement. Youâve set up the hardware, written code to read the sensorâs output, and tested it to respond to motion. Understanding how to adjust the PIR sensorâs settings allows you to tailor it to your specific application, whether itâs for security, automation, or interactive projects.