Class ADC – Analog-to-digital conversion¶
Create object¶
-
class
ADC.
ADC
(Pin)¶
Create an ADC object associated with a set pin. So you can read the analog value on that pin。
Pin
- ADC is available on dedicated pins, ESP32 available pins are:IO39、IO36、IO35、IO33、IO34、IO32. The ADC pins of the control board are P0、P1、P2、P3、P4、P10。
Detailed pin definitions ESP32 pins function list. and :ref:` mPython Board pins definition<mpython_pinout>` chapter。
Example:
from machine import ADC, Pin
adc = ADC(Pin(33)) # create an ADC object
Method¶
-
ADC.
read
()¶ Read ADC and return read result.
-
ADC.
atten
(db)¶ This method allows setting the amount of attenuation of the ADC input. This allows a wider range of possible input voltages, but at the expense of accuracy (the same number of bits now means a wider range). When atten() is not set, the default is 0DB attenuation. Possible attenuation options include:
db
-
ADC.
width
(bit)¶ Set the data width (resolution). The resolution of the ADC refers to the precision that can convert the collected analog signal into a digital signal. Usually, we use the “bit” to express, for example, 8 bit means that the ADC can correspond to the voltage signal within the specified range, respectively corresponding to - 2^8-1, that is, 256 digits of 0-255. The higher the resolution digits, the more accurate it can be expressed, and the less information is lost.
bit
- Width options are:Macro Definition Value Full Range ADC.WIDTH_9BIT
0 0x1ff(511) ADC.WIDTH_10BIT
1 0x3ff(1023) ADC.WIDTH_11BIT
2 0x7ff(2047) ADC.WIDTH_12BIT
3 00xfff(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)