Skip to content
loboris edited this page Jan 3, 2018 · 19 revisions

machine Module

Class ADC


This class includes full support for using ESP32 ADC peripheral
Functions are added to set the attenuation and to calibrate the ADC.

ESP32 ADC input voltage range depends on attenuation setting.

Attenuation Voltage range
0 dB 1.1 V
2.5 dB 1.5 V
6 dB 2.2 V
11 dB 3.9 V

Create the adc instance object

adc = machine.ADC(chan)

chan argument defines the ESP32 ADC channel and gpio which will will be used as adc input.

chan 0-3 will use gpio 36-39 as adc input
chan 4-7 will use gpio 32-35 as adc input
chan 8 (ADC.HALL) will use ESP32 Hall sensor as input. If Hall sensor is used, gpio#36 and gpio#39 cannot be used as adc inputs at the same time

Initially, the attenuation is set to 0 dB, and resolution to 12 bits.

adc.atten(value)

Set the attenuation value.

The following attenuation constants can be used for value:
ATTN_0DB - attenuation 0 dB (in / 1.0)
ATTN_2_5DB - attenuation 2.5 dB (in / 1.34)
ATTN_6DB - attenuation 6 dB (in / 2.0)
ATTN_11DB - attenuation 11 dB (in / 3.6)

adc.width(value)

Configure ADC capture width..

The following constants can be used for value:
WIDTH_9BIT - capture width is 9Bit
WIDTH_10BIT - capture width is 10Bit
WIDTH_11BIT - capture width is 11Bit
WIDTH_12BIT - capture width is 12Bit

adc.vref([refV], [pin])

Get or set ADC refference voltage and/or reference output pin.

The gain and offset factors of an ESP32 module's ADC are calculated using the reference voltage and the Gain and Offset curves provided in the lookup tables.
Nominal voltage refernce is 1100 mV, and can be adjusted to compensate for ESP32 chip differences.
Reference voltage can be set to refV (in range 1000 ~ 1200 mV).

The internal ESP32 reference can be roted to gpio to be measured and the value used to set the reference voltage for the specific ESP32 chip.
To route the reference voltage to the gpio set the argument pin to the gpio pin number.
Valid gpios are only 25, 26 and 27.

adc.read()

Read the ADC value as voltage (in mV)

Calibrated read is used.

adc.readraw()

Read the raw ADC value as integer value in a range 0 ~ 2^width-1

Clone this wiki locally