ESP32 S3 UNO - Light Sensor

This ESP32 S3 Uno light sensor tutorial shows you how to wire an LDR photoresistor to the ESP32 S3 Uno form board and read the light level with Arduino code. The photoresistor, also called a photocell, turns brightness into a voltage. You will see the reading and a label like "Dim" or "Very bright" on the Serial Monitor.

In this tutorial, you will:

  1. Learn how a light-dependent resistor (LDR) works
  2. Wire a photoresistor and a 10 kΩ resistor to the ESP32 S3 Uno
  3. Read the light level with analogRead() in Arduino IDE
  4. Sort the readings into dark, dim, light, bright and very bright
ESP32 S3 Uno light sensor

Hardware Preparation

1×ESP32 S3 Uno-form 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×10 kΩ Resistor
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

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 .

A bare LDR light sensor costs very little, but you must add a resistor to wire it, which makes the circuit a bit harder. If you want fewer wires, you can use an LDR light sensor module instead.

Overview of Light Sensor

Before you wire anything, it helps to know what the part really measures. This makes the readings much easier to understand later.

The sensor in this tutorial is a photoresistor. People also call it a light-dependent resistor (LDR) or a photocell. It senses how bright the light around it is.

Pinout

You do not need to worry about which leg goes where. A photoresistor is just a resistor, so its two pins are the same and you can connect it either way round.

Light Sensor Pinout

How It Works

The trick of a photoresistor is that its resistance follows the light. Once you know this, the numbers in the code make sense.

In bright light, its resistance drops very low. In the dark, its resistance goes very high. When you put it in a voltage divider with a 10 kΩ resistor, this change in resistance becomes a change in voltage. The board measures that voltage, so you can tell how bright or dark the place is.

How Light Sensor Works

WARNING

The photoresistor only shows a rough trend of the light level. It does not measure the real amount of light (lux). Use it for simple jobs like "is it dark yet?", not for exact measurements.

ESP32 S3 Uno - Light Sensor

The ESP32 S3 Uno reads the sensor through one of its analog pins. This is the part that differs most from a classic 5V Arduino, so read it carefully.

The analog headers A0 to A5 on the ESP32 S3 Uno turn a voltage between 0V and 3.3V into a number from 0 to 4095. This is a 12-bit ADC, so the range is four times bigger than the 0 to 1023 range of older Arduino boards. The board runs at 3.3V logic, and its pins are NOT 5V tolerant, so the sensor circuit must be powered from 3.3V.

Connect one leg of the photoresistor to an analog pin, and use the analogRead() function in your code to get its value. A higher number means more light.

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the A0 header and its GPIO number when you wire the light sensor.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The photoresistor and the 10 kΩ resistor form a voltage divider. Their middle point goes to the A0 header of the ESP32 S3 Uno.

The wiring diagram between ESP32 S3 Uno Light Sensor

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Light sensor pin 1 3.3V
Light sensor pin 2 A0 (GPIO2)
10 kΩ resistor, end 1 A0 (GPIO2)
10 kΩ resistor, end 2 GND

WARNING

Power the photoresistor from the 3.3V pin, not from 5V. The ESP32 S3 Uno analog pins are NOT 5V tolerant. In bright light the voltage on A0 gets close to the supply voltage, so a 5V supply can damage GPIO2.

ESP32 S3 Uno Code

The sketch reads the light sensor on A0 (GPIO2) twice per second. It prints the raw number and then a simple label, from "Dark" to "Very bright", based on a few thresholds.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-light-sensor */ #define LIGHT_SENSOR_PIN 2 // The ESP32 S3 Uno pin GPIO2 (A0) connected to the light sensor 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 A0 (GPIO2) (value between 0 and 4095) int value = analogRead(LIGHT_SENSOR_PIN); Serial.print("Analog reading: "); Serial.print(value); // the raw analog reading // We'll have a few threshholds, qualitatively determined if (value < 40) { Serial.println(" - Dark"); } else if (value < 800) { Serial.println(" - Dim"); } else if (value < 2000) { Serial.println(" - Light"); } else if (value < 3200) { Serial.println(" - Bright"); } else { Serial.println(" - Very bright"); } delay(500); }

