ESP32 ADC Tutorial: analogRead(), Calibration & Examples (Arduino)

In this tutorial, you’ll learn how to use the ESP32 ADC to read analog voltage with the Arduino IDE. We’ll use analogRead() to get the raw 12-bit reading and analogReadMilliVolts() to get a calibrated voltage in mV. You’ll learn which ESP32 ADC pins to use (and which to avoid with Wi-Fi), the real input voltage range for each attenuation setting, how to calibrate and filter the readings, and how fast the ESP32 ADC really is.

Then, we’ll build a few ESP32 ADC example projects: reading a potentiometer, an LED dimmer, a calibrated voltmeter, a noise-reduction filter, multi-channel continuous (DMA) sampling, an ADC speed test, and measuring 12v with a voltage divider. We’ll also fix the most common ESP32 analogRead problems (always 0, always 4095, noisy readings, and code that stopped compiling after the Arduino core 3.x update). Without further ado, let’s get right into it!

ESP32 ADC analogRead Example

To read an analog voltage with the ESP32 ADC in Arduino, connect your signal (0 to 3.1v) to an ADC1 pin such as GPIO35, then call analogRead(35) for the raw value (0 – 4095) or analogReadMilliVolts(35) for the calibrated voltage in mV. No pinMode() is needed. Here is the shortest complete sketch:

Open the serial monitor at 115200 baud and turn the potentiometer. The rest of this tutorial explains the ESP32 ADC pins, the voltage range and attenuation, calibration, the wiring, and the full LAB examples.

Note: All examples in this tutorial are tested with: ESP32 DevKit V1 (ESP32-WROOM-32), Arduino-ESP32 core 3.3.12, Arduino IDE 2.x. Every listing in this tutorial compiles with core 3.x. If you’re on the old core 2.x, see the core 3.x changes section.


ESP32 ADC Pins (ADC1 & ADC2 Channels)

The ESP32 has two 12-bit SAR ADCs with 18 analog input channels in total: ADC1 has 8 channels on GPIO32 – GPIO39, and ADC2 has 10 channels on GPIO0, 2, 4, 12 – 15, and 25 – 27. The 30-pin ESP32 DevKit V1 board exposes 15 of them (GPIO37 and GPIO38 aren’t broken out, and GPIO0 is the BOOT button).

ADC UnitChannelGPIONotes
ADC1CH0GPIO36 (VP)Input-only
ADC1CH3GPIO39 (VN)Input-only
ADC1CH4GPIO32
ADC1CH5GPIO33
ADC1CH6GPIO34Input-only
ADC1CH7GPIO35Input-only (used in this tutorial)
ADC1CH1, CH2GPIO37, GPIO38Not on most DevKit boards
ADC2CH0GPIO4Not usable while Wi-Fi is on
ADC2CH1GPIO0Strapping pin (BOOT button)
ADC2CH2GPIO2Strapping pin, onboard LED on many boards
ADC2CH3 – CH6GPIO15, 13, 12, 14GPIO12 & GPIO15 are strapping pins
ADC2CH7 – CH9GPIO27, 25, 26Not usable while Wi-Fi is on

ESP32 ADC2 & Wi-Fi: Which Pins Work With Wi-Fi?

Use the ADC1 pins (GPIO32 – GPIO39) whenever your project uses Wi-Fi. The Wi-Fi driver uses ADC2, so while Wi-Fi is running, an analogRead() on an ADC2 pin can’t get the ADC, and the Arduino core returns 0 without any error in your sketch.

Note: GPIO34, 35, 36, and 39 are input-only pins with no internal pull-up or pull-down resistors. That makes them perfect for analog inputs, but you can’t use them as outputs. And never apply more than 3.3v to any ESP32 pin: use a voltage divider for higher voltages (LAB 7).


ESP32 ADC Resolution, Voltage Range & Attenuation

The ESP32 ADC is 12-bit by default, so analogRead() returns 0 – 4095. With the default 11dB attenuation ( ADC_11db), the usable input range is about 150mV to 3100mV on the original ESP32, not the full 0 – 3.3v. Inputs below ~150mV read 0, and inputs above ~3.1v read 4095.

ParameterESP32 (Arduino Core 3.x Default)
ADC Type2x SAR ADC (successive approximation)
Resolution12-bit (0 – 4095), configurable 9 – 12 bits
Default Attenuation11dB (ADC_11db)
Usable Range @ 11dB~150mV – 3100mV
Max Pin Voltage3.3v (absolute)
Internal Reference (VREF)1100mV nominal, 1000 – 1200mV chip to chip
CalibrationeFuse-based, applied by analogReadMilliVolts()
Continuous (DMA) Sampling Rate20kHz – 2MHz (ADC1 only)

