Welcome back makers to another module about the use of the ESP32 Development board!
Today, I will show you how to interface a photoresistor, a resistor where the resistance changes based on the lighting levels, with an ESP32 to make an Automated LED light. The ESP32 is used to analyze the lighting levels, and turn on or off an LED accordingly.
Wiring the LED to the ESP32 board
Wiring the photoresistor to the ESP32 board using a voltage divider
/** * Interfacing Photoresistor using ESP32 * By TechMartian * */
/cosntants for the pins where sensors are plugged into. const int sensorPin = 2; const int ledPin = 5;
//Set up some global variables for the light level an initial value. int lightInit; // initial value int lightVal; // light reading
void setup() { // We'll set up the LED pin to be an output. pinMode(ledPin, OUTPUT); lightInit = analogRead(sensorPin); //we will take a single reading from the light sensor and store it in the lightCal //variable. This will give us a prelinary value to compare against in the loop }
void loop() {
lightVal = analogRead(sensorPin); // read the current light levels
//if lightVal is less than our initial reading withing a threshold then it is dark. if(lightVal - lightInit < 50) { digitalWrite (ledPin, HIGH); // turn on light }
//otherwise, it is bright else { digitalWrite (ledPin, LOW); // turn off light }
}