Using a PIR sensor _ Physical Computing with Python _ Python _ Coding projects for kids and teens
Using a PIR sensor _ Physical Computing with Python _ Python _ Coding projects for kids and teens
English
Project steps
Log in or sign up
https://projects.raspberrypi.org/en/projects/physical-computing/11 1/4
12/24/24, 5:06 PM Using a PIR sensor | Physical Computing with Python | Python | Coding projects for kids and teens
levels at which it is emitted by humans. In fact, all objects at temperatures above absolute zero
(-273.15C) emit infrared radiation.
A PIR sensor detects changes in the amount of infrared radiation it receives. When there is a
significant change in the amount of infrared radiation it detects, then a pulse is triggered. This
means that a PIR sensor can detect when a human (or any animal) moves in front of it.
1. As shown above, the Vcc pin needs to be attached to a 5V pin on the Raspberry Pi.
2. The Gnd pin on the PIR sensor can be attached to any ground pin on the Raspberry Pi.
3. Lastly, the Out pin needs to be connected to any of the GPIO pins.
Tuning a PIR
Most PIR sensors have two potentiometers on them. These can control the sensitivity of the
sensors, and also the period of time for which the PIR will signal when motion is detected.
https://projects.raspberrypi.org/en/projects/physical-computing/11 2/4
12/24/24, 5:06 PM Using a PIR sensor | Physical Computing with Python | Python | Coding projects for kids and teens
In the image above, the potentiometer on the right controls the sensitivity, and the potentiometer
on the left controls the timeout. Here, both are turned fully anti-clockwise, meaning that the
sensitivity and timeout are at their lowest.
When the timeout is turned fully anti-clockwise, the PIR will output a signal for about 2.5 seconds,
whenever motion is detected. If the potentiometer is turned fully clockwise, the output signal will
last for around 250 seconds. When tuning the sensitivity, it is best to have the timeout set as low as
possible.
Detecting motion
You can detect motion with the PIR using the code below:
from gpiozero import MotionSensor
pir = MotionSensor(4)
while True:
pir.wait_for_motion()
print("You moved")
pir.wait_for_no_motion()
https://projects.raspberrypi.org/en/projects/physical-computing/11 3/4
12/24/24, 5:06 PM Using a PIR sensor | Physical Computing with Python | Python | Coding projects for kids and teens
Spotted a mistake? Enjoying the project? Any opinions on the website? Let us know!
Send feedback
https://projects.raspberrypi.org/en/projects/physical-computing/11 4/4