ESP32 ADC Attenuation

The ESP32 ADC measures against an internal ~1.1v reference. To read higher voltages, the input passes through an attenuator first. The higher the attenuation, the higher the voltage you can measure, at the cost of some accuracy. Here are the measurable ranges from the Arduino-ESP32 ADC documentation:

ESP32 ADC attenuation vs input voltage range chart for ESP32, ESP32-S3, ESP32-S2 and ESP32-C3: ADC_0db, ADC_2_5db, ADC_6db and ADC_11db

AttenuationESP32ESP32-S3ESP32-S2 / C3
ADC_0db100 – 950mV0 – 950mV0 – 750mV
ADC_2_5db100 – 1250mV0 – 1250mV0 – 1050mV
ADC_6db150 – 1750mV0 – 1750mV0 – 1300mV
ADC_11db (default)150 – 3100mV0 – 3100mV0 – 2500mV

You can change the attenuation for all ADC pins or for one pin only:

For most projects, you’ll leave it at the default 11dB, which gives you the widest range. Use a lower attenuation only when your signal is small (for example a sensor that outputs 0 – 1v), because the narrower range gives you better resolution per mV.

ESP32 ADC Resolution

Use analogReadResolution(bits) to change the resolution of the value returned by analogRead(). On the original ESP32, values from 9 to 12 bits change the hardware resolution. Any other value (1 – 16) is just shifted from the 12-bit result, so asking for 16 bits doesn’t give you a better ADC. For example, analogReadResolution(10) gives you 0 – 1023, just like an Arduino UNO.


How the ESP32 ADC Works

The ESP32 has two SAR (Successive Approximation Register) ADCs. A SAR ADC samples the input voltage on a small internal capacitor, then finds its digital value with a binary search: it tries the most significant bit first, compares an internal DAC voltage with the input, keeps or clears that bit, and moves to the next one. A 12-bit result takes 12 of these steps, as you can see in the animation below.

ESP32 SAR ADC conversion animation: 12-bit successive approximation binary search comparing the DAC voltage with a 1873mV input, step by step from bit B11 to B0

Each ADC unit is controlled by an RTC controller (used by analogRead() and in deep sleep) or a digital controller with DMA for high-speed continuous sampling (LAB 5). Here is the internal block diagram from Espressif’s documentation:

ESP32 ADC internal block diagram: SAR ADC1 and ADC2 with RTC controllers, digital controllers and DMA

If it’s your first time learning about ADCs, check out this ADC tutorial for how ADCs work, ADC types, error sources, and sampling.


ESP32 ADC Arduino Functions (Core 3.x)

These are the ADC functions in the current Arduino-ESP32 core (3.x). The ones you’ll use 99% of the time are analogRead() and analogReadMilliVolts().

FunctionWhat It Does
analogRead(pin)Returns the raw ADC value (0 – 4095 at 12-bit). Not calibrated
analogReadMilliVolts(pin)Returns the calibrated input voltage in mV (uses the eFuse calibration data)
analogReadResolution(bits)Sets the resolution of analogRead() results (1 – 16, default 12)
analogSetAttenuation(atten)Sets the attenuation for all ADC pins: ADC_0db, ADC_2_5db, ADC_6db, ADC_11db (default)
analogSetPinAttenuation(pin, atten)Sets the attenuation for one pin
analogSetWidth(bits)ESP32 only: sets the hardware sample width (9 – 12 bits)
analogContinuous(pins, count, conv, freq, cb)Sets up continuous (DMA) sampling on ADC1 pins
analogContinuousStart() / Stop()Starts or stops continuous conversions
analogContinuousRead(&buf, timeout)Gets the averaged raw and mV results for each pin
analogContinuousSetAtten() / SetWidth()Attenuation and resolution for continuous mode
analogContinuousDeinit()Releases the continuous mode driver

ESP32 ADC Changes in Arduino Core 3.x (Old vs New)

Arduino-ESP32 core 3.0 (May 2024, now 3.3.x) moved to ESP-IDF 5 and removed several ADC functions that older tutorials still use, including the first version of this one. If your old ADC code stopped compiling after a core update, here is what to change, based on the official 2.x to 3.0 migration guide and our compile tests:

