ESP32 - Light Sensor

This tutorial instructs you how to use ESP32 with the light sensor. In detail, we will learn:

If you're in search of a module-based light sensor, I recommend taking a look at the ESP32 - LDR Light Sensor Module tutorial.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Light Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

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 in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to Light Sensor

The most wide-used light sensor is a photoresistor (also known as photocell, or light-dependent resistor, LDR).

It can be used to detect the presence ofthe light. It can also be used to measure the illuminance/brightness level of the light.

Light Sensor Pinout

A light sensor has two pins. Just like a normal resistor, We do NOT need to distinguish these pins.

Light Sensor Pinout

How Light Sensor Works

The photoresistor's resistance is in inverse proportion to the intensity of the light. The less light the photoresistor's face is exposed, the more the photoresistor's resistance is. Therefore, we can infer how bright the ambient light is by measuring the photoresistor's resistance.

How Light Sensor Works

WARNING

The value measured by photoresistor reflects the approximated tendency of the light's intensity, it does NOT represent exactly the luminous flux. Therefore, the photoresistor should not be used in an application that requires high accuracy. calibration is also required for some kind application.

ESP32 - Light Sensor

The ESP32's analog input pin converts the voltage (between 0v and ADC_VREF - default is 3.3V) into integer values (between 0 and 4095), called analog value or ADC value. By connecting an analog input pin of ESP32 to the photoresistor, we can read the analog value by using analogRead() function.

Wiring Diagram between Light Sensor and ESP32

ESP32 Light Sensor Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

The below ESP32 code reads the value from a light sensor and infers the light level

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-light-sensor */ #define LIGHT_SENSOR_PIN 36 // ESP32 pin GIOP36 (ADC0) void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); } void loop() { // reads the input on analog pin (value between 0 and 4095) int analogValue = analogRead(LIGHT_SENSOR_PIN); Serial.print("Analog Value = "); Serial.print(analogValue); // the raw analog reading // We'll have a few threshholds, qualitatively determined if (analogValue < 40) { Serial.println(" => Dark"); } else if (analogValue < 800) { Serial.println(" => Dim"); } else if (analogValue < 2000) { Serial.println(" => Light"); } else if (analogValue < 3200) { Serial.println(" => Bright"); } else { Serial.println(" => Very bright"); } delay(500); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • Open Serial Monitor on Arduino IDE
How to open serial monitor on Arduino IDE
  • Radiates light to sensor
  • See the result on Serial Monitor. It looks like the below::
COM6
Send
Analog Value = 406 => Dim Analog Value = 412 => Dim Analog Value = 465 => Dim Analog Value = 471 => Dim Analog Value = 3511 => Very bright Analog Value = 3521 => Very bright Analog Value = 3618 => Very bright
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Light Sensor and LED

Wiring Diagram

ESP32 Light Sensor LED Wiring Diagram

This image is created using Fritzing. Click to enlarge image

ESP32 Code

The below code turns ON the LED if it is dark, otherwise turns OFF the LED

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-light-sensor */ // constants won't change #define LIGHT_SENSOR_PIN 36 // ESP32 pin GPIO36 (ADC0) connected to light sensor #define LED_PIN 22 // ESP32 pin GPIO22 connected to LED #define ANALOG_THRESHOLD 500 void setup() { // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); pinMode(LED_PIN, OUTPUT); // set ESP32 pin to output mode } void loop() { int analogValue = analogRead(LIGHT_SENSOR_PIN); // read the value on analog pin if (analogValue < ANALOG_THRESHOLD) digitalWrite(LED_PIN, HIGH); // turn on LED else digitalWrite(LED_PIN, LOW); // turn off LED }

※ NOTE THAT:

This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a light sensor. The ESP32 ADC is good for projects that do NOT need high accuracy. However, for projects that need precise measurements, please note:

  • The ESP32 ADC is not perfectly accurate and might need calibration for correct results. Each ESP32 board can be a bit different, so you need to calibrate the ADC for each individual board.
  • Calibration can be difficult, especially for beginners, and might not always give the exact results you want.

For projects that need high precision, consider using an external ADC (e.g ADS1115) with the ESP32 or using an Arduino, which has a more reliable ADC. If you still want to calibrate the ESP32 ADC, refer to ESP32 ADC Calibration Driver

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES