Here PIR sensor with Arduino code is provided which turns on a LED when human or animal body is detected near the sensor. The human and animal body emits heat energy in the form of infrared radiation. The HR-SC501 passive infrared(PIR) sensor module is a heat detection sensor that can be used to detect nearby object emitted infrared radiation. Such sensor is useful in industrial automation where infrared radiation detection is required, for alarm systems in homes and other application where infrared detection is required.
Below is picture of HR-SC501 PIR sensor.
The PIR sensor has three pins for interfacing which are the GND, Output and Power pins.
Besides these pins, it has two potentiometer to adjust the sensitivity and time delay. It also got a jumper to set the trigger type to repeat or single. The sensor has internally two halves which can detect change in infrared radiation. So it is the change of heat radiation at the two half that is responsible for detection of infrared radiation and not the continuous heat intensity of radiation. Hence the PIR sensor can be used for motion detection. When it detects object movement it turns on the data output pin high which can be read by microcontroller like ATmega328p or Arduino as illustrated here. After movement is detected, the output is high for more than 2.5 seconds
The following shows the circuit diagram of PIR Sensor with Arduino.
In the circuit diagram above, the power pin and ground pin of the PIR sensor is connected to +5V and ground pin of Arduino uno. The output data pin of the PIR sensor is connected to the pin 2 of Arduino.
The following is the PIR sensor with Arduino code for detection of motion and turning on the on board LED on Arduino Uno.
int LED = 13; // Declare the built-in LED
int pirPin = 2; // Declare the used sensor pin
void setup(){
// Set the LED pin as OUTPUT
pinMode(LED, OUTPUT);
// Set the pir pin as input and setup the internal pull-up resistor
pinMode(sensorPin, INPUT_PULLUP);
// Wait for the sensor to take a snapshot of the room
// Approximately 3 seconds
delay(3000);
}
void loop(){
// Read the sensor, if it goes high, turn on LED for 1 second
if (digitalRead(sensorPin) == HIGH){
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
}
}
In the above PIR sensor with Arduino code, we have created two alias name for pin 13 and pin 2 as LED and pirPin. In the setup() function we create we make the LED pin as output and setup the pirPin as input with internal pullup resistor. Then we wait for 3 seconds for the PIR sensor to stabilize to surrounding heat radiation. In the loop() we monitor the state of the pirPin and if it is HIGH we turn the LED on for 1 seconds and then turn it off.
Once the code is uploaded, move your finger or body near the PIR sensor and you should see the LED turned on. If the PIR does not sense the heat radiation then one can adjust the sensitivity and time delay with the potentiometer. The pir motion sensor time adjustment allows one to adjust the time delay between 0.3seconds to 5minutes. Also the sensor sensitivity can be adjusted to sense object upto 7 meter.
So here we have provided a simple PIR sensor with Arduino code to illustrate how to write program for PIR sensor with Arduino. For writing similar code for ATmega328P microcontroller see PIR Sensor with AVR Interfacing and Programming.