4.Digital-Analog Converter
The usage of DAC
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=intTargetV/3.3V255DAC=(int)TargetV/3.3V∗255
The demo is as follows:
1
#define DAC1 25
2
3
void setup() {
4
}
5
6
void loop() {
7
8
// 8bit DAC, 255 = 3.3V, 0 = 0.0V
9
for(int i = 0; i < 255; i++){
10
dacWrite(DAC1, i);
11
delay(10);
12
}
13
}
Copied!
Export as PDF
Copy link