Introduction
A PIR motion sensor, is a small, low power and inexpensive sensor that is used to sense objects emitting low level radiation e.g humans, cats, dogs, etc. Mostly these sensors are used in motion detecting devices e.g motion activated lamps.
PIRs or referred to as PIR, “Passive Infrared”, “Pyroelectric”, or “IR motion” sensors, are easy to interface with, that is, if it detects something the output goes High else its always LOW.
PIRs have simple circuit made up of capacitors, resistors, voltage regulator(3.3V) and a BISS0001 chip that does all the processing. For settings, the sensor has two variable resistors to set sensitivity and delay time. Also there is a retrigger setting jumper.
Top View, Fresnel Lenses
Circuitry
Parts Used
We’ll learn how to interface PIR sensor with an Arduino Uno. Here is the part list we’ll need.
– PIR Motion Sensor
– Arduino Uno
– Jumper pack
– Mini Breadboard
Wiring
– Connect PIR motion Sensor Signal pin to Arduino digital pin 7
– Connect positive lead of the LED to Arduino digital pin 6
– Connect both LED and PIR sensor ground pins to Arduino ground pin
– Connect both LED and PIR sensor VCC pins to Arduino +5V pin
Breadboard Connections
Arduino Wiring
Code
Arduino code, PIR_Motion_Sensor.ino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
/* * connections * pir -- 7 * led -- 6 * vcc -- +5v * gnd -- gnd * */ #define On 1 //led on value #define Off 0 //led off value #define pir_pin 7 //PIR sensor input pin #define led_pin 6 //led output pin //setup void setup() { //set PIR pin as input pinMode(pir_pin, INPUT); //set led pin as ouput pinMode(led_pin, OUTPUT); } //loop void loop() { if(motionDetected()){ setLed(On); }else{ setLed(Off); } delay(250); } //change led void setLed(uint8_t status){ if(status) { digitalWrite(led_pin, HIGH); //set LED on }else{ digitalWrite(led_pin, LOW); //set LED off } } //Check if there is motion uint8_t motionDetected() { //read sensor ouput value if(digitalRead(pir_pin) == 1) { return true; //if person detected return true }else { return false; //if no person detected return false } } |
How the Code works
in the macro definitions, we define all the pins, PIR connected to pin 7 and LED to pin 6 on the Arduino.
1 2 |
#define pir_pin 7 #define led_pin 6 |
Then we have two functions, ‘setLed’ and ‘motionDetected’. The ‘setLed’ method takes a parameter that defines whether to turn the On/Off the LED.
1 |
void setLed(uint8_t status) |
The ‘motionDetected’ function reads the PIR sensor output and returns a HIGH or LOW
1 |
uint8_t motionDetected() |
The two methods are then used in the loop function where we continously check if the sensor is triggered (using motionDetected() method), if there is a person the LED goes on else its off.
1 2 3 4 5 |
if(motionDetected()){ setLed(On); }else{ setLed(Off); } |
finally we give our program a delay of 250 milliseconds between sensor reads
1 |
delay(250); |
Running the code
Resources
– BISS0001 Datasheet
– Fritzing Wiring
– PIR_Motion_Sensor.ino
Feedback is closed.