Digital To Analog Converter

Overview

ESP32 has two 8-bit DAC (digital to analog converter) channels, connected to GPIO25 (Channel 1) and GPIO26 (Channel 2).

The DAC driver allows these channels to be set to arbitrary voltages.

The DAC channels can also be driven with DMA-style written sample data, via the I2S driver when using the “built-in DAC mode”.

For other analog output options, see the Sigma-delta Modulation module and the LED Control module. Both these modules produce high frequency PWM output, which can be hardware low-pass filtered in order to generate a lower frequency analog output.

Application Example

Setting DAC channel 1 (GPIO 25) voltage to approx 0.78 of VDD_A voltage (VDD * 200 / 255). For VDD_A 3.3V, this is 2.59V:

#include <driver/dac.h>

...

    dac_output_enable(DAC_CHANNEL_1);
    dac_output_voltage(DAC_CHANNEL_1, 200);

API Reference

Functions

esp_err_t dac_pad_get_io_num(dac_channel_t channel, gpio_num_t *gpio_num)

Get the gpio number of a specific DAC channel.

Return
  • ESP_OK if success
Parameters
  • channel: Channel to get the gpio number
  • gpio_num: output buffer to hold the gpio number

esp_err_t dac_output_voltage(dac_channel_t channel, uint8_t dac_value)

Set DAC output voltage. DAC output is 8-bit. Maximum (255) corresponds to VDD3P3_RTC.

Note
Need to configure DAC pad before calling this function. DAC channel 1 is attached to GPIO25, DAC channel 2 is attached to GPIO26
Return
  • ESP_OK success
Parameters
  • channel: DAC channel
  • dac_value: DAC output value

esp_err_t dac_output_enable(dac_channel_t channel)

DAC pad output enable.

Note
DAC channel 1 is attached to GPIO25, DAC channel 2 is attached to GPIO26 I2S left channel will be mapped to DAC channel 2 I2S right channel will be mapped to DAC channel 1
Parameters
  • channel: DAC channel

esp_err_t dac_output_disable(dac_channel_t channel)

DAC pad output disable.

Note
DAC channel 1 is attached to GPIO25, DAC channel 2 is attached to GPIO26
Return
  • ESP_OK success
Parameters
  • channel: DAC channel

esp_err_t dac_i2s_enable(void)

Enable DAC output data from I2S.

Return
  • ESP_OK success

esp_err_t dac_i2s_disable(void)

Disable DAC output data from I2S.

Return
  • ESP_OK success

esp_err_t dac_cw_generator_enable(void)

Enable cosine wave generator output.

Return
  • ESP_OK success

esp_err_t dac_cw_generator_disable(void)

Disable cosine wave generator output.

Return
  • ESP_OK success

esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)

Config the cosine wave generator function in DAC module.

Return
  • ESP_OK success
Parameters
  • cw: Configuration.

GPIO Lookup Macros

Some useful macros can be used to specified the GPIO number of a DAC channel, or vice versa. e.g.

  1. DAC_CHANNEL_1_GPIO_NUM is the GPIO number of channel 1 (25);
  2. DAC_GPIO26_CHANNEL is the channel number of GPIO 26 (channel 2).

Macros

DAC_GPIO25_CHANNEL
DAC_CHANNEL_1_GPIO_NUM
DAC_GPIO26_CHANNEL
DAC_CHANNEL_2_GPIO_NUM