Digital To Analog Converter (DAC)

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 by the digital controller, 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 PDM/PWM output, which can be hardware low-pass filtered in order to generate a lower frequency analog output.

Application Example

Setting DAC channel 1 (GPIO25) 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_i2s_enable(void)

Enable DAC output data from I2S.

Returns

  • ESP_OK success

esp_err_t dac_i2s_disable(void)

Disable DAC output data from I2S.

Returns

  • ESP_OK success

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.

Parameters
  • channel – Channel to get the gpio number

  • gpio_num – output buffer to hold the gpio number

Returns

  • ESP_OK if success

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

Parameters
  • channel – DAC channel

  • dac_value – DAC output value

Returns

  • ESP_OK success

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

Parameters

channel – DAC channel

Returns

  • ESP_OK success

esp_err_t dac_cw_generator_enable(void)

Enable cosine wave generator output.

Returns

  • ESP_OK success

esp_err_t dac_cw_generator_disable(void)

Disable cosine wave generator output.

Returns

  • ESP_OK success

esp_err_t dac_cw_generator_config(dac_cw_config_t *cw)

Config the cosine wave generator function in DAC module.

Parameters

cw – Configuration.

Returns

  • ESP_OK success

  • ESP_ERR_INVALID_ARG The parameter is NULL.

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 (GPIO25);

  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

Structures

struct dac_cw_config_t

Config the cosine wave generator function in DAC module.

Public Members

dac_channel_t en_ch

Enable the cosine wave generator of DAC channel.

dac_cw_scale_t scale

Set the amplitude of the cosine wave generator output.

dac_cw_phase_t phase

Set the phase of the cosine wave generator output.

uint32_t freq

Set frequency of cosine wave generator output. Range: 130(130Hz) ~ 55000(100KHz).

int8_t offset

Set the voltage value of the DC component of the cosine wave generator output. Note: Unreasonable settings can cause waveform to be oversaturated. Range: -128 ~ 127.

Enumerations

enum dac_channel_t

Values:

enumerator DAC_CHANNEL_1

DAC channel 1 is GPIO25(ESP32) / GPIO17(ESP32S2)

enumerator DAC_CHANNEL_2

DAC channel 2 is GPIO26(ESP32) / GPIO18(ESP32S2)

enumerator DAC_CHANNEL_MAX
enum dac_cw_scale_t

The multiple of the amplitude of the cosine wave generator. The max amplitude is VDD3P3_RTC.

Values:

enumerator DAC_CW_SCALE_1

1/1. Default.

enumerator DAC_CW_SCALE_2

1/2.

enumerator DAC_CW_SCALE_4

1/4.

enumerator DAC_CW_SCALE_8

1/8.

enum dac_cw_phase_t

Set the phase of the cosine wave generator output.

Values:

enumerator DAC_CW_PHASE_0

Phase shift +0°

enumerator DAC_CW_PHASE_180

Phase shift +180°