ESP32 BASICS · LESSON 06
Analog Input: Using the ADC
How the ESP32's analog-to-digital converter works, its quirks, and how to get steadier readings.
Sensors like light-dependent resistors, potentiometers, and many temperature sensors output a voltage that varies smoothly, not just HIGH or LOW. The analog-to-digital converter (ADC) turns that voltage into a number your code can use.
Reading a value
adc_read.ino
#define SENSOR_PIN 34
void setup() {
Serial.begin(115200);
}
void loop() {
int raw = analogRead(SENSOR_PIN); // 0-4095
float volts = raw * (3.3 / 4095.0);
Serial.println(volts);
delay(200);
}
The ESP32's ADC reports a 12-bit value by default — a number from 0 to 4095 — representing a voltage between 0 V and roughly the board's reference voltage.
Quirks to know about
- Only certain pins (mostly in the GPIO 32–39 range) connect to the ADC — check your board's pinout before wiring a sensor to an arbitrary pin.
- Readings near the very top and bottom of the range are less linear than the middle; leave some headroom in your circuit rather than driving the pin to the extremes.
- Wi-Fi activity can add noise to ADC readings on some boards. Averaging several samples smooths this out.
KEY IDEA
Average a handful of readings instead of trusting a single sample — it costs almost nothing and removes most of the jitter.