DAC

About

DAC (digital to analog converter) is a very common peripheral used to convert a digital signal to an analog form.

ESP32 and ESP32-S2 have two 8-bit DAC channels, and ESP32-S31 has two 12-bit DAC channels. The DAC driver allows these channels to be set to arbitrary voltages.

DACs can be used for generating a specific (and dynamic) reference voltage for external sensors, controlling transistors, etc.

ESP32 SoC

DAC_1 pin

DAC_2 pin

ESP32

GPIO 25

GPIO 26

ESP32-S2

GPIO 17

GPIO 18

ESP32-S31

GPIO 4

GPIO 5

Arduino-ESP32 DAC API

dacWrite

This function is used to set the DAC value for a given pin/DAC channel.

bool dacWrite(uint8_t pin, uint16_t value);
  • pin GPIO pin.

  • value to be set. Default range is 0 - 255 (8-bit, equals 0 V - 3.3 V) and can be changed with dacWriteResolution(). Values are scaled to the SoC’s native DAC resolution (8-bit on ESP32 / ESP32-S2, 12-bit on ESP32-S31).

dacWriteResolution

This function is used to set the input resolution in bits for dacWrite().

void dacWriteResolution(uint8_t bits);
  • bits resolution in bits (1 - 16). Default is 8 (0 - 255). Set to 12 on ESP32-S31 for full native 12-bit control (0 - 4095).

dacDisable

This function is used to disable DAC output on a given pin/DAC channel.

bool dacDisable(uint8_t pin);
  • pin GPIO pin.