int ledPin = 14; // pin for LED
int pirPin = 12; // pin for PIR sensor
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirPin, INPUT); // declare PIR sensor as input
Serial.begin(115200);
}
void loop() {
val = digitalRead(pirPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn on the LED
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn off the LED
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
}