Old (Core 2.x)New (Core 3.x)Compile Error You’ll Get on 3.x
analogSetClockDiv(div)Removed. Use continuous mode to set a sampling rate'analogSetClockDiv' was not declared in this scope
adcAttachPin(pin)Removed. analogRead() attaches the pin itself'adcAttachPin' was not declared in this scope
analogSetVRefPin(pin)Removed. Calibration is automatic in analogReadMilliVolts()'analogSetVRefPin' was not declared in this scope
adcStart(), adcBusy(), adcEnd()Removed. analogRead() is a blocking read'adcEnd' was not declared in this scope
analogSetCycles(), analogSetSamples()Removed. Average in software (LAB 4) or use continuous mode'analogSetCycles' was not declared in this scope
esp_adc_cal_characterize() + esp_adc_cal_raw_to_voltage()analogReadMilliVolts(pin)Warning: legacy adc calibration driver is deprecated
ADC_ATTEN_DB_11ADC_11db (Arduino) or ADC_ATTEN_DB_12 (IDF)Warning: 'ADC_ATTEN_DB_11' is deprecated
ledcSetup() + ledcAttachPin()ledcAttach(pin, freq, res)'ledcSetup' was not declared in this scope

analogRead(), analogReadMilliVolts(), analogReadResolution(), analogSetAttenuation(), and analogSetPinAttenuation() work the same way on both cores, so a simple analogRead() sketch doesn’t need any change.


ESP32 ADC Example Wiring & Parts

All the LABs in this tutorial use the same setup: one 10kΩ potentiometer as the analog input on GPIO35, and one LED with a 330Ω resistor on GPIO5 for the LED dimmer LAB. Here is the wiring diagram for the ESP32 ADC example projects in this tutorial.

ESP32 ADC potentiometer wiring diagram: ESP32 DevKit V1 with a 10k potentiometer wiper on GPIO35 (analog input) and an LED with a 330 ohm resistor on GPIO5, on a breadboard

3V3 goes to one outer pin of the potentiometer (through the breadboard’s + rail). Power the pot from 3.3v only.

GND goes to the other outer pin of the potentiometer and to the LED’s cathode (short leg), through the breadboard’s − rail.

GPIO35 goes to the potentiometer’s middle pin (wiper). This is the ESP32 ADC input (ADC1_CH7) we read in the LABs.

GPIO5 goes to the 330Ω resistor, and the resistor goes to the LED’s anode (long leg). It’s the PWM output for the LED dimmer in LAB 2.

Component ESP32 DevKit Potentiometer Outer Pin 3V3 Potentiometer Wiper GPIO35 Potentiometer Outer Pin GND LED Anode (via 330Ω) GPIO5 LED Cathode GND

Note: Power the potentiometer from 3.3v, never from 5v (VIN). With 5v on the pot, the wiper can go above 3.3v and damage the ESP32’s ADC pin.

QTY.Component NameAmazon.comAliExpress
1ESP32 DevKit V1 (DOIT) BoardAmazonAliExpress
110kΩ PotentiometerAmazonAliExpress
1LEDs KitAmazonAliExpress
1Resistors Kit (330Ω, 30kΩ, 7.5kΩ)AmazonAliExpress
1100nF Ceramic CapacitorsAmazonAliExpress
10-25v Voltage Sensor Module (30k/7.5k Divider, LAB 7)AmazonAliExpress
1ADS1115 16-Bit ADC Module (Optional)AmazonAliExpress
1Breadboard & Jumper WiresAmazonAliExpress
1Micro USB CableAmazonAliExpress

ESP32 analogRead Potentiometer Example (LAB 1)

In this first ESP32 ADC example, we’ll read the potentiometer on GPIO35 with both analogRead() and analogReadMilliVolts() and print them in a format the Arduino Serial Plotter understands, so you can watch both values change as you turn the knob.

Wiring: the potentiometer’s wiper goes to GPIO35 and its outer pins go to 3V3 and GND as shown in the wiring diagram above.

Here is The Application Code For This Example (ESP32_ADC_Read_Pot_LAB1.ino)

ESP32 analogRead Example Code Explanation

First, we define the analog input pin. GPIO35 is an ADC1 pin (ADC1_CH7), so it keeps working when you add Wi-Fi later.

In the setup() function, we set the resolution and attenuation. Both lines only repeat the defaults (12-bit and 11dB), so you can delete them. I kept them so you know where to change these settings.

In the loop() function, we read the raw value and the calibrated voltage, then print them as label:value pairs, which the Serial Plotter in Arduino IDE 2.x shows as two named curves.

ESP32 analogRead Example Result

ESP32 ADC plot of analogRead() raw value and analogReadMilliVolts() voltage while turning a potentiometer on GPIO35: raw clips at 4095 and 0, calibrated mV range 142mV to 3171mV

Notice that the raw curve flattens at 0 near the low end and at 4095 near the top, while the mV curve stops at about 142mV and 3171mV on my board. That’s the ESP32 ADC’s usable range at 11dB attenuation, and the calibrated mV value never reads below the bottom of that range.


