Trybotics Logo

Interfacing Photoresistor With ESP32

DESCRIPTION

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.

Description:

Tools and Materials
3 More Images
  • ESP32 Development Board
  • Photoresistor or Light-dependent-resistor (LDR)
  • 5mm LED
  • 100Ω Resistor
  • 10kΩ Resistor
  • 4 pieces of jumper wires
  • Breadboard

Description:

Circuitry
2 More Images

Wiring the LED to the ESP32 board

  • Wire the GND pin from the ESP32 development board to the blue rail on the breadboard to create a common ground rail.
  • Connect the flat edge of the LED (also known as the cathode) to the common ground on the Arduino.
  • Connect the rounded edge of the LED (also known as the anode) to a 100Ωresistor.
  • Then, connect the free-end of this resistor to pin D5 on the ESP32, shown as the orange wire.

Wiring the photoresistor to the ESP32 board using a voltage divider

  • Connect one of the pins of the photoresistor to a 10kΩ resistor. Then connect the same end to pin D2 on the ESP32 board, shown as the white wire.
  • Connect the free-end of the resistor to the common ground.
  • Then, connect the free-end of the photoresistor to the 3.3V pin, shown as the red pin.

Description:

/**
 * 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
  }
}

Attachments

Description:


YOU MIGHT ALSO LIKE