How to Use PIR Motion Sensor with Arduino: Simple Guide
To use a
PIR motion sensor with Arduino, connect its power and output pins to the Arduino, then read the sensor's digital output in your code using digitalRead(). When motion is detected, the sensor outputs HIGH, which you can use to trigger actions like turning on an LED.Syntax
Here is the basic syntax to read a PIR sensor connected to an Arduino digital pin:
pinMode(pin, INPUT);sets the sensor pin as input.digitalRead(pin)reads the sensor state: HIGH means motion detected, LOW means no motion.
arduino
const int pirPin = 2; // PIR sensor output pin void setup() { pinMode(pirPin, INPUT); // Set PIR pin as input Serial.begin(9600); // Start serial communication } void loop() { int motionState = digitalRead(pirPin); // Read PIR sensor if (motionState == HIGH) { Serial.println("Motion detected!"); } else { Serial.println("No motion."); } delay(1000); // Wait 1 second before next read }
Example
This example shows how to connect a PIR sensor to Arduino pin 2 and turn on the built-in LED on pin 13 when motion is detected.
arduino
const int pirPin = 2; // PIR sensor output pin const int ledPin = 13; // Built-in LED pin void setup() { pinMode(pirPin, INPUT); // PIR sensor as input pinMode(ledPin, OUTPUT); // LED as output Serial.begin(9600); // Start serial communication } void loop() { int motionState = digitalRead(pirPin); // Read PIR sensor if (motionState == HIGH) { digitalWrite(ledPin, HIGH); // Turn LED on Serial.println("Motion detected!"); } else { digitalWrite(ledPin, LOW); // Turn LED off Serial.println("No motion."); } delay(500); // Check twice per second }
Output
Motion detected!
No motion.
Motion detected!
No motion.
...
Common Pitfalls
Common mistakes when using a PIR sensor with Arduino include:
- Not giving the sensor enough warm-up time (usually 30-60 seconds) before reading values.
- Connecting the sensor output to an analog pin and using
analogRead()instead ofdigitalRead(). - Not using the correct power supply voltage (usually 5V) or wiring the sensor incorrectly.
- Ignoring sensor noise by reading too fast without delay.
arduino
/* Wrong way: reading PIR sensor on analog pin with analogRead() */ const int pirPin = A0; void setup() { pinMode(pirPin, INPUT); Serial.begin(9600); } void loop() { int val = analogRead(pirPin); // Incorrect for PIR sensor Serial.println(val); delay(500); } /* Right way: use digital pin and digitalRead() */ const int pirPinCorrect = 2; void setup() { pinMode(pirPinCorrect, INPUT); Serial.begin(9600); } void loop() { int val = digitalRead(pirPinCorrect); // Correct Serial.println(val); delay(500); }
Quick Reference
Summary tips for using PIR motion sensor with Arduino:
| Step | Description |
|---|---|
| Connect VCC to 5V | Power the PIR sensor with 5 volts from Arduino. |
| Connect GND to GND | Connect sensor ground to Arduino ground. |
| Connect OUT to digital pin | Use a digital pin (e.g., pin 2) to read sensor output. |
| Use pinMode(pin, INPUT) | Set the sensor pin as input in setup(). |
| Use digitalRead(pin) | Read sensor state: HIGH means motion detected. |
| Add delay | Add delay to avoid noisy readings and allow sensor warm-up. |
Key Takeaways
Connect PIR sensor output to an Arduino digital input pin and power it with 5V and GND.
Use digitalRead() to detect motion: HIGH means motion detected, LOW means no motion.
Allow the PIR sensor 30-60 seconds to warm up before reading values.
Add delays between readings to reduce noise and false triggers.
Use the built-in LED or serial output to verify sensor detection in your code.