// library
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
int ledPin = 14; // pin for LED
int pirPin = 13; // pin for PIR sensor
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
#define ldrPin 34 // pin for LDR sensor
// constanta for lux
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirPin, INPUT); // declare PIR sensor as input
pinMode(ldrPin, INPUT); // declare LDR sensor as input
Serial.begin(115200);
}
void loop() {
// declare analog to variable light
int light = analogRead(ldrPin);
float voltage = light / 4095. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
val = digitalRead(pirPin); // read input value
if (val == HIGH && lux <= 100) { // check if the input is HIGH and light is <= 100 lux)
digitalWrite(ledPin, HIGH); // turn on the LED
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
delay(10000);
}
} else {
digitalWrite(ledPin, LOW); // turn off the LED
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
}