What is a motion sensor? How does it work? How do you use it?

Introduction

A Passive Infrared (PIR) motion sensor is an electronic sensor that measures infrared light emitted by objects within its field of view.

PIR sensors are commonly used in security alarms and automatic lighting applications.

PIR sensors detect general motion but do not provide information about who or what moved. For that, an imaging IR sensor is required.

We created a project using the PIR sensor in an anti-intrusion alarm system for your home!

How does the sensor detect motion?

All objects with a temperature above absolute zero emit thermal energy in the form of electromagnetic radiation. Normally, this radiation is not visible to the human eye because it is emitted at infrared wavelengths, but it can be detected by the motion sensor.

How does the sensor work?

The motion sensor has two slots, each made of a special material sensitive to infrared. When the sensor is inactive, both slots detect the same amount of IR, the ambient radiation emitted by the room. When a warm body like 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 area, the opposite occurs, generating a negative differential change. These change pulses are what the sensor detects.

Motion sensor pins

The pins of the motion sensor:

Vcc: Connect to the 5V pin on the Arduino board
Signal: Connect to one of the Arduino’s digital input pins
GND: Connect to ground (GND)

Detection with the serial monitor

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); // Initialize the serial monitor
}

void loop() {
  sensor_value = analogRead(A0); // Read the sensor value
  if (sensor_value > 100) { // If motion is detected
    Serial.println("Motion detected");
  }
  if (sensor_value < 100) { // If no motion is detected
    Serial.println("No motion");
  }
  delay(200); // Pause between readings
}

Light alert with motion detection

Let’s see how to turn on an LED when the sensor detects motion.

int sensor_value = 0;
int led_value = 0;
int led_pin = 11;

void setup() {
  pinMode(led_pin, OUTPUT); // Set the LED pin as output
}

void loop() {
  sensor_value = analogRead(A0); // Read the sensor value
  led_value = map(sensor_value, 0, 1023, 0, 255); // Map the sensor value to 0-255 range
  analogWrite(led_pin, led_value); // Set the LED brightness based on sensor value
}

You can replace the LED with a bulb connected through a relay to turn on a room’s light when someone enters.

Sound alert with motion detection

Now let’s see how to turn on a buzzer when the sensor detects motion.

int buzzer_pin = 3;          // Your buzzer pin
int motion_sensor_pin = A0;  // Your sensor pin

void setup() {
  pinMode(buzzer_pin, OUTPUT); // Set buzzer as output
}

void loop() {
  if (analogRead(motion_sensor_pin) > 100) { // If motion is detected
    digitalWrite(buzzer_pin, HIGH);           // Turn on buzzer
  } else {                                    // Otherwise
    digitalWrite(buzzer_pin, LOW);            // Turn off buzzer
  }
}