Before you start
- Board and software
- Classic ESP32-WROOM DevKit / ESP32 Dev Module, Arduino IDE 2 and Arduino-ESP32 3.3.2 review baseline, not a latest-release claim. Match GPIO labels, not header positions.
- Circuit and dependencies
- LDR from 3V3 to GPIO34, 10 kΩ resistor from GPIO34 to GND. GPIO34 is input-only ADC1, without internal pulls. No 5 V or negative input. This circuit measures relative light, not lux.
- Before this lesson
- Analog Inputs
- Evidence limits
- Expected behavior describes the example, not a hardware test performed in this review. Contributor media, where present, is identified separately; no compilation, board upload or accuracy measurement is claimed.
Turn Light into Numbers
The Story
Mission 08 used a potentiometer, which is a human-controlled analog input. You turned the knob, the voltage changed, and the ESP32 printed a different number.
Mission 09 replaces your hand with a sensor. An LDR changes when light changes. That means the room itself can move the ADC value. This is the bridge from reading a test dial to building projects that react to the environment.
Explain Like I'm 12
A sensor is like a translator. Light, moisture, pressure, or position is not something the ESP32 understands directly.
The sensor turns that real-world condition into an electrical signal. For an analog sensor, that signal is usually a voltage. The ESP32 ADC reads the voltage and turns it into a number.
Your job is to learn what the number means in your real room, with your real sensor.
Watch the tutorial
Follow this guide’s hardware, supply-voltage limits, wiring and GPIO assignments when building; the video may use different pins or a different board.
Mission Goal
Read an LDR light sensor with the ESP32 ADC, observe live sensor values, characterize bright and dark readings, and use a threshold to make a simple decision.
Estimated Time
40-45 min
Difficulty
Beginner
Prerequisites
Skills You'll Learn
- Explain how analog sensors convert real-world changes into voltage
- Wire an LDR light sensor as a voltage divider
- Read live sensor values in Serial Monitor
- Characterize dark and bright readings before choosing thresholds
- Use raw analog readings to make simple decisions
Components Required
- ESP32 DevKit boardReads the changing light sensor voltage
- LDR photoresistorChanges resistance when light changes
- 10 kOhm resistorForms the voltage divider with the LDR
- BreadboardHolds the LDR and resistor
- Jumper wiresConnect 3.3 V, GND, GPIO34, and the divider point
- USB data cableUpload code and watch live values
Sensors Turn Conditions into Signals
The ESP32 cannot see light directly. It can only read electrical signals.
An LDR changes resistance when light changes. A resistor divider converts that changing resistance into a changing voltage. The ADC converts that voltage into a number.
That chain is the core idea of analog sensing: real world change, electrical change, digital number, software decision.
Engineering Explanation
For the wiring shown, the LDR is above the divider midpoint and the fixed 10 kΩ resistor is below it. Vout = V3V3 × Rfixed / (RLDR + Rfixed). A typical LDR has lower resistance in brighter light, so this arrangement produces higher midpoint voltage and usually higher ADC counts in bright light. Darkness lowers the count. Reversing the two divider parts reverses that trend.
Use raw readings to characterize your own bright/dark conditions before setting LIGHT_THRESHOLD. The value 800 matches the supplied demonstration setup; it is illustrative, not calibrated lux or a universal light boundary. This sketch configures 12-bit ADC resolution and 11 dB attenuation explicitly. Classic ESP32's documented useful range at that attenuation is approximately 150–3100 mV; a 3V3-powered divider can saturate near an endpoint. Attenuation is not overvoltage protection.
Use GPIO34 (ADC1) when adding Wi-Fi on the classic chip; its ADC2 sharing restriction is not universal to all ESP32 families. Noise can cause a single-threshold decision to chatter. Start by inspecting wiring and raw counts, then use the existing hysteresis or moving-average lesson if the application needs steadier decisions. Relative bright/dark characterization is not calibrated illuminance measurement.
Real-World Analogy
Think of a weather thermometer. The outside world changes gradually, but you still need a number before you can make a decision such as 'wear a jacket.'
An analog sensor does the same kind of translation. The LDR does not tell the ESP32 'sunny' or 'dark.' It changes an electrical value. Your code decides what that value means.
Characterizing the range is like learning a new person's handwriting. You do not guess from one mark. You look at examples, learn the range, and then read it with confidence.
Wiring Diagram
Follow these steps in order. Unplug USB before you change any wires.
-
1
Unplug the ESP32 USB cable before wiring.
-
2
Place the LDR on the breadboard.
-
3
Connect one LDR leg to ESP32 3.3 V.
-
4
Connect the other LDR leg to GPIO34.
-
5
Connect one side of the 10 kOhm resistor to the same GPIO34 row.
-
6
Connect the other side of the resistor to ESP32 GND.
-
7
Check that GPIO34 is connected to the point between the LDR and the resistor.
-
8
Plug USB back in, upload the sketch, and open Serial Monitor at 115200 baud.
GPIO Table
| Signal | ESP32 Pin | Mode | Notes |
|---|---|---|---|
| LDR divider midpoint | GPIO34 | Analog input | Reads the voltage created by the LDR and resistor divider. |
| LDR high side | 3V3 | Power | Supplies safe voltage to the divider. |
| Fixed resistor low side | GND | Ground | Completes the voltage divider. |
| Serial debug | USB | Serial | Shows raw ADC values and light/dark decisions. |
Arduino Code
Copy this into Arduino IDE, then click Upload.
constexpr uint8_t LDR_PIN = 34;
constexpr uint16_t LIGHT_THRESHOLD = 800;
void setup() {
Serial.begin(115200);
analogReadResolution(12);
analogSetPinAttenuation(LDR_PIN, ADC_11db);
delay(500);
Serial.println("Mission 09: Analog light sensor ready");
Serial.println("Cover the LDR, then shine light on it, and watch the raw values.");
}
void loop() {
const uint16_t rawLight = analogRead(LDR_PIN);
Serial.print("Light ADC: ");
Serial.print(rawLight);
if (rawLight < LIGHT_THRESHOLD) {
Serial.println(" | DARK");
} else {
Serial.println(" | BRIGHT");
}
delay(250);
}
LDR to 3V3; fixed 10 kΩ to GND; midpoint on GPIO34. With this topology bright light normally increases counts. The 800-count threshold matches the supplied demonstration setup; it is illustrative; characterize your own conditions. Core 3.3.2, 12-bit ADC and 11 dB attenuation; no extra library.
Line-by-line Explanation
- LDR_PIN is GPIO34, the ADC input connected to the midpoint of the LDR voltage divider.
- LIGHT_THRESHOLD is the cutoff value that separates bright from dark for this example.
- Serial.begin(115200) starts Serial Monitor communication so you can inspect live sensor values.
- analogRead(LDR_PIN) reads the voltage at GPIO34 and returns a raw ADC number.
- Serial.print(rawLight) shows the raw reading first because raw data is the most important debugging tool.
- The if statement compares the raw reading with LIGHT_THRESHOLD.
- With the LDR connected to 3V3 and the fixed resistor to GND, brighter light normally raises counts. Reversing those two parts reverses the relationship.
- delay(250) slows the Serial Monitor output so you can read changes while covering and uncovering the sensor.
Recorded LDR bright and dark readings
Explore the build photos and recorded results. Open any image to inspect the details.
About this recorded build
Contributor-supplied build photos and Arduino captures. These records are not an independent hardware retest. Some breadboard contacts are hidden; follow the documented pin connections.
The 800-count threshold belongs to this recorded setup. Choose a threshold from readings in your own lighting; these counts are not calibrated lux.

