Bird
0
0
Arduinoprogramming~7 mins

PIR motion sensor in Arduino

Choose your learning style9 modes available
Introduction

A PIR motion sensor detects movement by sensing changes in infrared light. It helps your Arduino know when someone or something moves nearby.

To turn on a light automatically when someone enters a room.
To trigger an alarm if motion is detected in a restricted area.
To count people passing through a doorway.
To activate a camera when movement is sensed.
To save power by only running devices when motion is present.
Syntax
Arduino
int sensorPin = 2;  // PIR sensor output pin
int ledPin = 13;     // Built-in LED pin

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int sensorValue = digitalRead(sensorPin);
  if (sensorValue == HIGH) {
    digitalWrite(ledPin, HIGH);  // Turn LED on
    Serial.println("Motion detected!");
  } else {
    digitalWrite(ledPin, LOW);   // Turn LED off
    Serial.println("No motion.");
  }
  delay(500);
}

The PIR sensor output pin connects to a digital input pin on Arduino.

Use digitalRead() to check if motion is detected (HIGH) or not (LOW).

Examples
This example reads the PIR sensor on pin 7 and prints "Motion!" when movement is detected.
Arduino
int pirPin = 7;

void setup() {
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(pirPin) == HIGH) {
    Serial.println("Motion!");
  }
  delay(1000);
}
This example uses the PIR sensor to turn a buzzer on when motion is detected.
Arduino
int pirPin = 3;
int buzzerPin = 9;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  if (digitalRead(pirPin) == HIGH) {
    digitalWrite(buzzerPin, HIGH);  // Sound buzzer
  } else {
    digitalWrite(buzzerPin, LOW);   // Stop buzzer
  }
}
Sample Program

This program reads the PIR sensor connected to pin 2. When motion is detected, it lights up the built-in LED and prints "Motion detected!" to the serial monitor. When no motion is detected, the LED turns off and it prints "No motion." The delay slows down the readings to twice per second.

Arduino
int pirPin = 2;       // PIR sensor output pin
int ledPin = 13;      // Built-in LED pin

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int motion = digitalRead(pirPin);
  if (motion == HIGH) {
    digitalWrite(ledPin, HIGH);  // Turn LED on
    Serial.println("Motion detected!");
  } else {
    digitalWrite(ledPin, LOW);   // Turn LED off
    Serial.println("No motion.");
  }
  delay(500);
}
OutputSuccess
Important Notes

Make sure the PIR sensor has time to calibrate after powering on (usually about 30 seconds).

Use the serial monitor in Arduino IDE to see printed messages.

The built-in LED on pin 13 is useful for quick testing without extra parts.

Summary

A PIR motion sensor detects movement by sensing infrared changes.

Connect the sensor output to a digital input pin and use digitalRead() to check for motion.

Use the sensor to trigger actions like turning on lights or sounding alarms.