In this guide, we will show you how to use a PIR sensor with Raspberry Pi and write Python code to detect motion. PIR Sensors are used to detect movement and are often used in security systems. In this guide, we will use a PIR Sensor to turn an LED on when movement is detected. In this tutorial, you will learn how to set up a PIR sensor with a Raspberry Pi. You will also learn how to use the PIR sensor to detect when someone enters the room. check Also How to Setup a Raspberry Pi for the First Time | Getting Started with Raspberry Pi
- Raspberry Pi__________https://amzn.to/3wyVmj9
- PIR Sensor_________https://amzn.to/3GdU7ZW
What is a PIR sensor?
A passive infrared sensor (PIR sensor) is an electronic sensor that t is used to detect the presence of an object or person and measure it. infrared (IR) light emitted from objects in their field of view. They are most often used in PIR-based motion detectors. A PIR sensor is a sensor that detects motion. PIR sensors are commonly used in security systems and are used to detect when someone is in a room.
PIR sensors are used in a variety of applications, including security systems, automatic lighting controls, and energy-saving devices.
PIR sensor Applications
The applications of this circuit include :
- Security systems
- Automatic photographs
- Robot sensors
How does a PIR sensor work?
PIR sensors can also be used to detect the movement of objects, such as in a motion detector. PIR sensors work by detecting the infrared radiation emitted by objects. When an object is heated, it emits infrared radiation. This radiation is invisible to the human eye but can be detected by a PIR sensor. The sensor contains a special material that is sensitive to infrared radiation. When the sensor is exposed to infrared radiation, the material produces a small electrical current. This current is then used to trigger an alarm or activate a camera. PIR sensors are very sensitive, and can even detect the heat emitted by a human body.
Interface PIR Sensor With Raspberry PI
The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in academies and developing countries. and PIR sensors are a simple and useful way to add security to a home or office. What is Raspberry Pi And Their Uses – Beginner’s Guide
How to set up a PIR sensor with Raspberry Pi?
Raspberry Pi GPIO pin
The GPIO pins can be used to interface the Pi with external devices such as sensors, motors, and LEDs. They can also be used to interface the Pi with other computers and devices such as Arduino.
The pinout of the GPIO header is shown below:
The GPIO provides the following power and interface options :
- 3.3V (2 pins)
- 5V (2 pins)
- Ground (8 pins)
- General-purpose input and output
- PWM (pulse width modulation)
These allow a huge range of sensors, motors, and LEDs to be connected to the Raspberry Pi.
Schematic Diagram of PIR Sensor With Raspberry PI
Here is a circuit diagram of the PIR Sensor and Raspberry Pi :
|Wire colour||Sensor||Raspberry pi|
|② Yellow||OUT||GPIO 20|
Now, connect your Raspberry Pi GPIO PIN to the PIR motion sensor as per the connection diagram above:
Python Code Script for PIR Sensor With Raspberry PI
This program can read the sensor signal when motion detects and print it on the serial monitor.
Copy the following code and paste into Thonny IDE and save it as a Python file: Main.py
Create a new python file PIR.py and enter the following code.
To run the code, open the terminal and go to the directory where your PIR.py is located. Then enter the command ” sudo python PIR.py ” and hit enter
Getting started with Raspberry PI:- https://projects.raspberrypi.org/getting-started-with-raspberry-pi
# Source https://www.diyprojectslab.com/pir-sensor-with-raspberry-pi/
import RPi.GPIO as GPIO
GPIO.setup(20, GPIO.IN) #PIR
GPIO.setup(16, GPIO.OUT) #BUzzer
time.sleep(2) # to stabilize sensor
time.sleep(0.5) #Buzzer turns on for 0.5 sec
time.sleep(5) #to avoid multiple detection
time.sleep(0.1) #loop delay, should be less than detection delay
When run the script waits for the output pin to reach Low. It then prints a message on the screen every time the output changes. This is either when motion is detected (output changes to High) or the device sees no motion (output changes to Low).
Try modifying the reset time by rotating the “time” resistor clockwise by a few degrees. Run the script again, trigger the device, and then wait to see how long it takes to go back to the ready state.
The applications of this Project:
- Security systems
- Automatic doors
- Robot sensors
Read Similar Articles:
- Getting Started Raspberry Pi Pico – Pinout, Specs – Beginner Guide
- Interfacing PIR Motion Sensor with Raspberry Pi Pico
- Raspberry Pi Pico Home Automation System
- Interface Servo Motor With Raspberry Pi Pico
- Interface 0.96″ OLED Display with Raspberry Pi Pico
- Raspberry Pi Pico Weather Station Using Dht11 Sensor
- Interface 16*2 LCD Display With Raspberry Pi Pico