ESP32 Potentiometer LED Dimmer With ADC (LAB 2)

In this example, we’ll read the potentiometer with the ESP32 ADC and use the result to set the brightness of an LED with PWM. The PWM resolution is set to 12 bits so the ADC result (0 – 4095) can be written directly as the PWM duty cycle, with no scaling.

  1. Attach the LED pin (GPIO5) to a PWM channel at 1kHz and 12-bit resolution
  2. Read the ADC input channel (potentiometer on GPIO35)
  3. Write the ADC result to the LED’s PWM duty cycle

Wiring: the potentiometer stays on GPIO35, and the LED goes to GPIO5 through the 330Ω resistor, with its cathode to GND.

Here is The Application Code For This Example (ESP32_ADC_POT_LED_Dimmer_LAB2.ino)

ESP32 ADC LED Dimmer Code Explanation

In core 3.x, the LEDC (PWM) driver is pin-based. One call to ledcAttach() sets the pin, the frequency, and the resolution, and picks a free channel for you. Then ledcWrite() takes the pin number, not a channel number.

If you’re coming from the old version of this LAB (core 2.x), that’s the only change: ledcSetup() and ledcAttachPin() were removed in core 3.0. You can learn more about the PWM side in the ESP32 PWM Tutorial.

ESP32 ADC LED Dimmer Demo Video

This is the real hardware testing result for this example project on my ESP32 DevKit board. The LED brightness follows the potentiometer from fully off to fully on.


ESP32 ADC Read Voltage & Calibration (LAB 3)

To read an accurate voltage with the ESP32 ADC, use analogReadMilliVolts() instead of converting analogRead() with the usual raw * 3.3 / 4095 formula. The ESP32’s internal reference varies from 1000mV to 1200mV between chips, and its response is not linear near the ends, so the simple formula can be off by more than 100mV. analogReadMilliVolts() corrects for this with calibration data that Espressif burns into each chip’s eFuse at the factory.

In this LAB, we’ll print three values side by side: the raw reading, the naive formula, and the calibrated voltage. Then we’ll compare them with a multimeter.

Wiring: the same potentiometer on GPIO35. To check the readings, put your multimeter between GPIO35 and GND.

Here is The Application Code For This Example (ESP32_ADC_Read_Voltage_mV_LAB3.ino)

ESP32 ADC Calibration Code Explanation

The naive conversion assumes the ADC maps 0 – 3.3v linearly to 0 – 4095, which isn’t true for the ESP32:

The calibrated reading does all the work in one call. The first time you call it for an ADC unit, the core creates an ESP-IDF calibration scheme (line fitting on the ESP32 and ESP32-S2, curve fitting on the S3, C3, and C6) from the eFuse data and reuses it after that. So you don’t need to calibrate anything yourself or call it again in every reading. For the details, see Espressif’s ADC calibration driver documentation.

ESP32 ADC Calibration Test Result (vs Multimeter)

I set the potentiometer to four voltages, measured each one with my bench multimeter (HP 34401A) between GPIO35 and GND, and compared it with the two ESP32 readings:

ESP32 ADC calibration test setup: HP 34401A multimeter measuring the potentiometer voltage on GPIO35 of an ESP32 DevKit V1 on a breadboard

MultimeterRawNaive FormulaanalogReadMilliVolts()
0.207v880.071v0.219v
1.000v10440.841v1.018v
2.001v22431.807v2.029v
3.000v36832.967v2.998v
ESP32 ADC calibration test: multimeter readings of 0.207v, 1.000v, 2.001v and 3.000v on GPIO35 used to check analogReadMilliVolts() accuracy

ESP32 serial monitor output comparing raw analogRead value, naive raw * 3.3 / 4095 formula and calibrated analogReadMilliVolts() voltage at a 1.00v input

The calibrated reading stays within about 30mV of the multimeter across the range. The naive formula reads 136mV to 194mV low in the middle of the range, and it only gets close near 3v. Keep in mind that calibration can’t fix the dead bands: anything below about 140mV still reads around 140mV, and anything above ~3.1v reads the maximum. When I first tested this in 2021 with the legacy esp_adc_cal functions, a 2.00v input read 1.85v uncalibrated and 1.99v calibrated, with dead bands of about 0.14v at both ends.


ESP32 ADC Noise Reduction & Filtering (LAB 4)

The ESP32 ADC is noisy: even with the potentiometer standing still, readings jump by tens of counts. You can reduce this in hardware with a 100nF capacitor from the ADC pin to GND, and in software with multi-sampling and averaging. Espressif recommends both in its ADC documentation.

