class ADC – analog-to-digital conversion

The ADC class provides an interface to an analog-to-digital converter, representing a single endpoint that can sample a continuous voltage and convert it to a discrete value.

For additional control over ADC sampling, see machine.ADCBlock.

Example usage:

from machine import ADC

adc = ADC(pin)        # Create an ADC object acting on a pin
val = adc.read_u16()  # Read an analog value in the range 0-65535
val = adc.read_uv()   # Read an analog value in microvolts

Constructors

class machine.ADC(id, *, sample_ns, atten)

Access the ADC associated with the source identified by id. This id can be an integer (usually specifying a channel number), a Pin object, or other value supported by the underlying machine.

If additional keyword arguments are provided, they configure various aspects of the ADC. If not provided, these settings take their previous or default values. The settings include:

  • sample_ns is the sampling time in nanoseconds.

  • atten specifies the input attenuation.

The available ESP32 pins are: IO39, IO36, IO35, IO33, IO34, IO32. The mPython Board ADC pins are P0, P1, P2, P3, P4, P10.

For detailed pin definitions, see ESP32 pin function table..

Methods

ADC.init(*, sample_ns, atten)

Apply the given settings to the ADC. Only the specified parameters are changed. For a description of the parameters, see the ADC constructor above.

ADC.block()

Return the ADCBlock instance associated with this ADC object. This method exists only if the port supports the ADCBlock class.

ADC.read_u16()

Perform an analog read and return an integer in the range 0-65535. The returned value represents the raw reading taken by the ADC, scaled so the minimum value is 0 and the maximum value is 65535.

ADC.read_uv()

Perform an analog read and return an integer value in microvolts. The specific port determines whether this value is calibrated and how.

ESP32-specific methods

ADC.read()

Read the ADC and return the result.

ADC.atten(db)

This method allows setting the attenuation level of the ADC input. This allows a wider range of possible input voltages, but at the cost of precision (the same number of bits now represents a wider range). When atten() is not set, the default value is 0DB attenuation. The possible attenuation options include:

  • db

    Macro

    Attenuation

    Value

    Full-scale voltage

    ADC.ATTN_0DB

    0dB attenuation

    0

    1V

    ADC.ATTN_2_5DB

    2.5dB attenuation

    1

    1.5V

    ADC.ATTN_6DB

    6dB attenuation

    2

    2V

    ADC.ATTN_11DB

    11dB attenuation

    3

    3.3V

ADC.width(bit)

Set the data width (resolution). The resolution of an ADC refers to the precision with which it can convert the collected analog signal into 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 digital values. A higher resolution in bits means a more precise representation and less information loss.

  • bit - the width options:

    Macro

    Value

    Full-scale

    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 the 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