class ADC – analog to digital conversion

The ADC class wraps a single analog-to-digital converter channel that samples a voltage on a pin (or one of the on-chip analog channels) and returns its discretised value.

Example usage:

from machine import ADC, Pin

adc = ADC(Pin("P6"))      # ADC channel on header pin P6 (PA5)
val = adc.read_u16()      # raw reading scaled to 0..65535

Constructors

class machine.ADC(id: int | str | Pin) ADC

Construct an ADC object for the analog source identified by id. The accepted forms are:

  • an integer channel number (018 on STM32, port- specific elsewhere);

  • a Pin object, or a board pin string such as "P6" – the pin must be analog-capable;

  • one of the internal-channel constants (CORE_TEMP, CORE_VREF, CORE_VBAT, VREF or CORE_VDD) to read the MCU’s internal sensors. STM32 only.

Methods

read_u16() int

Sample the analog channel once and return the result as an unsigned 16-bit integer (065535). Lower-resolution ADCs are left-aligned into the 16-bit range so the port-specific raw resolution is hidden.

read_uv() int

Sample the analog channel and return the result in microvolts. The reading is calibrated against the internal reference where hardware supports it. mimxrt port only.

Constants

The constants below are only available on the STM32 port; pass them as the id argument to construct an ADC that reads one of the on-chip analog sensors. The result of read_u16() is the channel’s raw 16-bit reading; for calibrated values use the helpers on pyb.ADCAll.

VREF: int

External voltage-reference channel.

CORE_VREF: int

Internal 1.21 V (nominal) voltage-reference channel (VREFINT).

CORE_TEMP: int

Internal die-temperature sensor channel.

CORE_VBAT: int

Backup-battery voltage channel (VBAT).

CORE_VDD: int

MCU supply-rail channel (VDDA).