ADC legacy vs OneShot
Posted: 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):
This gives something like the following and it reacts properly to temperature changes:
But since I get the following warning when compiling:
I am trying to migrate the code so I wrote this:
But the result is always all bits set to 1 (511, since it's 9 bits width):
I am following this example but I cannot see what's wrong,
Any ideas?
Thank you.
I have the following code that works perfectly fine to read the raw value from a temperature sensor (LM35DZ):
- #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:
- #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.