// Pin definitions
int pirPin = 2; // PIR sensor pin
int ledPin = 8; // LED pin
void setup() {
// Initialize the PIR sensor pin as input
pinMode(pirPin, INPUT);
// Initialize the LED pin as output
pinMode(ledPin, OUTPUT);
// Start the serial communication
Serial.begin(9600);
}
void loop() {
// Read the value from the PIR sensor
int pirState = digitalRead(pirPin);
// If motion is detected
if (pirState == HIGH) {
// Turn on the LED
digitalWrite(ledPin, HIGH);
Serial.println("Motion detected!");
} else {
// Turn off the LED
digitalWrite(ledPin, LOW);
Serial.println("No motion.");
}
// Small delay to avoid rapid toggling
delay(500);
}