Where DACs are used
- Audio playback and tone generation
- Programmable voltage references and setpoints
- Waveform and function generation
- Control signals for analogue circuits
- Instrumentation and data acquisition systems
Digital systems work with numbers and logic states. The physical world responds to voltage and current. Learn how digital values become analogue signals, then generate and measure useful waveforms.
A digital-to-analogue converter (DAC) maps a digital number to a corresponding analogue output voltage or current. The digital input has a finite number of possible codes. The output is therefore made up of discrete levels, even when it is used to approximate a continuously changing signal.
An ADC measures an analogue input and reports a digital code. A DAC takes a digital code and produces an analogue output. Many microcontrollers provide ADC inputs, but that does not mean they also have a true DAC output.
A PWM pin is a digital switching output. With suitable filtering it can approximate an analogue level, but it is not automatically a true DAC.
For an N-bit DAC there are 2N possible codes. An 8-bit converter has 256 codes, from 0 to 255. More bits provide smaller nominal steps, but actual accuracy also depends on reference quality, linearity, noise, output loading and the converter's specifications.
For a common ideal unipolar DAC transfer, VOUT = code × VREF / 2N. The highest code is one LSB below VREF. Some devices define full-scale and endpoint behaviour differently; read the datasheet transfer function before making precision calculations.
Illustrative ideal transfer function. Real devices have offset, gain and linearity errors.
An R-2R ladder uses repeated R and 2R resistor sections to create binary-weighted contributions. Each digital input switches its branch to a defined logic voltage or ground. The contributions combine to form an output voltage proportional to the input code.
Ideal 4-bit R-2R model: VOUT = VCC × code / 16, so the highest code is one LSB below VCC. A physical ladder also depends on termination, resistor ratios, switch resistance and loading. Buffer the output before driving a load.
PWM rapidly switches a digital pin between LOW and HIGH. Changing its duty cycle changes the average value. A low-pass RC filter attenuates the switching carrier and leaves a smoother voltage. The result still contains ripple and responds more slowly when the filter is made stronger.
For example, 50% duty from a 5 V logic output has an ideal average near 2.5 V before considering loading and switching losses. The filter's time constant τ = R × C controls how quickly the output follows changes. A larger time constant reduces ripple but slows response.
First-order ideal RC estimate. Actual ripple depends on PWM frequency, load, pin characteristics and filter topology. Choose a cutoff well below the PWM carrier and consider the signal bandwidth you need.
A waveform generator repeatedly sends a sequence of digital samples to a DAC. The sample rate determines how often the output is updated. The waveform frequency depends on sample rate and the number of samples used per cycle.
Ideal sampled waveform display. Real output quality depends on DAC settling, sample timing, reconstruction filtering, reference stability and load.
Microcontroller boards differ. The classic Arduino Uno R3 has PWM outputs but no built-in true DAC. An external DAC module or an RC-filtered PWM output can be used depending on the required quality. Some ESP32 chips include a DAC, while several newer variants do not.
| Board / chip | On-chip DAC | Notes |
|---|---|---|
| Arduino Uno R3 (ATmega328P) | No | PWM is available; external DAC or filtered PWM can be used. |
| Original ESP32 | Yes, 2 × 8-bit | GPIO25 and GPIO26 on the original ESP32 chip/module family. |
| ESP32-S2 | Yes, 2 × 8-bit | GPIO17 and GPIO18. |
| ESP32-S3 / C3 / C6 | No general-purpose DAC peripheral | Use PWM with filtering or an external DAC; check exact chip documentation. |
Board variants, modules and development boards can expose different pins. Confirm the exact SoC and board schematic. The Arduino-ESP32 API documents dacWrite() for supported DAC pins; the output is not a precision reference and is limited by the chip's supply and electrical characteristics.
Important: the phrase “ESP32 board” is not enough to identify DAC support. Check the chip marking and the official pin/peripheral documentation before wiring or compiling code.
This activity uses a supported original ESP32 board with DAC output on GPIO25. The sketch sends a 64-sample sine lookup table to the DAC at a fixed interval. With a 500 µs sample interval, the nominal sample rate is 2 kHz and the waveform frequency is about 31.25 Hz.
Use the ground clip only on the circuit's common ground. Do not attach it to an unknown or mains-referenced point.
#include <Arduino.h>
#include <math.h>
const int DAC_PIN = 25; // Original ESP32 DAC1
const unsigned int SAMPLE_US = 500; // 2,000 samples per second
const int TABLE_SIZE = 64;
uint8_t sineTable[TABLE_SIZE];
void setup() {
for (int i = 0; i < TABLE_SIZE; i++) {
float angle = 2.0f * PI * i / TABLE_SIZE;
float sample = 127.5f + 120.0f * sinf(angle);
sineTable[i] = (uint8_t)constrain((int)lroundf(sample), 0, 255);
}
}
void loop() {
static int index = 0;
static unsigned long nextSample = micros();
// Simple timing demonstration, not a precision DDS engine.
if ((long)(micros() - nextSample) >= 0) {
dacWrite(DAC_PIN, sineTable[index]);
index = (index + 1) % TABLE_SIZE;
nextSample += SAMPLE_US;
}
}
Expected result: a low-frequency stepped sine-like waveform centred near half the DAC range. Actual amplitude and offset depend on the chip, board, load and output characteristics. The simple loop can show timing jitter; it is a teaching sketch, not a precision signal generator.
Compare the true DAC example with an Uno PWM output followed by a passive RC low-pass filter. Use an LED or a high-impedance meter/scope input to observe changes. The output is a smoothed PWM average, not a true DAC conversion.
With 10 kΩ and 10 µF, τ is about 0.1 s and the ideal cutoff is about 1.59 Hz. This is a deliberately slow demonstration filter.
const int PWM_PIN = 9;
void setup() {
pinMode(PWM_PIN, OUTPUT);
}
void loop() {
for (int duty = 0; duty <= 255; duty++) {
analogWrite(PWM_PIN, duty);
delay(10);
}
for (int duty = 255; duty >= 0; duty--) {
analogWrite(PWM_PIN, duty);
delay(10);
}
}Observe how the filtered voltage rises and falls more smoothly than the raw PWM waveform. Try a smaller capacitor, then compare ripple and response time. The Uno's PWM carrier frequency and pin behaviour depend on its timer configuration and board variant.
Safety: these are extra-low-voltage experiments only. Never connect a grounded oscilloscope probe to household mains, an inverter output or an unknown floating circuit. A typical bench oscilloscope ground clip is connected to protective earth. Use an appropriate isolated low-voltage source, verify grounding, and do not exceed the microcontroller pin's voltage/current limits. Do not connect the DAC directly to a speaker, motor or other low-impedance load.
Explore these open lessons at your own pace. For personalised explanations, project-specific troubleshooting, guided practical training or masterclasses, EazyLife offers paid support tailored to your learning goals.
Free or sponsored lesson referrals may be available through active programmes; they are not guaranteed.