Be careful with the capacitor, though. It filters the high-frequency components of your signal as well as the noise. For a near-DC signal like a temperature sensor or a battery voltage, it’s fine. For anything that changes fast, I’d rather use an active buffer. In most of my designs, I put a voltage follower (op-amp buffer) and a small RC filter in front of the ADC input. The buffer also stops the channels from disturbing each other when the ADC’s sampling capacitor switches from one channel to the next.

ESP32 ADC input buffer and RC filter: op-amp voltage follower and RC low-pass filter before the ADC1 sampling capacitor

In this LAB, we’ll do the software part: a moving average filter that averages the last 15 readings.

Wiring: the same potentiometer on GPIO35. For the hardware filter, add a 100nF capacitor between GPIO35 and GND.

Here is The Application Code For This Example (ESP32_ADC_Moving_Average_LAB4.ino)

ESP32 ADC Moving Average Filter Code Explanation

The readADC_Avg() function stores each new reading in a circular buffer of FILTER_LEN samples and returns the average of the whole buffer. You can change the filter length to whatever you want, but the larger it gets, the slower the filter responds to real changes (more phase lag), while the output gets smoother.

ESP32 ADC Filter Demo Video

This is the real hardware testing result for this example on my ESP32 board. The blue curve is the raw ADC reading and the red curve is the filtered output.


ESP32 ADC Continuous Mode (DMA) Multi-Channel Example (LAB 5)

The ESP32 ADC continuous mode samples one or more ADC1 pins in the background with DMA, at a sampling rate you choose (20kHz to 2MHz on the ESP32), and calls your function when a batch of conversions is ready. Use it when you need a fixed sampling rate or several channels without blocking the CPU, which analogRead() can’t do.

In this LAB, we’ll sample GPIO34 and GPIO35 at 20kHz, average every 20 conversions per pin, and print both channels.

Wiring: the potentiometer stays on GPIO35, and a jumper wire connects GPIO34 to GND so the second channel has a known input. You can put any other 0 – 3.1v signal on GPIO34 instead.

Here is The Application Code For This Example (ESP32_ADC_Continuous_Multi_Channel_LAB5.ino)

ESP32 ADC Continuous Mode Code Explanation

We configure the continuous driver with the pin list, how many conversions to average per pin, the total sampling frequency, and the callback function. The callback runs in an interrupt, so it only sets a flag.

In the loop(), when the flag is set, analogContinuousRead() gives us an array with one result per pin, holding both the averaged raw value and the averaged calibrated voltage in mV.

Note: Continuous mode works on ADC1 pins only, and an ADC unit can’t run continuous mode and analogRead() at the same time. Don’t call analogRead() on an ADC1 pin while continuous mode is running. The minimum sampling frequency is 20kHz on the ESP32 (611Hz on the S2, S3, C3, and C6).

ESP32 ADC Continuous Mode Result

GPIO34 (tied to GND) stays at 0 (142mV, the bottom of the range), while GPIO35 follows the potentiometer as I turn it. Both channels are sampled in the background, and each line is the average of 20 conversions per pin.

ESP32 ADC continuous mode multi-channel serial output: GPIO34 tied to GND reads 0 while GPIO35 follows a potentiometer, raw value and mV


How Fast Is the ESP32 ADC? (Sampling Rate Test, LAB 6)

On my ESP32 DevKit with core 3.3.12, one analogRead() call takes about 43.5µs (23 kSPS), and analogReadMilliVolts() takes about 44.4µs. For faster or evenly spaced samples, use continuous mode, which runs from 20kHz up to 2MHz on the ESP32.

Readers often ask how fast the ESP32 ADC can go, and the datasheet doesn’t give a simple answer for analogRead(). So let’s measure it. This sketch times 10,000 calls of each function with micros(). In core 3.x, analogRead() is a blocking read that returns only after the conversion is done, so this timing is the real time per sample.

Wiring: the same potentiometer on GPIO35. The speed doesn’t depend on the input voltage.

Here is The Application Code For This Example (ESP32_ADC_Speed_Test_LAB6.ino)

ESP32 analogRead speed test serial output: analogRead() 43.5us per sample (23 kSPS) and analogReadMilliVolts() 44.4us per sample at 240MHz

If you need precise timing between samples with analogRead(), call it from a timer interrupt or a timed task. See the ESP32 Timers & Timer Interrupt Tutorial.


Measure 12v (or a Battery) With ESP32 ADC & Voltage Divider (LAB 7)

To measure a voltage higher than 3.3v with the ESP32 ADC, scale it down with a voltage divider and multiply the reading back in your code. With R1 = 30kΩ and R2 = 7.5kΩ (the values on the common “0 – 25v voltage sensor” modules), the divider ratio is 5, so 12v at the input becomes 2.4v at the ESP32 pin.

