Class ADC – Analog-to-Digital Conversion
Constructors
- class ADC.ADC(Pin)
Create an ADC object associated with the specified pin. This allows you to read analog values on that pin.
Pin- ADC is available on dedicated pins. ESP32 available pins: IO39, IO36, IO35, IO33, IO34, IO32. mPython Board ADC pins: P0, P1, P2, P3, P4, P10.
For detailed pin definitions, see ESP32 Pinout Table. and mPython Board Pin Definition section.
Example:
from machine import ADC, Pin
adc = ADC(Pin(33)) # create an ADC object
Methods
- ADC.read()
Read the ADC and return the result.
- ADC.atten(db)
This method allows setting the attenuation of the ADC input. This allows a wider possible input voltage range, but at the cost of precision (the same number of bits now represents a wider range). Without setting atten(), the default is 0dB attenuation. Possible attenuation options include:
dbMacro Definition
Attenuation
Value
Full-scale Voltage
ADC.ATTN_0DB0dB
0
1V
ADC.ATTN_2_5DB2.5dB
1
1.5V
ADC.ATTN_6DB6dB
2
2V
ADC.ATTN_11DB11dB
3
3.3V
- ADC.width(bit)
Set the data width (resolution). ADC resolution refers to the precision with which the collected analog signal can be converted to a digital signal, usually expressed in “bits”. For example, 8 bits means the ADC can map the voltage signal within the specified range to 0 - 2^8-1, i.e. 0-255, which is 256 digital values. The higher the resolution bits, the more precise the representation and the less information is lost.
bit- Width options:Macro Definition
Value
Full-scale
ADC.WIDTH_9BIT0
0x1ff(511)
ADC.WIDTH_10BIT1
0x3ff(1023)
ADC.WIDTH_11BIT2
0x7ff(2047)
ADC.WIDTH_12BIT3
0xfff(4095)
Example:
from machine import ADC, Pin
adc = ADC(Pin(34)) # create an ADC object
adc.atten(adc.ATTN_11DB) # set 3.3V Range
x = adc.read()
print(x)
Constants
Attenuation
- ADC.ATTN_0DB
- ADC.ATTN_2_5DB
- ADC.ATTN_6DB
- ADC.ATTN_11DB
Data Width
- ADC.WIDTH_9BIT
- ADC.WIDTH_10BIT
- ADC.WIDTH_11BIT
- ADC.WIDTH_12BIT