ADC legacy vs OneShot

rocotocloc
Posts: 23
Joined: Tue May 07, 2024 5:47 am

ADC legacy vs OneShot

Postby rocotocloc » Tue Nov 12, 2024 3:04 pm

Hello,

I have the following code that works perfectly fine to read the raw value from a temperature sensor (LM35DZ):

Code: Untitled.c Select all

#include "driver/adc.h"

void water_probe_temperature_test_legacy(void *pvParameters)
{
int adc_raw;

ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_9));

while (1)
{
adc_raw = adc1_get_raw(ADC_CHANNEL_3);
printf("water probe raw data: %d\n", adc_raw);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

This gives something like the following and it reacts properly to temperature changes:
water probe raw data: 289
water probe raw data: 289
water probe raw data: 289
water probe raw data: 289
water probe raw data: 289
water probe raw data: 288
water probe raw data: 282
water probe raw data: 278
water probe raw data: 273
water probe raw data: 269
water probe raw data: 265
water probe raw data: 263
water probe raw data: 262
water probe raw data: 262

But since I get the following warning when compiling:
#warning "legacy adc driver is deprecated, please migrate to use esp_adc/adc_oneshot.h and esp_adc/adc_continuous.h for oneshot mode and continuous mode drivers respectively"

I am trying to migrate the code so I wrote this:

Code: Untitled.c Select all

#include "esp_adc/adc_oneshot.h"

void water_probe_temperature_test_new_driver(void *pvParameters)
{
int adc_raw;

adc_oneshot_unit_handle_t adc1_handle;
adc_oneshot_unit_init_cfg_t init_config1 = {
.unit_id = ADC_UNIT_1,
};
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));

adc_oneshot_chan_cfg_t config = {
.bitwidth = ADC_BITWIDTH_9,
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_3, &config));

while (1)
{
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_3, &adc_raw));
printf("water probe raw data: %d\n", adc_raw);
vTaskDelay(pdMS_TO_TICKS(1000));
}

ESP_ERROR_CHECK(adc_oneshot_del_unit(adc1_handle));
}

But the result is always all bits set to 1 (511, since it's 9 bits width):
water probe raw data: 511
water probe raw data: 511
water probe raw data: 511
water probe raw data: 511
water probe raw data: 511
water probe raw data: 511
water probe raw data: 511
water probe raw data: 511

I am following this example but I cannot see what's wrong,

Any ideas?

Thank you.

rocotocloc
Posts: 23
Joined: Tue May 07, 2024 5:47 am

Re: ADC legacy vs OneShot

Postby rocotocloc » Wed Nov 13, 2024 6:44 am

I reply to myself.

It's related to the attenuation, for some reason the default value is not the same in the legacy and new library.

I have set 12 db attenuation and I am getting exactly the same value with both libraries:

Code: Select all

void water_probe_temperature_test_legacy(void *pvParameters)
{
    int adc_raw;

    ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_9));
    // SET ATTENUATION
    ESP_ERROR_CHECK(adc1_config_channel_atten(ADC_CHANNEL_3, ADC_ATTEN_DB_12));

    while (1)
    {
        adc_raw = adc1_get_raw(ADC_CHANNEL_3);
        printf("water probe raw data: %d\n", adc_raw);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

Code: Select all

void water_probe_temperature_test_new_driver(void *pvParameters)
{
    int adc_raw;

    adc_oneshot_unit_handle_t adc1_handle;
    adc_oneshot_unit_init_cfg_t init_config1 = {
        .unit_id = ADC_UNIT_1,
    };
    ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));

    adc_oneshot_chan_cfg_t config = {
        .bitwidth = ADC_BITWIDTH_9,
        .atten = ADC_ATTEN_DB_12, // SET ATTENUATION
    };
    ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_3, &config));

    while (1)
    {
        ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_3, &adc_raw));
        printf("water probe raw data: %d\n", adc_raw);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }

    ESP_ERROR_CHECK(adc_oneshot_del_unit(adc1_handle));
}

Dbuggz32
Posts: 2
Joined: Sat Oct 10, 2020 9:48 pm

Re: ADC legacy vs OneShot

Postby Dbuggz32 » Sun Apr 27, 2025 4:37 am

I think this is related to the Attenuation setting, the old code accepted 11db, the new wants 12db.
regards

Who is online

Users browsing this forum: Amazon [Bot], PerplexityBot, Semrush [Bot] and 18 guests