ESP32 MicroPython Light Sensor

This guide will teach you how to use an LDR light sensor (also known as a photoresistor, light-dependent resistor, photocell) with an ESP32 microcontroller and MicroPython. We'll cover the following:

ESP32 MicroPython light sensor

If you are looking for a module with a light sensor, check the tutorial ESP32 MicroPython LDR Light Sensor Module.

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Light Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
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 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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Light Sensor

This guide explains how to use a photoresistor, also known as a Light-Dependent Resistor (LDR), to measure the brightness of surrounding light.

Pinout

A photoresistor has two pins. Since it is a resistor, you do not need to distinguish these pins. They are identical.

Light Sensor Pinout

How It Works

A photoresistor is a unique resistor that changes its resistance in response to light. Its resistance decreases in bright light and increases in darkness or no light. By measuring the photoresistor's resistance, we can determine the brightness or darkness of the surrounding light, making it useful for measuring light levels in different environments.

How Light Sensor Works

WARNING

The light sensor value provides a general indication of light brightness, but it's not a precise measurement. Use it when exact measurements are not required.

Wiring Diagram

How to connect ESP32 and light sensor

The wiring diagram between ESP32 MicroPython Light Sensor

This image is created using Fritzing. Click to enlarge image

ESP32 MicroPython Code

This code checks the light amount using a photocell and describes how bright it is.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-light-sensor """ from machine import ADC, Pin import time # Initialize ADC (Analog to Digital Converter) adc = ADC(Pin(36)) # The ESP32 pin GPIO36 (ADC0) connected to the light sensor # Set the ADC width (resolution) to 12 bits adc.width(ADC.WIDTH_12BIT) # Set the attenuation to 11 dB, allowing input range up to ~3.3V adc.atten(ADC.ATTN_11DB) while True: # Read the input on analog pin ADC0 (value between 0 and 4095) value = adc.read() # Read the 12-bit ADC value directly description = "" # We'll have a few thresholds, qualitatively determined if value < 41: description = "Dark" elif value < 819: description = "Dim" elif value < 2048: description = "Light" elif value < 3277: description = "Bright" else: description = "Very bright" print(f"Analog reading: {value} - {description}") time.sleep(0.5) # delay for 500 milliseconds

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the LDR light sensor to the ESP32 according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny\'s editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Direct light onto the sensor.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Analog reading: 652 - Dim Analog reading: 608 - Dim Analog reading: 748 - Dim Analog reading: 752 - Dim Analog reading: 3908 - Very bright Analog reading: 3952 - Very bright Analog reading: 3996 - Very bright
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

※ NOTE THAT:

This tutorial demonstrates how to use the adc.read() function to read values from an ADC (Analog-to-Digital Converter) connected to a light sensor. The ESP32's ADC is suitable for projects that do not require high precision. However, if your project needs accurate measurements, keep the following in mind:

  • The ESP32 ADC is not perfectly accurate and may require calibration for precise results. Each ESP32 board may vary slightly, so calibration is necessary for each individual board.
  • Calibration can be challenging, especially for beginners, and might not always yield the exact results you desire.

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

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