This is the documentation for the latest development branch of MicroPython and may refer to features that are not available in released versions.

If you are looking for the documentation for a specific release, use the drop-down menu on the left and select the desired version.

class DAC – digital to analog conversion

The DAC is used to output an analog voltage based on a digital value.

The output voltage will be between 0 and 3.3V.

DAC is currently supported on ESP32 [1], SAMD and Renesas RA.

Note

The STM32 port has similar functionality to machine.DAC. See pyb.DAC for details.

Example usage (ESP32):

from machine import DAC

dac = DAC(pin)    # create a DAC object acting on a pin
dac.write(128)    # write a value to the DAC
dac.write(255)    # output maximum value, 3.3V

Constructors

class machine.DAC(id)

Construct a new DAC object.

id is a pin object (ESP32 and Renesas RA) or an index to a DAC resource (SAMD).

Note

On the ESP32, DAC functionality is available on pins 25 and 26. On the ESP32-S2, pins 17 and 18. See ESP32 Quickref for more details.

Note

SAMD21 has one DAC resource, SAMD51 has two. See SAMD Quickref for more details.

Methods

DAC.write(value)

Output an analog voltage to the pin connected to the DAC.

value is a representation of the desired output; a linear interpolation of 0-3.3V, though the range differs depending on the port and micro, see below:

Port/micro

Bits

Range

ESP32

8

0-255

SAMD21

10

0-1023

SAMD51

12

0-4095

Renesas RA

12

0-4095

Footnotes