ESP8266 - Light Sensor

This tutorial instructs you how to use the light sensor with ESP8266. In detail:

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
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 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.

Light Sensor pinout

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.

How Light Sensor Works

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

The wiring diagram between ESP8266 NodeMCU and Light Sensor

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.

ESP8266 Code

The code below reads the value from the photocell and qualitatively determines the light level.

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

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 out the result on the Serial Monitor.
COM6
Send
Analog reading: 163 - Dim Analog reading: 152 - Dim Analog reading: 187 - Dim Analog reading: 188 - Dim Analog reading: 957 - Very bright Analog reading: 972 - Very bright Analog reading: 981 - Very bright
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Light Sensor and LED

  • When the environment is dark, the code will activate the LED, otherwise it will deactivate it.
/* * 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 connected to light sensor #define LED_PIN D8 // The ESP8266 pin connected to LED #define ANALOG_THRESHOLD 500 void setup() { pinMode(LED_PIN, OUTPUT); // Configure the ESP8266 pin to the output mode } void loop() { int analog_value = analogRead(LIGHT_SENSOR_PIN); // read the value on analog pin if (analog_value < ANALOG_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED }
  • The wiring diagram for the code above.
The wiring diagram between ESP8266 NodeMCU and Light Sensor LED

This image is created using Fritzing. Click to enlarge image

Video Tutorial

Challenge Yourself

  • When the light in your room is dim, activate it automatically.
  • Refer to ESP8266 - Relay for instructions on how to do this.

※ 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!