DEV Community

Cover image for ESP8266 - Light Sensor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

ESP8266 - Light Sensor

A photoresistor, also called an LDR, is a resistor whose resistance changes with the amount of light hitting it. Wire one up to an ESP8266 analog pin and you can read how bright or dark a room is, right from your sketch, using nothing more than analogRead().

The full tutorial, with every detail and the line-by-line code explanation, is on our site: ESP8266 - Light Sensor

What you need

Wiring

ESP8266 - Light Sensor wiring diagram

A photoresistor has two pins, and since it is just a resistor, it has no polarity, so it doesn't matter which pin goes where. Because an LDR alone can't produce a readable voltage swing on its own, you pair it with a 10 kΩ resistor to form a voltage divider. Connect the junction between the photoresistor and the resistor to the ESP8266's analog input pin, labeled A0, so the board can read the changing voltage as light levels change.

How it works

The photoresistor's resistance drops as more light hits its face and rises in darkness. The ESP8266's analog pin measures the resulting voltage at the divider junction and converts it into an integer between 0 and 1023, called the ADC value. Lower voltage swings correspond to different light levels, so by reading this number repeatedly you get a rough, relative measure of brightness rather than an absolute light reading.

Code

The sketch calls analogRead() on the sensor pin every 500 milliseconds and prints the raw value over serial using Serial.print(). It then compares that value against a handful of thresholds to label the reading qualitatively as Dark, Dim, Light, Bright, or Very bright, so you can watch it change as you cover or shine light on the sensor in the Serial Monitor.

esp8266-light-sensor.ino

/*
 * This ESP8266 NodeMCU code was developed by newbiely.com
 *
 * This ESP8266 NodeMCU code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/esp8266/esp8266-light-sensor
 */

#define LIGHT_SENSOR_PIN A0 // The ESP8266 pin ADC0

void setup() {
  // Initialize the Serial to communicate with the Serial Monitor.
  Serial.begin(9600);
}

void loop() {
  // reads the input on analog pin (value between 0 and 1023)
  int analog_value = analogRead(LIGHT_SENSOR_PIN);

  Serial.print("Analog Value = ");
  Serial.print(analog_value);   // The raw analog reading

  // We'll have a few threshholds, qualitatively determined
  if (analog_value < 40) {
    Serial.println(" => Dark");
  } else if (analog_value < 800) {
    Serial.println(" => Dim");
  } else if (analog_value < 2000) {
    Serial.println(" => Light");
  } else if (analog_value < 3200) {
    Serial.println(" => Bright");
  } else {
    Serial.println(" => Very bright");
  }

  delay(500);
}
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/esp8266/esp8266-light-sensor

Top comments (0)