PlatformIO Community

ESP32 ADC2 readproblem

Hi all
I’m using the esp32 wroom module and want to read out some analog values with the ADC2. But it doesn’t work correct.

I think the problem occours at configuration with “atten” flag. ADC1 works fine and gives me good results. but ADC2 is always to high and seems to be out of range.

As example I read a voltage of 1.563V:
ADC1 with ATTEN_DB_6 (max. 2.2V) gives me 2931 wich is fine for me.
ADC2 with ATTEN_DB_6 (max. 2.2V) gives me 4095 wich is not correct.

my config file looks like this:

static const adc_channel_t channel[8] = {ADC_CHANNEL_0, ADC_CHANNEL_3, ADC_CHANNEL_4, ADC_CHANNEL_5, ADC_CHANNEL_6, ADC_CHANNEL_7, ADC_CHANNEL_8, ADC_CHANNEL_9};

void configure_adc(void){
    //Configure ADC
    esp_adc_cal_value_t val_type[2];
    // Configure ADC1 Channel 0, 3-7
    for(int i = 0; i<=7; i++){
        adc1_config_channel_atten(channel[i], atten);
    // Configure ADC2 Channel 7 - 9
    for(int i = 5; i<=8){
        adc2_config_channel_atten(channel[i], atten);
    //Characterize ADC
    adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
    for(int i = 0; i<=1; i++) {
        val_type[i] = esp_adc_cal_characterize(unit[i], atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);

Reading looks like this:

//Reading ADC2 Channel 7 to 9
 for(int j = 7; j<=9; j++){
           adc_reading[j] = 0;
            for (int i = 0; i < NO_OF_SAMPLES; i++) {
                int32_t raw;
                adc2_get_raw((adc2_channel_t)channel[j], ADC_WIDTH_BIT_12, &raw);
                adc_reading[j] += raw;
        for(int j = ARR_CHANNEL_U2_MIN; j<=ARR_CHANNEL_U2_MAX; j++){
            adc_reading[j] = adc_reading[j]/NO_OF_SAMPLES;

After this the value of adc_reading[X] is 4095 and this is wrong. Is there an issue while configuration or reading that I don’t see yet?

So first of all this loop is out-of-bounds because i=8 is invalid for a channel array with 8 elements, max index is 7.

Here again, you read channel[j] with j = 7…9, but only 7 is valid index? If you want to read channel 7 to 9 you have to use the channel elemnts from 5,6 and 7, which map to ADC_CHANNEL_7…9.

hi maxgerhardt
thanks for answering.
ohh you are right. I did a mistake while copy the code from vs code to the forum. In vs-code I’ve some constants wich are the correct arraybounds.

The issue is also existing when the bounds are correct.

only difference to ADC1 is this function. For ADC2 it seems that there are no function like this.


The adc2_config_channel_atten(…) function looks also very different to adc1_config_channel_atten(…) in original rtc_module.c file. I know that it has to take care that wifi is off but shouldn’t there be the functions adc_gpio_init(…) and adc_set_atten(…) also?

Okay, can you share the minimal, complete example code with which the problem occurs? Github or google drive uploads preferred. It’s hard to say whether we’re talking about the same code and problem then.

There is an issue with ADC2 if WiFi is enabled which may well lead to the problems you are experiencing with FSD readings.