-
Notifications
You must be signed in to change notification settings - Fork 344
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 |
adc = machine.ADC(pin)
pin argument defines the gpio which will will be used as adc input.
It can be given as integer pin number, or machine.Pin(n) object.
Only GPIOs 32-39 can be used as ADC inputs.
use machine.ADC.HALL
constant to select 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.
import machine
adc=machine.ADC(36)
adc
# prints: ADC(Pin(36): width=12 bits, atten=0dB (1.1V), Vref=1100 mV)
Set the attenuation value.
The following attenuation constants can be used for value:
ATTN_0DB
- attenuation 0 dB (range: 0 - 1.1 V)
ATTN_2_5DB
- attenuation 2.5 dB (range 0 - 1.5 V)
ATTN_6DB
- attenuation 6 dB (range: 0 - 2.5 V)
ATTN_11DB
- attenuation 11 dB (range: 0 - 3.9 V'
adc.atten(adc.ATTN_11DB)
adc
# prints: ADC(Pin(36): width=12 bits, atten=11dB (3.9V), Vref=1100 mV)
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.width(adc.WIDTH_10BIT)
adc
# prints: ADC(Pin(36): width=10 bits, atten=11dB (3.9V), Vref=1100 mV)
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.
Returns tuple containing the current refference voltage in mV and selected output pin.
# Get the reference voltage
machine.ADC.vref()
(1100, 0)
# Route the internal reference voltage to GPIO#25
machine.ADC.vref(vref_topin=25)
# You can now measure the voltage on GPIO#25 with the voltmeter
# Set the reference voltage to measured value in mV
# This value will be used to get more precise readings
machine.ADC.vref(vref=1096)
(1096, 0)
Read the ADC value as voltage (in mV)
Calibrated read is used.
Read the raw ADC value as integer value in a range 0 ~ 2^width-1