First of all we need to know what is motion sensor and how it works ?
A passive infrared sensor (PIR ) also called as motion sensor is an electronic device which senses motion using a pair of pyroelectric sensors to detect heat energy in the surrounding environment. These two sensors sit beside each other, and when the signal differential between the two sensors changes ( suppose if a person enters the room), the sensor will engage. It basically catches movement. It has three terminals namely Gnd, Vcc and signal pin with 3V regulator, time delay controller, sensitivity controller and BIS001.
PIR terminals - Gnd, Vcc and signal pin. Gnd is considered as the negative pin and is connected to the ground of the system. Vcc basically powers up the pin typically 5V. Signal pin is the output pin.
Arduino uno : Arduino Uno is a microcontroller board based on the ATmega328. It has 20 digital input/output pins (of which 6 can be used as PWM outputs and 6 can be used as analog inputs), a 16 MHz resonator, a USB connection, a power jack, an in-circuit system programming (ICSP) header, and a reset button.
Now we can start working on the circuit :
PIR connections - Connect the Gnd pin of sensor to the ground of Arduino. Vcc pin of the sensor to 5V of Arduino. And signal / output pin to digital pin 5 of Arduino board.
Led connections - Positive terminal of the led to digital pin 9 of Arduino. Negative terminal should be connected to any one leg of resistor. Another leg of the resistor should be connected to Gnd of Arduino.
Refer the circuit diagram for better understanding. Circuit diagram is also uploaded in the hardware section so that you can download.
Code :
const int led = 9; // Led positive terminal to the digital pin 9.
const int sensor = 5; //signal pin of sensor to digital pin 5.
const int state = LOW;
const int val = 0;
void setup() { // Void setup is ran only once after each powerup or reset of the Arduino board.
pinMode(led, OUTPUT); // Led is determined as an output here.
pinMode(sensor, INPUT); // PIR motion sensor is determined is an input here.
Serial.begin(9600);
}
void loop(){ // Void loop is ran over and over and consists of the main program.
val = digitalRead(sensor);
if (val == HIGH) {
digitalWrite(led, HIGH);
delay(500); // Delay of led is 500
if (state == LOW) {
Serial.println(" Motion detected");
state = HIGH;
}
}
else {
digitalWrite(led, LOW);
delay(500);
if (state == HIGH){
Serial.println("The action/ motion has stopped");
state = LOW;
}
}
}
Comments