Introduction
This experiment demonstrates how to use a PIR motion sensor with an Arduino to create a simple security system that detects motion.
Components Needed
- PIR Motion Sensor
- Arduino (e.g., Uno, Nano)
- LED
- 220-ohm Resistor
- Jumper wires
Circuit Setup
- Connect the PIR sensor's output to a digital input pin (e.g., D2) on the Arduino.
- Connect the LED to a digital output pin using a 220-ohm resistor in series.
The LED will light up when motion is detected.
Code for PIR Motion Sensor
Upload the following code to your Arduino to detect motion:
const int pirPin = 2;
const int ledPin = 13;
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = digitalRead(pirPin);
if (sensorValue == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED when motion is detected
Serial.println("Motion Detected!");
} else {
digitalWrite(ledPin, LOW); // Turn off LED when no motion
Serial.println("No Motion");
}
delay(500);
}
Explanation
The PIR motion sensor detects motion and sends a HIGH signal to the Arduino. This triggers the LED to light up, and a message is printed on the Serial Monitor.
Troubleshooting
- If the sensor doesn't detect motion, check the wiring and ensure the sensor is powered correctly.
- If the LED remains on or off, verify the sensor's sensitivity setting.