0

I could not understanding why ESP-IDF voltage calculation if apply to convert the raw reading from GPIO39 would result in crash ?

uint32_t voltage = esp_adc_cal_raw_to_voltage(read_raw, adc_chars);

Below is the simple source code

 #include <stdio.h>
    #include <stdlib.h>
    #include "freertos/FreeRTOS.h"
    #include "freertos/task.h"
    #include "freertos/queue.h"
    #include "driver/gpio.h"
    #include "driver/adc.h"
    #include "driver/dac.h"
    #include "esp_system.h"
    #include "esp_adc_cal.h"
    #define BLINK_GPIO 2
    static const adc_atten_t atten = ADC_ATTEN_DB_11;
    static const adc_unit_t unit = ADC_UNIT_1;
    #define DEFAULT_VREF    1100
    static void Read_GPIO39() {
        int read_raw = 0;
        adc1_config_width(ADC_WIDTH_BIT_12);
        adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);
        read_raw=adc1_get_raw(ADC1_CHANNEL_3);
       adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));


esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF,adc_chars);
       **//uint32_t voltage = esp_adc_cal_raw_to_voltage(read_raw, adc_chars);**
        printf("Intensity : %d\n", read_raw);

        //printf("Voltage: %d\n", voltage);
        gpio_pad_select_gpio(BLINK_GPIO);
        gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
        gpio_set_level(BLINK_GPIO, 1);
    }


    void app_main() {
        ESP_ERROR_CHECK(nvs_flash_init());
        // initialise_wifi();
        while (1) {
            Read_GPIO39();

            vTaskDelay(3000 / portTICK_PERIOD_MS);
            gpio_set_level(BLINK_GPIO, 0);
        }


    }
Codo
  • 75,595
  • 17
  • 168
  • 206
  • Your code references `adc_chars` but that's not defined anywhere in the code you posted. Please post an example of the actual, complete but minimal code which demonstrates the crash. Also, what is the crash/bootloader message? – romkey Apr 12 '19 at 14:57
  • adc_chars added , voltage reading still 0?? – TSG anti SO dark forces Apr 12 '19 at 15:14
  • So this code doesn't crash, it just gives you a result you don't like? You didn't actually set `adc_chars` to anything in your code, you just allocated it... of course you're getting a weird result. Have you actually read the documentation on `esp_adc_cal_raw_to_voltage()`? – romkey Apr 12 '19 at 15:19
  • esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF,adc_chars); may help to compute voltage , however the actual voltage measure using the digital-multimeter show very much lower different reading otherwise? – TSG anti SO dark forces Apr 12 '19 at 15:47

1 Answers1

0

With the two statements below , voltage reading could display with a variance of +/-7% deviation from the actual voltage reading, raw reading max =4095 or 2^12 bits ~= 3.3v .The range seemed to be within the region tested and documented before by others .

static const adc_atten_t atten = ADC_ATTEN_DB_11;
    static const adc_unit_t unit = ADC_UNIT_1;
    #define DEFAULT_VREF    1100
 adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));

 esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF,adc_chars);