Read an ESP32 analog input
Differentiate a raw conversion result from a calibrated voltage and protect the input from out-of-range signals.
What you'll learn
- Configure ADC1 in oneshot mode with ESP-IDF.
- Read a raw analog-to-digital conversion count.
- Understand attenuation, resolution and calibration.
- Protect the ADC input from unsupported voltage levels.
Components needed
- Original ESP32 boardGPIO34 / ADC1 channel 6 accessible
- 10 kΩ potentiometerA variable voltage for experimentation
- Jumper wiresInput and ground connections
- USB cablePower, programming and serial monitoring
Circuit diagram
Circuit diagram placeholder
Connect the potentiometer wiper to GPIO34 and join grounds. Keep every applied voltage inside the limits documented for the exact ESP32 input and attenuation.
Diagram coming soonCode
Study the highlighted firmware, then copy it into the matching development environment. Adapt board pins and initialization before compiling.
#include "esp_adc/adc_oneshot.h"
adc_oneshot_unit_handle_t adc1;adc_oneshot_unit_init_cfg_t init = { .unit_id = ADC_UNIT_1,};adc_oneshot_new_unit(&init, &adc1);adc_oneshot_chan_cfg_t channel = { .atten = ADC_ATTEN_DB_12, .bitwidth = ADC_BITWIDTH_DEFAULT,};adc_oneshot_config_channel(adc1, ADC_CHANNEL_6, &channel);int raw = 0;adc_oneshot_read(adc1, ADC_CHANNEL_6, &raw);ADC1 channel 6 maps to GPIO34 on the original ESP32. ADC_ATTEN_DB_12 selects the attenuation range, not permission to apply any voltage. Raw counts are digital conversion results; calibration and the selected attenuation are needed when interpreting them as millivolts.
Simulate It
Run the circuit in your browser when a Wokwi simulation is connected to this lesson.
Wokwi simulation placeholder
Add the public Wokwi embed URL for this lab to launch the interactive simulator here.
wokwiEmbedUrl: "https://wokwi.com/projects/..."Expected Output
The raw value changes as the potentiometer position changes. It is a conversion count, not a calibrated millivolt reading; print it inside a loop to observe the variation.
Expected output image placeholder
Expected ESP32 ADC output placeholder showing changing raw conversion counts
Diagram coming soon07 · Challenge Task
Take this lab further
Print ten samples per second and calculate the average of ten readings. Compare the averaged output with single samples while turning the potentiometer.