The formula for the input voltage is:

VIN = VPIN × (R1 + R2) / R2

For example, if analogReadMilliVolts() reads 2400mV: VIN = 2.4 × (30k + 7.5k) / 7.5k = 12.0v. Keep the maximum input below 15.5v with these resistors so the ADC pin stays under 3.1v.

ESP32 voltage divider wiring diagram to measure 12v: 30k ohm and 7.5k ohm resistors on a breadboard, divider midpoint to GPIO34, 12v supply and ESP32 sharing GND

12V+ goes to the top of R1 (30kΩ). This is the voltage you’re measuring, so it never touches an ESP32 pin directly.

GPIO34 goes to the divider’s middle point, between R1 (30kΩ) and R2 (7.5kΩ). GPIO34 is an ADC1 pin, so this works with Wi-Fi on.

GND goes to the bottom of R2, the negative terminal of the 12v source, and the ESP32’s GND.

Here is The Application Code For This Example (ESP32_ADC_Voltage_Divider_LAB7.ino)

Note: The source being measured and the ESP32 must share the same GND. Use 1% resistors, and for the best accuracy measure your two resistors with a multimeter and put the real values in R1 and R2. The divider’s high impedance can make readings noisy, so the code averages 16 samples (a 100nF capacitor from GPIO34 to GND also helps).


ESP32 ADC Accuracy: When to Use an External ADC (ADS1115)

The ESP32 ADC is fine for potentiometers, light sensors, battery monitoring, and most hobby measurements, especially with analogReadMilliVolts() and some averaging. But it’s not linear near both ends of its range, it’s noisy, and its effective resolution is closer to 9 – 10 bits than 12. For precise measurements, use an external ADC such as the ADS1115 over I2C.

ParameterESP32 Internal ADCADS1115 (External, I2C)
Resolution12-bit (≈9 – 10 effective bits)16-bit
Input Range~0.15 – 3.1v (at 11dB)±0.256v to ±6.144v (programmable gain)
LinearityPoor near 0v and near 3.1vVery good across the range
Differential InputsNoYes (2 differential or 4 single-ended)
Max Sampling RateUp to 2MHz (continuous mode)860 SPS
Works With Wi-FiADC1 onlyYes, any pin (I2C)
Best ForPots, sensors, battery level, fast samplingPrecise voltage, current, and bridge sensors

My personal opinion: I wouldn’t design a critical measurement system (where human health, safety, or money is involved) around the ESP32’s internal ADC. Use the ESP32 for what it does best (Wi-Fi, BLE, and the application) and offload precise measurements to a dedicated ADC chip or a small microcontroller with a well-documented ADC. It totally depends on the application, though, and at the end of the day, it’s your call.


ESP32 ADC Troubleshooting (analogRead Not Working)

ESP32 analogRead Always Returns 0

The most common cause is an ADC2 pin (GPIO0, 2, 4, 12 – 15, 25 – 27) while Wi-Fi is on: the read fails silently and returns 0. Move the input to an ADC1 pin (GPIO32 – 39). Also check the input isn’t below ~150mV (the dead band), that you used the GPIO number and not the board label, and that the pin is really an ADC pin.

ESP32 analogRead Always Returns 4095

The input is above the top of the range: about 3.1v at 11dB (or lower if you changed the attenuation). Check the wiring (a floating or 3.3v-shorted pin reads 4095), check that the pot isn’t powered from 5v, and use a voltage divider for higher voltages (LAB 7).

ESP32 ADC Readings Are Noisy or Jumping

Add a 100nF capacitor from the ADC pin to GND, average several samples (LAB 4), keep analog wires short and away from the antenna, and power the board from a clean supply. Wi-Fi transmissions add noise too, so sample between transmissions if you can.

‘ledcSetup’ was not declared in this scope

Your sketch uses the core 2.x LEDC API, removed in Arduino-ESP32 core 3.0. Replace ledcSetup(ch, freq, res) + ledcAttachPin(pin, ch) with ledcAttach(pin, freq, res), and write with ledcWrite(pin, duty). See LAB 2.

‘analogSetClockDiv’ was not declared in this scope

This function (and adcAttachPin, analogSetVRefPin, adcStart/adcBusy/adcEnd, analogSetCycles, analogSetSamples) was removed in core 3.0. Delete the line. For a set sampling rate, use continuous mode (LAB 5). See the core 3.x changes table.

esp_adc_cal: legacy adc calibration driver is deprecated

The old esp_adc_cal functions still compile on core 3.x with this warning, but you don’t need them. Replace the whole calibration code with analogReadMilliVolts(pin), which applies the same eFuse calibration (LAB 3).

ESP32-S2 analogRead Returns More Than 4095

