Reading Analog Sensors - the ESP32 ADC
So far your ESP32 has only asked the world yes-or-no questions: is the button pressed or not? Is the pin HIGH or LOW? But most of the real world is not yes-or-no. Light is not simply "on" or "off" - it is dim, medium, or blindingly bright. Temperature is not "hot or cold" - it is 19.4 degrees, or 23.1, or 31.7. A knob is not just "left or right" - it is turned exactly 37% of the way round.
To measure these smooth, in-between quantities, the ESP32 uses a special piece of hardware called an ADC - an Analog-to-Digital Converter. This one lesson turns your board from a switch-reader into a genuine measuring instrument. By the end you will build a working automatic night-light that turns an LED on by itself when the room goes dark.
What you will learn
- What an ADC actually does: turning a continuous voltage into a whole number.
- The ESP32's 12-bit ADC, its
0-4095range, and howanalogRead()works. - How the ESP32 compares with the older Arduino Uno (10-bit,
0-1023). - Attenuation, the usable voltage range, and why readings go strange at the very top and bottom.
- The two ADC blocks (ADC1 and ADC2) and the golden rule: ADC2 breaks when WiFi is on.
- How a photoresistor (LDR) plus one fixed resistor forms a voltage divider - with the real formula and real numbers.
- Turning raw readings into useful values: mapping, calibration, and smoothing out noise.
- Building a complete, self-adjusting night-light, and fixing jumpy readings.