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
Header File
Functions
Header File
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.
DAC_CHANNEL_1_GPIO_NUM
is the GPIO number of channel 1 (GPIO25);DAC_GPIO26_CHANNEL
is the channel number of GPIO 26 (channel 2).
Header File
Header File
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.
-
dac_channel_t en_ch
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
-
enumerator DAC_CHANNEL_1