Early ESP32-S2 chips (revision v0.0) default to 13-bit readings (0 – 8191) because of an ADC erratum. Call analogReadResolution(12) in setup() to get 0 – 4095.


ESP32 vs ESP32-S3 vs ESP32-C3 ADC

All ESP32 chips use the same Arduino ADC functions, but the number of channels, the pins, the range, and the ADC2 rules differ. On the ESP32-C3, ADC2 can’t be used with analogRead() at all, and the ESP32-C6 has only one ADC.

ChipADC UnitsChannelsRange @ 11dBADC2 NotesContinuous Rate
ESP32218 (ADC1: 8, ADC2: 10)150 – 3100mVBlocked while Wi-Fi is on20kHz – 2MHz
ESP32-S22200 – 2500mVShared with Wi-Fi611Hz – 83kHz
ESP32-S3220 (GPIO1 – 20)0 – 3100mVShared with Wi-Fi611Hz – 83kHz
ESP32-C326 (ADC1: 5, ADC2: 1)0 – 2500mVNot supported in analogRead()611Hz – 83kHz

The LAB sketches in this tutorial use GPIO34 and GPIO35, which are ADC1 pins on the original ESP32 only. On the ESP32-S3, use an ADC1 pin from GPIO1 – GPIO10 (for example GPIO1 and GPIO2), and on the ESP32-C3, use GPIO0 – GPIO4. Everything else in the code stays the same, and the sketches compile for both boards on core 3.3.12.


Wrap Up

To conclude this tutorial, we’ve seen how to use the ESP32 ADC in Arduino core 3.x: reading raw values with analogRead(), reading calibrated voltage with analogReadMilliVolts(), choosing the right ADC1 pins, setting the attenuation, filtering noise, sampling in continuous mode, and measuring higher voltages with a divider. You can build on these examples to read sensors like the LM35 temperature sensor.

If you’ve got any questions or suggestions, drop me a comment below and I’ll be glad to help! And check out the ESP32 Programming Tutorials series for more ESP32 tutorials.


Download ESP32 ADC Project Files

Download all attachment files for this Article/Tutorial (project files, schematics, code, etc.) using the link below. Everything is tested and verified to work on real hardware, as you’ve seen in the demo videos & screenshots. Please consider supporting my work if it helped you.


ESP32 ADC FAQ

Does the ESP32 have analog input pins?

Yes. The ESP32 has 18 analog input channels on two 12-bit ADCs: ADC1 on GPIO32 – GPIO39 and ADC2 on GPIO0, 2, 4, 12 – 15 and 25 – 27. The 30-pin DevKit V1 exposes 15 of them. Use the ADC1 pins when your project uses Wi-Fi.

What is the maximum voltage the ESP32 ADC can read?

With the default 11dB attenuation, the ESP32 ADC reads up to about 3.1v (3100mV), and the absolute maximum on any pin is 3.3v. To measure higher voltages like 5v or 12v, use a voltage divider and multiply the reading back in code.

Can I connect 5v directly to an ESP32 analog pin?

No. ESP32 pins are not 5v tolerant, and 5v can damage the pin or the chip. Scale a 5v signal down with a voltage divider (for example 10kΩ from the 5v signal to the pin and 15kΩ from the pin to GND gives 3.0v, inside the 3.1v range).

What is the difference between analogRead() and analogReadMilliVolts()?

analogRead() returns the raw, uncalibrated ADC value (0 – 4095 at 12-bit). analogReadMilliVolts() returns the input voltage in mV, corrected with the calibration data stored in the chip’s eFuse, which is much more accurate than converting the raw value with a simple formula.

What is the ESP32 ADC input impedance?

The ESP32 ADC input is a switched sampling capacitor, not a fixed resistance, so the source impedance matters. Keep the source impedance low (under about 10kΩ), or add a 100nF capacitor at the pin or an op-amp buffer when you read from a high-impedance source like a large voltage divider.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

