ADC settings bug

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Damianvv
Posts: 5
Joined: Mon Feb 17, 2020 12:52 pm

ADC settings bug

Post by Damianvv » Fri May 01, 2020 11:03 am

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

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ADC settings bug

Post by Roberthh » 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.

User avatar
JacquesC
Posts: 16
Joined: Sun Feb 17, 2019 5:04 am

Re: ADC settings bug

Post by JacquesC » Sat May 02, 2020 7:13 am

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 3989 times

Damianvv
Posts: 5
Joined: Mon Feb 17, 2020 12:52 pm

Re: ADC settings bug

Post by Damianvv » Mon May 04, 2020 11:38 am

Thank you for the information, glad to know it's intended behaviour.

User avatar
T-Wilko
Posts: 30
Joined: Thu Sep 19, 2019 8:08 am

Re: ADC settings bug

Post by T-Wilko » Mon May 04, 2020 4:50 pm

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?

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: ADC settings bug

Post by tve » Mon May 04, 2020 5:08 pm

The other ADC is not usable by applications if wifi is used.

Post Reply