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=๏ผˆint๏ผ‰TargetV/3.3Vโˆ—255DAC=๏ผˆint๏ผ‰TargetV/3.3Vโˆ—255

The demo is as follows:

#define DAC1 25 

void setup() {  
}

void loop() {
  
  // 8bit DAC, 255 = 3.3V, 0 = 0.0V 
  for(int i = 0; i < 255; i++){
    dacWrite(DAC1, i);
    delay(10);
  }
}

Last updated