Introduction: PIR Motion Sensor: How to Use PIRs With Arduino & Raspberry Pi
You can read this and other amazing tutorials on ElectroPeak's official website
In this tutorial, you will learn how to use a PIR motion sensor to detect movement. At the end of this tutorial you will learn:
- How PIR motion sensors work
- How to use PIR sensors with Arduino
- How to use PIR sensors with Raspberry Pi
Step 1: How PIR Motion Sensors Work
Passive Infra Red sensors can detect movement of objects that radiate IR light (like human bodies). Therefore, using these sensors to detect human movement or occupancy in security systems is very common. Initial setup and calibration of these sensors takes about 10 to 60 seconds.
The HC-SR501’s infrared imaging sensor is an efficient, inexpensive and adjustable module for detecting motion in the environment. The small size and physical design of this module allow you to easily use it in your project. The output of PIR motion detection sensor can be connected directly to one of the Arduino (or any microcontroller) digital pins. If any motion is detected by the sensor, this pin value will be set to “1”. The two potentiometers on the board allow you to adjust the sensitivity and delay time after detecting a movement.
PIR modules have a passive infrared sensor that detects the occupancy and movement from the infrared radiated from human body. You can use this module in security systems, smart lighting systems, automation, etc. There are different PIR modules available in the market, but all of them are basically the same. They all have at least a Vcc pin, GND pin, and digital output. In some of these modules, there is a ball like a lens on the sensor that improves the viewing angle.
Step 2: Using a PIR Sensor With Arduino
You can connect PIR output to any digital pin.
There is a jumper behind this module. If you move the jumper to L position, the sensor will ‘toggle’ (change state) whenever motion is detected. This is unlikely to be of much use in a practical applications. This mode is called non-triggering or Single Triggering mode. Moving the jumper to the H position will result in the more usual sensor logic. The sensor will turn on when motion is detected and turn off a while after the last motion is detected. This sensor will reset the timer (which would otherwise turn the output off) each time motion is detected; this would be applicable, for example, for room occupancy lighting control where you don’t want the lights to blink off while the unit resets. This is called Retriggering mode. (or repeatable trigger mode). There are also two potentiometers behind this module. By changing the SENSITIVITY potentiometer, you can reduce or increase the sensitivity of the sensor (clockwise increase), and also by changing TIME potentiometer the output delay after movement detection will be changed.
You must add the library and then upload the code. If it is the first time you run an Arduino board, Just follow these steps:
Go to www.arduino.cc/en/Main/Software and download the software of your OS. Install the IDE software as instructed.
- Run the Arduino IDE and clear the text editor and copy the following code in the text editor.
- Choose the board in tools and boards, then select your Arduino Board.
- Connect the Arduino to your PC and set the COM port in tools and port.
- Press the Upload (Arrow sign) button.
- You are all set!
For proper calibration, there should not be any movement in front of the PIR sensor for up to 15 seconds (until pin 13 is turned off). After this period, the sensor has a snapshot of its viewing area and it can detect movements. When the PIR sensor detects a movement, the output will be HIGH, otherwise, it will be LOW.
Step 3: Using a PIR Sensor With Raspberry Pi
Note the circuit and use the following code:
Step 4: Example Projects
Interested in further readings? Do not miss this project: