/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-light-sensor
*/
// constants won't change
// LDR Initial
#define LIGHT_SENSOR_PIN 35 // ESP32 pin GIOP35 (ADC0) connected to light sensor
#define LED_PIN 22 // ESP32 pin GIOP22 connected to LED
#define ANALOG_THRESHOLD 500
int LDR_detection () {
delay(1000);
int analogValue = analogRead(LIGHT_SENSOR_PIN); // read the value on analog pin
if (analogValue < ANALOG_THRESHOLD) {
return 4; // When the LDR detect dark
}
else {
return 5; // when the LDR detect bright
}
}
void setup() {
// LDR Setup
pinMode(LED_PIN, OUTPUT); // set ESP32 pin to output mode
}
void loop() {
if (LDR_detection() == 4) {
digitalWrite(LED_PIN, HIGH); // turn on LED
}
else if (LDR_detection() == 5) {
digitalWrite(LED_PIN, LOW); // turn off LED
}
else {
Serial.println("LDR Detection Error!!!");
}
}