Page 1 of 1

ESP32 ADC Range

Posted: Sat Jun 24, 2017 10:02 pm
by rdagger
I read the ESP32 ADC voltage range can be set in firmware to either 0-1V, 0-1.4V, 0-2V, or 0-4V.
Can you change the range in code or does it have to be set when the firmware is built?
Does anyone know the default range of the MicroPython.org daily builds?
I'm guessing it is 0-1.4V with a 12bit resolution (0-4095).

Re: ESP32 ADC Range

Posted: Sun Jun 25, 2017 6:25 am
by Roberthh
Once you created an adc object, you can call adc.atten() to change the range, and adc.width to change the bit size e.g.:

Code: Select all

import machine
adc = machine.ADC(machine.Pin(36)
adc.atten(adc.ATTN_6DB)
This sets the range to 0..2 V. You can also add arguments as atten=value and width=value to the constructor. Do a dir(machine.ADC) to see the possible values. As far as I can recall, only ADC1 is supported at the moment.
Note: from my tests I can say that the ESP32 ADC is lousy, very noisy and non-linear. If you have a need for precision, use an external ADC.

Re: ESP32 ADC Range

Posted: Tue Jun 27, 2017 6:13 am
by rdagger
Thanks for the detailed response!

Re: ESP32 ADC Range

Posted: Mon May 10, 2021 9:37 pm
by MatthiasP
Hello all,

https://docs.espressif.com/projects/esp ... alibration
provides very useful information regarding the ESP32 ADCs' limitations regarding noise, input voltage range and calibration.
For esp-edf, ADC calibration and conversion routines can be found here:
https://github.com/espressif/esp-idf/bl ... al_esp32.c

I have partly ported the conversion routine using the Vref (voltage reference) calibration value to MicroPython:
https://github.com/matthias-bs/MicroPython-ADC_Cal

The code is currently limited to
  • ADC1
  • 10 bits width
  • 6 dB attenuation
  • Vref calibration only (as opposed to Two-Point calibration)
Also the chip-specific Vref value has to be provided manually. It can be read as follows:

Code: Select all

$ espefuse.py --port <port> adc_info
Hope this helps!

Best regards,
MatthiasP

Re: ESP32 ADC Range

Posted: Tue May 11, 2021 8:30 pm
by MatthiasP
Added internal reading of Vref efuse calibration value to
https://github.com/matthias-bs/MicroPython-ADC_Cal.

Re: ESP32 ADC Range

Posted: Fri May 14, 2021 10:47 am
by MatthiasP
https://github.com/matthias-bs/MicroPython-ADC_Cal now supports all ESP32 ADC1 bit widths and 0/2.5/6 dB attenuation.

Re: ESP32 ADC Range

Posted: Fri May 14, 2021 7:07 pm
by davef
Thanks for providing this improvement, I will try it out next week.

Regarding your comment about noise, every minute and for one second I take 10 samples and average them. Then over a 10 minute period I do a moving average. Good when looking for relatively long-term changes.

Re: ESP32 ADC Range

Posted: Wed Jul 21, 2021 7:15 am
by aleskeyfedorovich
Roberthh wrote:
Sun Jun 25, 2017 6:25 am

Code: Select all

import machine
adc = machine.ADC(machine.Pin(36)
adc.atten(adc.ATTN_6DB)
I think there's a typo:

Code: Select all

adc.atten(machine.ADC.ATTN_6DB)