In setup(), the code calls analogSetAttenuation(ADC_11db). By default, the ESP32 S3 Uno ADC can only measure a small voltage. With 11 dB attenuation, the analog pin can read the full range, from 0V up to about 3.3V. Without this line, the readings reach 4095 too early and the bright levels all look the same.

The thresholds (40, 800, 2000 and 3200) are the old 0 to 1023 values scaled up for the 12-bit ADC. Change them to fit the light in your room.

Detailed Instructions

Follow these steps in order:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into Arduino IDE.
  6. Upload the code by clicking the Upload button.
  7. Open the Serial Monitor and set the baud rate to 9600.
  8. Shine a light on the sensor, then cover it with your hand.
  9. Check the result on the Serial Monitor.
∞
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
Analog reading: 652 - Dim Analog reading: 608 - Dim Analog reading: 749 - Dim Analog reading: 1530 - Light Analog reading: 3828 - Very bright Analog reading: 3890 - Very bright Analog reading: 3925 - Very bright
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: Write down the readings in your room with the light on and off. Then set the thresholds in the code between those values.

Language References

These pages explain the functions used in the sketch in more detail.

FAQ

What range does analogRead() give for the light sensor on the ESP32 S3 Uno?

The ESP32 S3 Uno has a 12-bit ADC, so analogRead() returns a value from 0 to 4095. On the UNO R4 and other older Arduino boards the default range is 0 to 1023. That is why the thresholds in this code are about four times bigger.

Why does the code call analogSetAttenuation(ADC_11db)?

It sets the ESP32 S3 Uno ADC input range to about 0 to 3.3V, so the full light sensor signal maps to 0 to 4095. Without it, the range is smaller and the readings hit 4095 in normal room light. The UNO R4 does not need this line, it is only needed on the ESP32 S3 Uno. Keep the voltage on the analog pin at 3.3V or less.

Can I power the photoresistor from 5V on the ESP32 S3 Uno?

No. In bright light, the voltage on the analog pin gets close to the supply voltage. The ESP32 S3 Uno pins are not 5V tolerant, so 5V can damage the pin. Always use the 3.3V pin.

Why does the reading go down in bright light instead of up?

The photoresistor and the 10 kΩ resistor are swapped. If the photoresistor goes to GND and the resistor goes to 3.3V, the logic is inverted. Swap the two parts, or just reverse the thresholds in the code.

Can a photoresistor measure light in lux?

Not with good accuracy. Each photoresistor is a bit different, and its response is not linear. For real lux values, use a digital light sensor such as the BH1750, connected to the SDA (GPIO8) and SCL (GPIO9) pins.

What if my project needs more light sensors than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 support I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 support I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in your code. For extra photoresistors you need analog pins, so use GPIO15 or GPIO16. Keep two warnings in mind. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port shows up in Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET
Nothing prints on the Serial Monitor Wrong baud rate Set the Serial Monitor to 9600 baud
Reading is always 0 Sensor not powered or wire on the wrong pin Check the 3.3V wire and make sure the middle point goes to A0 (GPIO2)
Reading is always 4095 10 kΩ resistor not connected to GND Check the resistor wire to GND
Values jump around a lot Loose breadboard contact or flickering light Press the parts firmly into the breadboard and test under steady light
Readings reach 4095 in normal room light analogSetAttenuation line missing Keep analogSetAttenuation(ADC_11db) in setup()
Reading drops when the light gets brighter Photoresistor and resistor swapped Put the photoresistor on the 3.3V side and the resistor on the GND side
Labels do not fit your room Thresholds made for a different light level Note your own readings and change the thresholds in the code

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