ADC legacy vs OneShot

rocotocloc
Posts: 13
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):
  1. #include "driver/adc.h"
  2.  
  3. void water_probe_temperature_test_legacy(void *pvParameters)
  4. {
  5.     int adc_raw;
  6.  
  7.     ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_9));
  8.  
  9.     while (1)
  10.     {
  11.         adc_raw = adc1_get_raw(ADC_CHANNEL_3);
  12.         printf("water probe raw data: %d\n", adc_raw);
  13.         vTaskDelay(pdMS_TO_TICKS(1000));
  14.     }
  15. }

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:

  1. #include "esp_adc/adc_oneshot.h"
  2.  
  3. void water_probe_temperature_test_new_driver(void *pvParameters)
  4. {
  5.     int adc_raw;
  6.  
  7.     adc_oneshot_unit_handle_t adc1_handle;
  8.     adc_oneshot_unit_init_cfg_t init_config1 = {
  9.         .unit_id = ADC_UNIT_1,
  10.     };
  11.     ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
  12.  
  13.     adc_oneshot_chan_cfg_t config = {
  14.         .bitwidth = ADC_BITWIDTH_9,
  15.     };
  16.     ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_3, &config));
  17.  
  18.     while (1)
  19.     {
  20.         ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_3, &adc_raw));
  21.         printf("water probe raw data: %d\n", adc_raw);
  22.         vTaskDelay(pdMS_TO_TICKS(1000));
  23.     }
  24.  
  25.     ESP_ERROR_CHECK(adc_oneshot_del_unit(adc1_handle));
  26. }

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: 13
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));
}

Who is online

Users browsing this forum: Bing [Bot], 快乐的小尾巴 and 85 guests