const int pirPin = 2; // The digital pin connected to the PIR sensor's output
const int ledPin = 13; // The pin for the LED indicator (you can use the built-in LED)
// Variable to store the current state of the sensor
int sensorState = 0;
void setup() {
// Start serial communication to display results on the Serial Monitor
Serial.begin(9600);
// Set the ledPin as an OUTPUT
pinMode(ledPin, OUTPUT);
// Set the pirPin as an INPUT
pinMode(pirPin, INPUT);
Serial.println("PIR Motion Sensor Test");
Serial.println("Calibrating sensor...");
// Give the sensor time to calibrate (usually 20-60 seconds)
delay(20000);
Serial.println("Sensor ready!");
}
void loop() {
// Read the state of the PIR sensor's output pin
sensorState = digitalRead(pirPin);
// Check if the sensor has detected motion
if (sensorState == HIGH) {
// If motion is detected, turn the LED on
digitalWrite(ledPin, HIGH);
// Print a message to the Serial Monitor
Serial.println("Motion detected!");
// Wait for a moment to avoid printing too many messages
delay(1000);
} else {
// If no motion is detected, turn the LED off
digitalWrite(ledPin, LOW);
}
}