EAZYLife STEM LAB · MODULE 27

DAC and Signal Generation

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.

Free open lessonDigital-to-analogue conversionPractical signal generation
CODE · VOLTAGE · WAVEFORM
01 · FROM DIGITAL TO ANALOGUE

What does a DAC actually do?

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.

Digital code
0000 0000
DAC
Analogue output
Voltage

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

DAC is not the same as ADC

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.

VoltageTime / codeLevel 1Level 2Level 3Level 4Level 5Ideal trend
Each code maps to one output level. A reconstruction filter can smooth rapid changes when the DAC is generating a sampled waveform.
02 · RESOLUTION AND QUANTISATION

How many output levels can you create?

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.

Number of codes = 2N
Ideal LSB step = VREF / 2N

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.

Explore DAC resolution

Code128 / 255
Ideal output1.657 V
Step size12.94 mV
Available levels256

Illustrative ideal transfer function. Real devices have offset, gain and linearity errors.

03 · R-2R RESISTOR LADDER

Build a DAC from two resistor values

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.

RRRR2R2R2R2R2RD3 · MSBD2D1D0 · LSBVOUTSwitches select VREF or GNDEach branch is controlled by one digital bit
Each digital bit contributes a weighted fraction. A practical ladder needs correct termination and a defined switch state for every input.

Why R-2R is useful

  • Only two nominal resistor values are required.
  • It can be extended to more bits.
  • It demonstrates binary weighting physically.
  • It can be built from discrete resistors for learning.

Why matching matters

  • Use closely matched resistors; ratio error affects linearity.
  • Output loading changes the result unless buffered.
  • GPIO output resistance and current limits matter.
  • Keep wiring short and use a stable logic supply.

Four-bit ladder code explorer

Binary code1111
Ideal output5.000 V
Ideal step0.333 V

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.

04 · PWM VERSUS A TRUE DAC

A filtered PWM signal can approximate an analogue level

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.

PWM output before filtering

HIGHLOW · Time →

After low-pass filtering

Smoothed averageResidual ripple · Time →
Ideal average voltage ≈ Duty cycle × VHIGH

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.

Explore a simple RC filter

Ideal average2.50 V
Time constant100 ms
Approx. cutoff1.59 Hz

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.

05 · WAVEFORMS AND SAMPLING

Generate more than a fixed voltage

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.

Common waveforms

  • Sine: smooth periodic waveform, common in audio and test signals.
  • Square: switches between two levels, useful for timing and digital tests.
  • Triangle: rises and falls linearly, useful for modulation and testing.
  • Sawtooth: ramps in one direction then resets, useful in sweep and oscillator circuits.

Important signal properties

  • Frequency: cycles per second, measured in hertz.
  • Amplitude: distance from centre to peak.
  • Offset: centre voltage relative to ground.
  • Peak-to-peak: total difference between minimum and maximum.
  • Sampling: discrete time points used to represent a changing signal.

Waveform generator model

VTime0 V3.3 V
Sample rate required640 samples/s
Ideal output range0.65–2.65 V
Quantisation12.94 mV / step

Ideal sampled waveform display. Real output quality depends on DAC settling, sample timing, reconstruction filtering, reference stability and load.

06 · MICROCONTROLLER DAC OPTIONS

Check the actual board, not just the family name

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 / chipOn-chip DACNotes
Arduino Uno R3 (ATmega328P)NoPWM is available; external DAC or filtered PWM can be used.
Original ESP32Yes, 2 × 8-bitGPIO25 and GPIO26 on the original ESP32 chip/module family.
ESP32-S2Yes, 2 × 8-bitGPIO17 and GPIO18.
ESP32-S3 / C3 / C6No general-purpose DAC peripheralUse 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.

07 · PRACTICAL BUILD A

Generate a sine wave with an original ESP32 DAC

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.

Components and connections

  • Original ESP32 development board with GPIO25 DAC
  • USB cable and Arduino IDE with ESP32 board support
  • Oscilloscope and ×10 probe, if available
  • Optional 1 kΩ series protection resistor for the output test lead
ESP32 GPIO25 (DAC1)Oscilloscope probe tipESP32 GNDOscilloscope ground clip

Use the ground clip only on the circuit's common ground. Do not attach it to an unknown or mains-referenced point.

Test procedure

  1. Confirm that the board is an original ESP32 variant with DAC on GPIO25.
  2. Upload the sketch below and allow it to run.
  3. Connect the scope tip to GPIO25 and the ground clip to ESP32 GND.
  4. Set the scope to DC coupling, suitable volts/div and about 10 ms/div.
  5. Observe the stepped sine approximation and adjust the timebase.
  6. Measure frequency, minimum, maximum and peak-to-peak voltage.
#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.

08 · PRACTICAL BUILD B

Make an analogue-like output with Arduino Uno PWM

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.

PWM pinRVOUTGNDCMeasure here
Simple first-order RC low-pass filter. Connect the oscilloscope probe across the capacitor, with its ground clip at circuit ground.

Example parts

  • Arduino Uno R3-compatible board
  • 10 kΩ resistor in series with PWM pin
  • 10 µF capacitor from output node to GND (observe polarity if electrolytic)
  • Common ground and oscilloscope or high-impedance meter

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.

Example sketch

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.

09 · MEASUREMENT AND TROUBLESHOOTING

Measure the signal, not just the code

Oscilloscope checks

  • Use DC coupling when measuring offset and absolute output level.
  • Measure peak-to-peak voltage and period, then calculate frequency.
  • Look for staircase steps, ripple, clipping and timing jitter.
  • Use a suitable probe attenuation setting and verify its ground reference.
  • Keep the signal within the board's permitted output range.

Common faults

  • No output: check DAC-capable chip, pin, sketch and common ground.
  • Flat or clipped signal: check code limits, offset, amplitude and output range.
  • Excessive ripple: review sample rate, PWM carrier and filter cutoff.
  • Slow response: reduce RC time constant or adjust the signal bandwidth.
  • Unexpected amplitude: check probe factor, load and actual reference/supply.
  • Distorted waveform: check sampling density, timing jitter and DAC settling.

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.

10 · KNOWLEDGE CHECK

Check what you understand

1. What does an N-bit DAC provide?

2. Which statement about an Arduino Uno R3 PWM pin is correct?

3. What is a key advantage of an R-2R ladder?

4. What happens when the RC filter time constant is increased?

KEEP LEARNING · GO FURTHER

Free to learn. More support when you need it.

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.

Enquire about guidance