ESP32 S3 - Light Sensor Triggers LED

This tutorial shows you how to use the ESP32 S3 together with a light sensor (LDR) to automatically control an LED based on the ambient light level in the surrounding environment. By reading the analog voltage produced by a light-dependent resistor and comparing it against a defined threshold, you will see a practical example of how analog sensor data can drive digital GPIO outputs on the ESP32 S3.

What you'll build:

  1. A circuit connecting an LDR light sensor and an LED to the ESP32 S3
  2. A sketch that continuously reads the analog value from the light sensor via the ESP32 S3 ADC
  3. A system that automatically turns the LED on when the measured light level drops below a threshold (i.e., it is dark)
  4. A foundation for building automatic night lights, daylight-controlled relays, and ambient-light monitoring projects

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Light Sensor
1×LED Kit
1×LED (red)
1×LED Module
1×Alternatively, Button and LED Kit
1×220Ω Resistor
1×10 kΩ Resistor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
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 .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

The LDR light sensor is very affordable, but it requires a resistor for wiring, which can make the setup more complex. To simplify the wiring, you can use an LDR light sensor module as an alternative.

Overview of LED and Light Sensor

The LDR light sensor and LED make an intuitive pairing for building ambient-light-controlled projects on the ESP32 S3. As light intensity decreases, the LDR resistance rises, which changes the voltage at the analog input pin; the ESP32 S3 reads this voltage via its ADC and compares it against a threshold to decide whether to switch the LED on or off through a GPIO output.

Key Specifications

We have dedicated tutorials covering each component in depth. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to the ESP32 S3, and Arduino code. Learn more about them at the following links:

Wiring Diagram

Connect the light sensor and LED to your ESP32 S3 by following the diagram below. Double-check all connections before applying power, paying particular attention to the LED polarity, the 220 ohm current-limiting resistor, and the 10 kΩ pull-down resistor used to form a voltage divider with the LDR.

The wiring diagram between ESP32 S3 Light Sensor LED

This image is created using Fritzing. Click to enlarge image

Safety Notes

Always include the 220 ohm current-limiting resistor in series with the LED to prevent drawing excessive current through the ESP32 S3 GPIO pin, which can permanently damage the microcontroller. The LDR and 10 kΩ resistor form a voltage divider that keeps the analog signal within the safe 0–3.3V input range that the ESP32 S3 ADC expects — never connect the LDR output directly to a 5V source without a divider.

Component Pin ESP32 S3 Pin
Light Sensor (LDR) One leg 3.3V
Light Sensor (LDR) Other leg A0 (and 10kΩ to GND)
LED Anode (+) D3 (via 220Ω resistor)
LED Cathode (-) GND

ESP32 S3 Code

The following code reads the analog value from the light sensor on the ESP32 S3, compares it against a predefined threshold, and switches the LED on when the measured value falls below that threshold — indicating low ambient light. The threshold can be adjusted directly in the code to calibrate the trigger point for your specific lighting environment.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-light-sensor-triggers-led */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-light-sensor */ // constants won't change #define LIGHT_SENSOR_PIN 6 // ESP32-S3 pin GPIO6 (ADC1_CH5) connected to light sensor #define LED_PIN 4 // ESP32-S3 pin GPIO4 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-S3 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 }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components following the wiring diagram shown above.
  3. Connect the ESP32 S3 board to your PC using a USB Type-C cable.
  4. Open Arduino IDE on your PC.
  5. Select the correct ESP32 S3 board and COM port from the Tools menu.
  6. Copy the above code and paste it into Arduino IDE.
  7. Click the Upload button to compile and upload the code to the ESP32 S3.
Arduino IDE Upload Code
  1. Open the Serial Monitor at 115200 baud.
  2. Shine a bright light directly onto the light sensor and observe the LED turn off.
  3. Cover the light sensor with your hand to block the light and observe the LED turn on.
  4. Check the Serial Monitor to see the live analog readings printed in real time.
  5. Pro Tip: Try adjusting the threshold value in the code to fine-tune the trigger point for your room's ambient light conditions — print the raw ADC reading to the Serial Monitor first to determine the best threshold before hardcoding it.

Serial Monitor Output

The following Serial Monitor output shows the ESP32 S3 reading analog data from the light sensor in real time. Each line displays the current ADC value and the corresponding LED state triggered by the light threshold.

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-06-16 10:22:01] Light value: 3120 | LED: OFF [2026-06-16 10:22:02] Light value: 1854 | LED: OFF [2026-06-16 10:22:03] Light value: 612 | LED: ON [2026-06-16 10:22:04] Light value: 205 | LED: ON
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application and Project Ideas

Once you can trigger an LED from a light sensor reading on the ESP32 S3, you can extend this pattern to a wide range of light-sensitive automation projects.

  1. Automatic night light: Turn on a room light or LED strip automatically when ambient light drops below a threshold at dusk or when a room is darkened.
  2. Daylight-controlled relay: Switch a relay (and therefore a mains-powered lamp or appliance) based on natural light levels instead of a manual switch.
  3. Greenhouse lighting controller: Maintain a minimum light level for plants by activating grow lights whenever the sensor detects insufficient natural light.
  4. Security lighting: Trigger bright LEDs or a camera when an area suddenly becomes dark, such as when a light is switched off in a monitored zone.
  5. Solar charge monitor: Use the light sensor to detect whether sunlight is available and route the signal to a charging control circuit.
  6. Smart curtain controller: Combine the light sensor with a servo motor or stepper motor to open or close curtains automatically based on the time of day and brightness.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

Now that you can control an LED with a light sensor on the ESP32 S3, try these progressively challenging extensions to deepen your understanding and build more capable ambient-light detection systems.

  1. Beginner: Modify the threshold value in the code and observe how it changes the light level at which the LED switches on, then print a message to the Serial Monitor confirming the active threshold.
  2. Beginner: Add a second LED wired to a different GPIO pin and make it light up at a different (higher ADC value) threshold, so you have two light zones with separate visual indicators.
  3. Intermediate: Replace the simple on/off LED behaviour with PWM brightness control so the LED gradually brightens as the environment gets darker and dims as it gets brighter.
  4. Intermediate: Add an I2C OLED screen to display the live ADC reading and LED state in a formatted readout, removing the dependency on the Serial Monitor.
  5. Advanced: Store a configurable threshold value in ESP32 S3 non-volatile storage (Preferences library) so the setting persists across power cycles without re-uploading code.
  6. Advanced: Stream the light sensor readings over WiFi to a simple web page served by the ESP32 S3, and control the detection threshold remotely from a browser slider.

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