The purpose of the DAC is the opposite of that of the ADC. The DAC converts a digital signal into an analog signal for output.
Remember the music when NyBoard is turned on? It is using PWM to make music sound which uses high-speed switching to adjust the duty cycle to output voltage.
Compared with PWM, the DAC will directly output the voltage without calculating the duty cycle. ESP32 integrates a 2-channel 8-bit DAC with a value of 0-255. The voltage range is 0-3.3V. Therefore, the formula for calculating the output voltage of the DAC is as follows:
DAC=(int)TargetV/3.3V∗255
The demo is as follows:
#defineDAC125voidsetup() { }voidloop() {// 8bit DAC, 255 = 3.3V, 0 = 0.0V for(int i =0; i <255; i++){dacWrite(DAC1, i);delay(10); }}