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 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 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.
- Return
ESP_OK if success
- Parameters
channel
: Channel to get the gpio numbergpio_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 channeldac_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_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
ESP_ERR_INVALID_ARG The parameter is NULL.
- 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.
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
Enumerations¶
-
enum
dac_channel_t
¶ Values:
-
DAC_CHANNEL_1
= 0¶ DAC channel 1 is GPIO25(ESP32) / GPIO17(ESP32S2)
-
DAC_CHANNEL_2
= 1¶ DAC channel 2 is GPIO26(ESP32) / GPIO18(ESP32S2)
-
DAC_CHANNEL_MAX
¶
-