esp32-s2 adc configuration and attenuation

Adraen
Posts: 4
Joined: Wed Jun 21, 2017 10:17 am

esp32-s2 adc configuration and attenuation

Postby Adraen » Thu Jul 08, 2021 6:20 pm

Hi there,

I'm having a bit of an issue grasping the attenuation of the ADC. According to the documentation the voltage range is dependent on the attenuation and "By setting higher attenuation it is possible to read higher voltages." https://docs.espressif.com/projects/esp ... dc_atten_t

Although doing some tests with the latest esp-idf (4.4/master branch), I'm really not seeing any differences changing the attenuation. Regardless of the configured attenuation (from 0db to 11db) I get a reading from 0 to 8191 in the voltage range 0 -- ~2.5v. Even at 0db attenuation I get a reading for 1.0v--2.5V and at 11db attenuation is can't read anything above ~2.5v (but still down to 0v)

My setup is very simple, I've got a potentiometer as a voltage divider connect to channel 0 on an esp32-s2.

Reading the documentation I would expect the ADC reading to saturate when the voltage is above the range of the attenuation (e.g 2v at 0db attenuation to be either 8191 or a fixed value once the voltage is too high for that attenuation).

Anything obvious i'm missing here? Is it just that if the voltage read isn't in the suggested range for the attenuation then it won't be accurate? If so is it simply a matter of shifting attenuation at certain thresholds?

Thanks"

Code: Select all

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <soc/adc_channel.h>
#include <soc/rtc.h>
#include <driver/adc.h>
#include <esp_adc_cal.h>
#include <esp_log.h>

static const char* TAG = "test";

void app_main() {
    adc1_config_width(ADC_WIDTH_BIT_13);
    adc1_config_channel_atten(ADC1_GPIO1_CHANNEL, ADC_ATTEN_11db);

    for (;;) {
        int raw = adc1_get_raw(ADC1_GPIO1_CHANNEL);
        ESP_LOGI(TAG, "%d", raw);

        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

User avatar
ESP_krzychb
Posts: 400
Joined: Sat Oct 01, 2016 9:05 am
Contact:

Re: esp32-s2 adc configuration and attenuation

Postby ESP_krzychb » Fri Jul 09, 2021 6:37 am

Hi Adraen,

As far as I have checked, your example works correctly with the latest master branch https://github.com/espressif/esp-idf/co ... 48fda1884e.
For ADC_ATTEN_0db I see ADC readings saturating at about 0.75 V; for ADC_ATTEN_11db the readings saturate at about 2.5 V.

I have a minor comment that the link you provided is to documentation for ESP32. The correct link for ESP32-S2 is https://docs.espressif.com/projects/esp ... s/adc.html (even if documentation for adc1_config_channel_atten function on ESP-IDF master is common for both ESP32 and ESP32-S2 :D )

Adraen
Posts: 4
Joined: Wed Jun 21, 2017 10:17 am

Re: esp32-s2 adc configuration and attenuation

Postby Adraen » Fri Jul 09, 2021 7:49 am

Hi krzychb,

Thank you very much for checking, sorry for wasting your time, another day and this time it works... I spent ages on this yesterday and couldn't make any sense of it, spent 5 minutes on it today and it's just working as i'm expecting...

For the documentation, yeah you're right, just spent so much time jumping between documentations yesterday that I ended up pasting the link to the wrong one :D

Anyway, thanks again and sorry for the wasted time

User avatar
ESP_krzychb
Posts: 400
Joined: Sat Oct 01, 2016 9:05 am
Contact:

Re: esp32-s2 adc configuration and attenuation

Postby ESP_krzychb » Fri Jul 09, 2021 9:43 am

No worries!

abhijeet085
Posts: 1
Joined: Fri Jan 20, 2023 12:50 pm

Re: esp32-s2 adc configuration and attenuation

Postby abhijeet085 » Fri Jan 20, 2023 12:55 pm

Hi Adraen,

I am also using same setup as you have used. I am facing the issue when I applied 3.3v, I am reading 2.5v(which means it got saturated).

Are you able to read 3.3v with max count as 8191 ?? If yes, then can you please guide me?


Thanks,
Abhijeet.

Who is online

Users browsing this forum: No registered users and 96 guests