ESP8266 - LDR Module

The LDR light sensor module can sense and measure the amount of light around it. It has two outputs: one that gives a digital signal (either LOW or HIGH) and another that gives an analog signal.

This tutorial instructs you how to use an ESP8266 and an LDR light sensor module to detect and measure the light. We will cover the following:

LDR Light Sensor Module
image source: diyables.io

Later on, you can change the code so that when the LDR light sensor detects light, it can turn on an LED or a light bulb using a relay.

If you prefer a light sensor in its raw form, I suggest exploring the tutorial on the ESP8266 - Light Sensor.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×LDR Light Sensor Module
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of LDR Light Sensor Module

You can use the LDR light sensor module to detect if there is light or measure how bright it is in the area. It has a digital output pin and an analog output pin for you to choose from.

Pinout

The LDR light sensor module has four pins:

  • VCC pin: Connect it to a power source with a voltage between 3.3V and 5V.
  • GND pin: Connect it to the ground, which has 0V.
  • DO pin: This is a digital output pin. It gives a HIGH signal when it's dark and a LOW signal when it's light. You can adjust the threshold between darkness and lightness using a potentiometer that is built into the module.
  • AO pin: This is an analog output pin. The output value decreases as the light gets brighter and increases as the light gets darker.
LDR Light Sensor Module Pinout
image source: diyables.io

Furthermore, the LDR light sensor module includes two LED indicators:

  • One PWR-LED indicator shows the power status.
  • One DO-LED indicator shows the state of light on the DO pin: it turns on when there is light and turns off when it is dark.

How It Works

Regarding the DO pin:

  • The LDR light sensor module has a built-in potentiometer that allows you to adjust the sensitivity or threshold for light detection.
  • When the light intensity in the surrounding environment is higher than the set threshold (meaning it's light), the output pin of the sensor becomes LOW, and the DO-LED turns on.
  • When the light intensity in the surrounding environment is lower than the set threshold (meaning it's dark), the output pin of the sensor becomes HIGH, and the DO-LED turns off.

Regarding the AO pin:

  • The value read from the AO pin decreases as the light intensity in the surrounding environment increases (meaning it's brighter).
  • The value read from the AO pin increases as the light intensity in the surrounding environment decreases (meaning it's darker).

Note that the potentiometer does not affect the value on the AO pin.

Wiring Diagram

Since the light sensor module has two outputs, you can choose to use one or both of them, depending on what you need.

  • The wiring diagram between ESP8266 and the LDR light sensor module when using DO only.
The wiring diagram between ESP8266 NodeMCU and LDR Light Sensor Module

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

  • The wiring diagram between ESP8266 and the LDR light sensor module when using AO only.
The wiring diagram between ESP8266 NodeMCU and LDR Module

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between ESP8266 and the LDR light sensor module when using both AO an DO.
The wiring diagram between ESP8266 NodeMCU and Light Sensor Module

This image is created using Fritzing. Click to enlarge image

ESP8266 Code - Read value from DO pin

/* * 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-ldr-module */ #define DO_PIN D7 // ESP8266's pin connected to DO pin of the ldr module void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the ESP8266's pin as an input pinMode(DO_PIN, INPUT); } void loop() { int light_state = digitalRead(DO_PIN); if (light_state == HIGH) Serial.println("The light is NOT present"); else Serial.println("The light is present"); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Cover and uncover the LDR light sensor module by your hand or something
  • Check out the result on the Serial Monitor.
COM6
Send
The light is present The light is present The light is NOT present The light is NOT present The light is NOT present The light is present The light is present The light is present
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

If you observe that the LED status remains constantly on or off, regardless of the light conditions, you have the option to adjust the potentiometer. This adjustment will allow you to fine-tune the light sensitivity of the sensor.

Additionally, you have the flexibility to modify the code to activate an LED or a light when light is detected. You can even control a servo motor to enable rotation. For detailed instructions and further information, please refer to the tutorials provided at the end of this guide.

ESP8266 Code - Read value from AO pin

/* * 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-ldr-module */ #define AO_PIN A0 // ESP8266's pin connected to AO pin of the ldr module void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); } void loop() { int light_value = analogRead(AO_PIN); Serial.print("The analog value: "); Serial.println(light_value); }

Detailed Instructions

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Cover and uncover the LDR light sensor module by your hand or something
  • Check out the result on the Serial Monitor.
COM6
Send
The analog value: 145 The analog value: 146 The analog value: 146 The analog value: 572 The analog value: 678 The analog value: 945 The analog value: 956 The analog value: 1001 The analog value: 1002 The analog value: 1012 The analog value: 1013 The analog value: 645 The analog value: 546 The analog value: 346 The analog value: 172
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!