Supplied LDR and resistor build for GPIO34. Hidden contacts and the resistor value cannot be established from this image alone.
View full-size image
Supplied bright capture: raw ADC values approximately 862–877 are above the sketch threshold of 800.
View full-size image
Supplied dark capture: raw ADC count 0 is below the sketch threshold of 800.
View full-size imageCode and files
Expected Behaviour
Illustrative format, not recorded measurements: Light ADC: 650 | DARK Light ADC: 2380 | BRIGHT
For the shown divider, covering the LDR should lower the count. Your values depend on the part, wiring, lighting and ADC saturation. Choose a threshold between your own observed ranges. No percentage or lux is calculated.
Common Mistakes
Choosing a threshold before measuring
The example value may not match your LDR, resistor, or room lighting.
Expecting all LDR modules to behave the same
Different modules can have different resistor values and output direction.
Connecting the LDR without a fixed resistor
The ESP32 ADC needs a voltage, and the LDR alone does not create a useful divider.
Using 5 V on the divider
Some tutorials for other boards use 5 V.
Treating one reading as the final truth
Analog readings can wiggle because real signals contain noise.
Troubleshooting
Most ESP32 problems are wiring, power, library, or timing issues. Check these first.
The value never changes
Likely cause: GPIO34 may not be connected to the divider midpoint, or the LDR and resistor may be in separate breadboard rows.
Fix: Trace the exact midpoint where the LDR, resistor, and GPIO34 wire meet.
The value is always near 0
Likely cause: GPIO34 may be tied to GND, or the divider may be shorted.
Fix: Check that the LDR goes to 3.3 V and the resistor goes to GND with GPIO34 between them.
The value is always near 4095
Likely cause: The input may be miswired to 3V3, the ground resistor may be disconnected, or bright light may drive this divider beyond the useful ADC range.
Fix: Power off to trace the midpoint and fixed resistor to GND. A legitimate high-end plateau can be saturation; do not increase the supply or treat 4095 as a voltage.
BRIGHT and DARK seem reversed
Likely cause: The divider direction changes whether light makes the reading rise or fall.
Fix: Power off and confirm LDR to 3V3, fixed resistor to GND and midpoint to GPIO34. The shown topology should increase counts with light; only reverse the code comparison if you deliberately use the opposite divider topology.
The decision flickers near the threshold
Likely cause: The reading is noisy and sits too close to the cutoff.
Fix: Move the threshold farther from the boundary or average several readings.
Engineer Tip
Observe the raw readings in the real conditions where the project will run before choosing an analog threshold. Characterize the range first, then choose a threshold; this is not calibrated lux.
Remember This Forever
A raw count is not automatically a physical unit.
Characterize your own bright and covered conditions, choose a threshold, and keep that relative decision distinct from calibrated lux.
Mini Challenge
No wrong answers — experiment and have fun!
- Write down your bright value, covered value, and normal-room value.
- Change LIGHT_THRESHOLD so your room light is BRIGHT but a covered LDR is DARK.
- Average five analogRead() values and compare whether the decision flickers less.
- Print BRIGHT, DIM, or DARK using two thresholds instead of one.
FAQs
What is an analog sensor?
An analog sensor is a component or circuit that changes its output voltage as the real-world condition changes.
What is an LDR?
An LDR is a light-dependent resistor. Its resistance changes when the amount of light hitting it changes.
Can the ESP32 read an LDR directly?
The ESP32 reads voltage, not resistance directly. The LDR needs a resistor divider so light changes become voltage changes.
Why do I need a 10 kOhm resistor with the LDR?
The fixed resistor and LDR form a voltage divider. Without the fixed resistor, the ADC pin will not get a meaningful changing voltage.
Why are my bright and dark values different from yours?
Room lighting, LDR type, resistor value, wiring direction, and USB power can all change the exact raw ADC numbers.
What is calibration?
Calibration compares an instrument against documented reference values under specified conditions. This lesson only characterizes relative bright/dark counts; it does not calibrate illuminance in lux.
What is a threshold?
A threshold is a chosen cutoff value. If the sensor reading crosses that value, your code changes its decision.
Why does the sensor value jump around?
Analog sensor readings often contain noise from wiring, power, nearby light changes, and ADC variation.
Is sensor noise bad?
Small noise is normal. Engineers handle it with averaging, filtering, stable wiring, and carefully chosen thresholds.
Can I use ADC2 pins for analog sensors with WiFi?
On classic ESP32, ADC2 shares hardware with Wi-Fi. This example uses ADC1 GPIO34. Check the target-specific ADC documentation for other families.
Why does covering the LDR make the number go up in some circuits?
The direction depends on whether the LDR is connected to 3.3 V or GND in the voltage divider. The trend matters more than the direction.
How do soil moisture sensors relate to this mission?
Many soil moisture modules output a changing analog voltage. Observe dry and wet reference conditions before choosing relative thresholds, just as this lesson characterizes dark and bright readings. This alone does not establish calibrated physical moisture units.
Should I convert every sensor value to percent?
Percent can help humans understand the value, but always inspect the raw ADC number first.
Can analog sensors be used in real products?
Yes. Products use analog sensing for light, moisture, pressure, position, battery voltage, sound level, and many other measurements.
What should I learn after analog sensors?
Learn I2C communication. Many modern sensors are smart digital devices that communicate measured values over data wires.
Previous Mission
Next Mission
Continue Learning
Technical references
- Arduino-ESP32 3.3.2 ADC API — Raw counts, calibrated millivolts, attenuation and family-dependent ranges.

