ESP8266 - Light Sensor
This tutorial instructs you how to use the light sensor with ESP8266. In detail:
- How a light sensor works
- How to connect a light sensor to an ESP8266
- How to program an ESP8266 to read the value from the light sensor
If you're looking for a light sensor in module form, I recommend checking out the tutorial for the ESP8266 - LDR Light Sensor Module.
Hardware Preparation
1 | × | ESP8266 NodeMCU | |
1 | × | Micro USB Cable | |
1 | × | Light Sensor | |
1 | × | 10 kΩ resistor | |
1 | × | Breadboard | |
1 | × | Jumper Wires | |
1 | × | (Optional) 5V Power Adapter for ESP8266 | |
1 | × | (Optional) ESP8266 Screw Terminal Adapter |
Or you can buy the following sensor kit:
1 | × | DIYables Sensor Kit 30 types, 69 units |
Overview of Light Sensor
The sensor used in this tutorial is a photoresistor, also known as a light-dependent resistor, LDR, or photocell.
It is not only used to detect light, but also to measure the intensity of the surrounding light.
The Light Sensor Pinout
A photoresistor has two pins which do not need to be distinguished, as it is a type of resistor and thus the pins are symmetric.

How It Works
The amount of light that the photoresistor's face is exposed to affects its resistance. By measuring this resistance, we can determine the brightness of the surrounding light.

WARNING
The value from the light sensor only gives an approximate indication of the intensity of light, not a precise measure of luminous flux. Consequently, it should not be used for applications that need a high degree of accuracy.
ESP8266 - Light Sensor
ESP8266's analog pins can be used as analog inputs. These analog input pins convert the voltage (ranging from 0 volts to VCC) into integer values (ranging from 0 to 1023), referred to as ADC value or analog value.
Connect one pin of the photoresistor to an analog input pin. Using analogRead() function, read the analog value from the pin. This will allow us to determine the light levels relatively.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
ESP8266 Code
The code below reads the value from the photocell and qualitatively determines the light level.
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 code and open it with the Arduino IDE.
- Click the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
- Open the Serial Monitor.
- Shine a light on the sensor.
- Check the Serial Monitor to view the result.
Light Sensor and LED
- When the environment is dark, the code will activate the LED, otherwise it will deactivate it.
- The wiring diagram for the code above.

This image is created using Fritzing. Click to enlarge image
Video Tutorial
Creating videos is a time-consuming task. If you find video tutorials helpful for your learning, please let us know by subscribing to our YouTube channel . If there is high demand for videos, we will work on creating them.
Challenge Yourself
- When the light in your room is dim, activate it automatically.
- Refer to ESP8266 - Relay for instructions on how to do this.