Lab 04
Intermediate
ESP32

Read an ESP32 analog input

Differentiate a raw conversion result from a calibrated voltage and protect the input from out-of-range signals.

MCU:ESP32 · ADC oneshot
Official reference documentation
01

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.
02

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
03

Circuit diagram

04

Code

Study the highlighted firmware, then copy it into the matching development environment. Adapt board pins and initialization before compiling.

main.c
#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.

05

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/..."
06

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.

07 · 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.

Up next · Lab 05

Your first Arduino LED blink

Next Lab
Back to all labs