A passive infrared motion sensor (PIR sensor) is an electronic sensor that measures infrared light emitted by objects in its field of view.
PIR sensors are commonly used in security alarm and automatic lighting applications.
PIR sensors detect general movement, but give no information on who or what has moved. For this, an IR imaging sensor is required.
All objects with a temperature above absolute zero emit thermal energy in the form of electromagnetic radiation. This radiation is not usually visible to the human eye, as it radiates at infrared wavelengths, but it can be detected by the motion sensor.
The motion sensor has two slots, each made of a special infrared-sensitive material. When the sensor is inactive, both slits detect the same amount of IR, the ambient amount emitted from the room. When a warm body such as a human or animal passes by, it first intercepts one half of the motion sensor, causing a positive differential change between the two halves. When the warm body leaves the detection zone, the opposite occurs, with the sensor generating a negative differential change. These pulses of change are what is detected.
Motion sensor pins :
Now let’s see how to display motion detection on the serial monitor.
int sensor_value=0; // Variable for sensor value
void setup (){
Serial.begin(9600); // Serial monitor initialization
}
void loop (){
sensor_value=analogRead(A0); // Retrieves sensor value
if (sensor_value >100){ //if motion detected
Serial.println("Motion detected");
}
if (sensor_value <100){ //if no movement is detected
Serial.println("No Motion");
}
delay(200); // We pause between each measurement
}
Let’s see how to light a led on the motion sensor.
int sensor_value=0;
int led_value=0;
int led_pin=11;
void setup (){
pinMode(led_pin,OUTPUT); //Output led assigned
}
void loop (){
sensor_value=analogRead(A0); // Retrieves sensor value
led_value=map(sensor_value,0,1023,0,255); // Convert this value to 5V.
analogWrite(led_pin, value_led); // Assign this value to the led.
}
You can replace the LED by a bulb with a relay to light up a room when someone enters.
Now we’ll see how to switch on a buzzer when the sensor detects movement.
int buzzer_pin=3; // Your buzzer pin
int sensor_movement=A0; // Your sensor pin
void setup (){
pinMode(buzzer_pin,OUTPUT); //Output buzzer assignment
}
void loop (){
if (analogRead(sensor_movement)> 100){ // If the sensor detects movement
digitalWrite(buzzer_pin,HIGH); // Switch on buzzer
}
if (analogRead(sensor_movement)< 100){ // If the sensor detects no movement
digitalWrite(buzzer_pin,LOW); // Buzzer off
}
}