// Define the pins for the motion sensor and LED
int motionSensorPin = 2; // PIR motion sensor is connected to digital pin 2
int ledPin = 13; // LED is connected to digital pin 13
// Variable to hold the motion sensor status
int sensorState = 0;
void setup() {
pinMode(motionSensorPin, INPUT); // Set the motion sensor pin as input
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Serial.begin(9600); // Initialize serial communication
}
void loop() {
sensorState = digitalRead(motionSensorPin); // Read the state of the motion sensor
if (sensorState == HIGH) { // Check if the sensor detected motion
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("Motion detected - LED on");
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("No motion - LED off");
}
delay(000); // Wait for 1 second before the next reading
}