Page 1 of 1

ADC settings bug

Posted: Fri May 01, 2020 11:03 am
by Damianvv

Code: Select all

from machine import ADC, Pin
import time

BATTERY_CHARGE_STATUS = 34
BATTERY_READOUT_PIN = 35

adc1 = ADC(Pin(BATTERY_CHARGE_STATUS))
adc1.width(ADC.WIDTH_10BIT)

adc2 = ADC(Pin(BATTERY_READOUT_PIN))
adc2.width(ADC.WIDTH_12BIT)

while True:
    value1 = adc1.read()
    value2 = adc2.read()
    print("Value1: {}, value2: {}".format(value1, value2))
    time.sleep(2)
In the above example, adc1 gets a width of 10 and adc2 gets a width of 12. When reading the values from these adc's, both will have a 12 bit width.
The last width declaration always decides the width of all adc's. Is this intended behaviour?

Micropython version: esp32-idf3-20191220-v1.12

Re: ADC settings bug

Posted: Fri May 01, 2020 2:02 pm
by Roberthh
In fact your code is not using two ADCs. If uses one ADC with two port pins multiplexed to it. Therefore you get the same ADC setting on both pins. So either you change the setting each time you change the pin, or you proceed with 12 bit and discard the lower bits if required. With the poor performance of the ESP32 ADC the difference should minor. According to tests made, the practical resolution is about 9 bit.

Re: ADC settings bug

Posted: Sat May 02, 2020 7:13 am
by JacquesC
I agree. I prefer to use an external ADS1015 or ADS1115.
It allows to have 4 analog inputs with choices of settings more advantageous than what the ESP32 would allow (differential inputs, programmable gain, sampling).
I directly introduced the converter into the connection cable.
And micropython libraries are accessible here : https://github.com/robert-hh/ads1x15
IMG_1206.JPG
IMG_1206.JPG (111.96 KiB) Viewed 4002 times

Re: ADC settings bug

Posted: Mon May 04, 2020 11:38 am
by Damianvv
Thank you for the information, glad to know it's intended behaviour.

Re: ADC settings bug

Posted: Mon May 04, 2020 4:50 pm
by T-Wilko
Roberthh wrote:
Fri May 01, 2020 2:02 pm
In fact your code is not using two ADCs. If uses one ADC with two port pins multiplexed to it. Therefore you get the same ADC setting on both pins. So either you change the setting each time you change the pin, or you proceed with 12 bit and discard the lower bits if required. With the poor performance of the ESP32 ADC the difference should minor. According to tests made, the practical resolution is about 9 bit.
I thought the esp32 had 2 (?) seperate ADC's?
If the OP simply chose a different GPIO (that was linked to the other ADC) for the second analogue input, wouldn't that be handled separately?

Re: ADC settings bug

Posted: Mon May 04, 2020 5:08 pm
by tve
The other ADC is not usable by applications if wifi is used.