11 thoughts on “ESP32 ADC Tutorial: analogRead(), Calibration & Examples (Arduino)”

  1. I have a code for counting samples of ADC.I’m attaching the code here.

    Code:

    #include
    #include
    #include

    const uint16_t samples = 10000;
    uint8_t clockDiv =255;

    void setup() {
    Serial.begin(115200);
    adc1_config_width(ADC_WIDTH_BIT_12);
    adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);
    adc_set_clk_div(clockDiv); //doesn’t work
    }

    void loop() {

    const uint16_t samples = 10000;
    auto t1 = std::chrono::system_clock::now();

    for (uint32_t i=0; i
    adc_buf[i]=adc1_get_raw(ADC1_CHANNEL_3);
    }
    auto t2 = std::chrono::system_clock::now();

    std::chrono::duration diff = t2 – t1;
    auto ms = std::chrono::duration_cast(diff).count();
    //For seeing output if needed
    //for (int j=0; j
    // Serial.print(adc_buf[j]);Serial.print(” “);
    // }

    Serial.println();
    Serial.printf(“Samples: %u\n”, samples);
    Serial.printf(“Duration: %llums\n”, ms);
    Serial.printf(“KSPS: %0.4f”, static_cast(samples) / ms);
    Serial.println();

    }

    The problem is that no clock divider is changing the sampling rate .. My device showing about 20ksps.
    Although if I change CPU frequency from the ArduinoIDE settings the code showing sampling rate as low as 1ksps .But why does the adc_set_clk_div(clockDiv) not working.Could you kindly look over this problem ?

    Reply
    • Hi Samit!
      Well, i’ve looked into this while writing this tutorial.
      Your procedure for measuring the adc sampling rate is logically correct.
      However, the adc read function is a non-blocking routine. Which means, reading the timer before and after that function will not give you the adc conversion time.
      There was a function in the adc driver that was a blocking routine which waits until conversion completion then returns. But they’ve removed it for some reason back in 2019 or something.
      I know it can be strange a little bit, but the adc function we have is working asynchronously in a non-blocking way.
      This shows you that changing the clock div won’t change anything at all. While changing cpu frequency will change the speed it goes from time capture before and after adc read. This is logical too!

      Hope this helps.

      Kind regards,

      Khaled M.

      Reply
      • Thanks a lot for your explanation. Yeah it’s look kinda weird . However then could you suggest any other way of measuring the ADC sampling frequency as I need to know and be able to change it’s ADC sampling frequency for my project. Thanks in advance.

        Reply
        • I think switching to espressif IDF can help you get more control over the ADC operation. But with a little bit more complex workflow. I’ll be doing this in the future here on my website.
          For now, you can search online if you want to take this route!

          Reply
  2. Also as mentioned ADC-DMA can be used for faster sampling ,could you kindly demonstarate with example how to store ADC values in DMA and read it. Hoping to see more about DMAs. And yes, you have created an excellent website,your contents are so well explained and brought all together in a
    so much organized way.Looking forward to seeing lots of videos and projects from here .

    Reply
  3. Khaled – thanks for this good post. As to the non-blocking ADC read…does using the (I think) lower-level function sequence in Arduino IDE allow for correct time measurement?

    startMicro = micros();
    adcStart(ADXLpin);
    while(adcBusy(ADXLpin)) {;}
    ADXLval = adcEnd(ADXLpin);
    endMicro = micros();

    Reply
    • While researching this topic, I found out that they’ve removed it from the APIs a few years ago. Try to change the ADC’s clock divider and see if it’s getting faster or slower.
      If it’s giving you the same conversion time, then the API is not working in the latest Arduino Framework.

      Reply
  4. Hello Khaled, thanks for the tutorial… I have spent some precious time on trying to correct it… Now i can trust in the values, at least between 140mV ~ 3120mv, as you mentioned there are dead-bands in both spectrum ends, but i am not expecting reading values near to the ends (i guess…).

    But, i have some questions:

    first, regarding ADC, do i have to implement the ADC calibration functions on every code? The calibrated values is not stored on register for future use? If i always have to implement, do i have to call the funcitions every time i do an analog read, or it can be just in the initialization? Or run time to time? Have you tested not call the function and noticed some deviation with time (that needs to do a recalibration)?

    and now regarding the DAC (pwm function). I noticed that you are using some funcionts to configure the PWM output (such configuration as frequency and resolution), i guess this functions which you are using is not provided by espressif, right? And i have checked on the espressif documention and could not find anything that mention i can change the PWM output characteristics (they say that the resolution is 255 and that is it!) so, could you help on this?

    Thanks a lot.

    Reply
  5. Hii anyone please help me, in testing ADCs. After lots of struggle I found good content so you are only my hope please share your contact number.

    Reply
  6. Hola Khaled! excelente contenido, hace mucho que busco información del ADC del esp32, muchos dicen que no es de fiar, pero tu has tirado por tierra esto con este articulo, y me has dado una luz de esperanzo en la implementación para mi tesis, medición de variables eléctricas de CA, es viable realizarlo con este micro? Tanto la tensión como la corriente, seria ideal ir por canales distintos, como se implementaría el filtro por hardware del cual hablas?, es viable poder medir algunos armónicos, tanto en tensión como de corriente? Armónicos de 3,5,7,9 en alguna de las formas de onda? Sigue así y ojalá puedas profundizar éste tema del ADC.

    Reply

Leave a Comment