A PIR motion sensor detects movement by sensing changes in infrared light. It helps your Arduino know when someone or something moves nearby.
PIR motion sensor in 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).
int pirPin = 7; void setup() { pinMode(pirPin, INPUT); Serial.begin(9600); } void loop() { if (digitalRead(pirPin) == HIGH) { Serial.println("Motion!"); } delay(1000); }
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 } }
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.
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); }
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.
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.
