// Motion-Activated LED Lighting (PIR)
// PIR sensor's output pin
const int pirPin = 2;
// LED pin
const int ledPin = 13;
// Initialize PIR sensor state
int pirState = LOW;
// Previous PIR sensor state
int lastPirState = LOW;
void setup() {
// Read the signal from the PIR sensor to detect motion
pinMode(pirPin, INPUT);
// send a signal to the LED to turn it on or off
pinMode(ledPin, OUTPUT);
// Initializes the serial communication at a baud rate of 9600 bits per second.
Serial.begin(9600);
}
void loop() {
// Read PIR sensor value
pirState = digitalRead(pirPin);
if (pirState == HIGH && lastPirState == LOW) {
// PIR sensor detects motion
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("Motion detected!");
} else if (pirState == LOW && lastPirState == HIGH) {
// PIR sensor no longer detects motion
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("Motion stopped.");
}
// Store current PIR state
lastPirState = pirState;
}