Page 1 of 1

convert ADC readings

Posted: Fri Nov 03, 2017 2:04 am
by newsettler_AI
Hi,

I'm trying to measure input voltage by adc1 in range 0 - 3,3 V.

This is how I expected to convert raw values to voltage:

adc_input_voltage = (Vref * adc_RAW ) / 4096

and here is part code & settings:

Code: Select all

float convert_adc_to_voltage(uint32_t adc_val)
{
	float adc_volt = (3.3 * adc_val) / 4096;
	return adc_volt;
}

void adc_task(void *pvParameter)
{
    // initialize ADC
    adc1_config_width(ADC_WIDTH_12Bit);
    adc1_config_channel_atten(ADC_CHANNEL,ADC_ATTEN_11db);

    uint32_t adc_value;
    float adc_voltage;
    while(1)
    {
		adc_value = adc1_get_raw((ADC_CHANNEL));
		adc_voltage = convert_adc_to_voltage(adc_value);
		printf("ADC(GPIO34):%d(raw), %.3f (V)\n",adc_value, adc_voltage);
		vTaskDelay(1000/portTICK_PERIOD_MS);
	
		// output example (real input voltage is 0,75V): 
		// ADC(GPIO34):724(raw), 0.583 (V)
		// ADC(GPIO34):726(raw), 0.585 (V)
    }
}

My conversion is not correct. I studied https://github.com/espressif/esp-idf/bl ... iver/adc.h and was confused with ADC_ATTEN_DB_11 options:
1.
input voltage of ADC will be reduced to about 1/3.6
2.
(ADC_ATTEN_11db) gives full-scale voltage 3.9V
3.
11dB attenuation the maximum voltage is limited by VDD_A, not the full scale voltage
Let say, I set 0,75 V to adc pin as I tried before.

1) does this value will be reduced by 1/3,6 as noted in first note? So, actual input voltage would be decreased to 0,208 V to be able compare it with Vref (which is 1,0 ... 1,1 V)?
2) actual full scale would be 0 ... 3,3 V for ADC_ATTEN_11db option? Can I use formula that I mentioned at start and assume Vref = 3,3 ?
3) how can I calculate adc voltage without using adc1_to_voltage function?

Re: convert ADC readings

Posted: Fri Nov 03, 2017 2:42 am
by WiFive
newsettler_AI wrote:how can I calculate adc voltage without using adc1_to_voltage function?
Why would you want to? That function corrects for nonlinearity and should give the best result.

Re: convert ADC readings

Posted: Fri Nov 03, 2017 9:21 am
by newsettler_AI
WiFive wrote:
newsettler_AI wrote:how can I calculate adc voltage without using adc1_to_voltage function?
Why would you want to? That function corrects for nonlinearity and should give the best result.
At first, this function dont show 0 when I short adc pin to ground, its about 30-70 mV. So I need to set some rule for adc_raw = 0.
At second, I need reverse compatiable function with older idf version.