int ledPin = 13; // choose the pin for the LED
int inputPin = 7; // choose the input pin (for PIR sensor)
// we start, assuming no motion detected
int pirState = LOW; // variable for reading the pin status
int val = 0;
void setup() {
// declare LED as output
pinMode(ledPin, OUTPUT);
// declare sensor as input
pinMode(inputPin, INPUT);
// start the serial communication at 9600 bits per second
Serial.begin(9600);
}
void loop() {
// read Input from the sensor
val = digitalRead(inputPin);
// if the sensor is HIGH, >> motion is detected
if (val == HIGH) {
// turn on the LED
digitalWrite(ledPin, HIGH);
Serial.println("LED Turned on");
// check if the state has changed from LOW to HIGH
if (pirState == LOW) {
// print a message to the serial monitor
Serial.println("Motion detected!");
}
// set the state to HIGH, so we only print the message once
// Use Truth Tables if u wanna explain it more smoothly
pirState = HIGH;
Serial.println("Recorded the current State of PIR Sensor");
} else {
// if the sensor is LOW, meaning no motion is detected
digitalWrite(ledPin, LOW);
Serial.println("No Motion Detected");
// set the state to LOW
pirState = LOW;
}
}