ESP32 ADC Range

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

ESP32 ADC Range

Post by rdagger » Sat Jun 24, 2017 10:02 pm

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).

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

Re: ESP32 ADC Range

Post by Roberthh » Sun Jun 25, 2017 6:25 am

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.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: ESP32 ADC Range

Post by rdagger » Tue Jun 27, 2017 6:13 am

Thanks for the detailed response!

MatthiasP
Posts: 7
Joined: Mon Apr 19, 2021 7:31 pm

Re: ESP32 ADC Range

Post by MatthiasP » Mon May 10, 2021 9:37 pm

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

MatthiasP
Posts: 7
Joined: Mon Apr 19, 2021 7:31 pm

Re: ESP32 ADC Range

Post by MatthiasP » Tue May 11, 2021 8:30 pm

Added internal reading of Vref efuse calibration value to
https://github.com/matthias-bs/MicroPython-ADC_Cal.

MatthiasP
Posts: 7
Joined: Mon Apr 19, 2021 7:31 pm

Re: ESP32 ADC Range

Post by MatthiasP » Fri May 14, 2021 10:47 am

https://github.com/matthias-bs/MicroPython-ADC_Cal now supports all ESP32 ADC1 bit widths and 0/2.5/6 dB attenuation.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP32 ADC Range

Post by davef » Fri May 14, 2021 7:07 pm

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.

aleskeyfedorovich
Posts: 28
Joined: Mon Aug 27, 2018 4:43 pm

Re: ESP32 ADC Range

Post by aleskeyfedorovich » Wed Jul 21, 2021 7:15 am

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)

Post Reply