Electronics & Circuits · Study deck

DAC and PWM: Loads, Filters, and Code

PWM switches a signal on and off in short cycles.

Voltage Vera is your guide for this deck.

analogdigitaloutput
Voltage Vera, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Calculate PWM ripple for an RC filter and load.
  • Implement DAC and PWM output on Arduino and ESP32.
  • Explain: Switch to PWM + RC filter, then increase capacitance; the output gets smoother but responds more slowly.
  • Explain: Result: The ideal 100 Hz design gives 0.159uF.
iotclass.org

Major section

Load Experience with PWM/DAC

Switch to PWM + RC filter, then increase capacitance; the output gets smoother but responds more slowly.

  • Lower PWM frequency below the filter cutoff; the ripple warning should appear.

Change the duty cycle, frequency, supply voltage, and filter values. Watch the waveform and the load response change before you build anything.

iotclass.org

Major section

PWM to Analog Conversion (RC Filter)

Result: The ideal 100 Hz design gives 0.159uF.

  • A common 0.22uF capacitor with 10k ohm resistance gives about 72 Hz cutoff, which is still low enough to smooth a 1 kHz PWM signal but responds more slowly.
  • f_cutoff should be 10-20x below PWM frequency.
iotclass.org

Major section

DAC Resolution vs Accuracy

An 8-bit DAC has 12.9 mV steps at 3.3V, but typical INL of +/-1 LSB means actual output can deviate by +/-12.9 mV from the ideal.

  • For precision applications, use a higher-resolution DAC (12-bit or 16-bit) and calibrate against a known reference voltage.

The Mistake: Assuming an 8-bit DAC with a 3.3V reference produces exact voltages at every step (e.g., expecting exactly 1.650V for value 128).

iotclass.org

Major section

PWM Frequency Too Low

Cameras recording at 30-60 fps can capture individual PWM cycles, and human hearing extends to ~20 kHz.

  • On ESP32, use ledcSetup(channel, freq, resolution) to set custom PWM frequency.

The Mistake: Using the default Arduino PWM frequency (490 Hz on most pins) for motor control, resulting in audible motor whine, or for LED dimming causing visible flicker in video recordings.

iotclass.org

Major section

For Kids: Meet the Sensor Squad!

DACs are like translators who speak the language of motors and lights!

  • the microcontroller had a new challenge. "I know the motor needs to spin at 50% speed, and I can think in numbers like 128 out of 255.
  • the battery was impressed. "So you are a reverse translator!".
iotclass.org

Major section

For Kids: Meet the Sensor Squad! (continued)

But Temperature Terry noticed something. "Wait, Danny, you are not on every microcontroller.

  • I want to glow at half brightness, but I need a smooth amount of power, not a number!".
  • Danny pointed to his friend PWM Pete. "Pete has a clever trick!
  • Lila blinked. "I had no idea!
iotclass.org

Major section

DAC/PWM Actuator Control

DAC Resolution (8-bit) -> ADC Resolution: Both use the same 2n2^n2n idea.

  • An 8-bit DAC has 256 output levels, while a 12-bit ADC has 4,096 input levels.
  • Cross-module connection: PWM Actuator Control: Explains how DC motors respond to PWM average voltage via back-EMF and inductance.
iotclass.org

Deck summary

Key takeaways

Switch to PWM + RC filter, then increase capacitance; the output gets smoother but responds more slowly.

  • Result: The ideal 100 Hz design gives 0.159uF.
  • An 8-bit DAC has 12.9 mV steps at 3.3V, but typical INL of +/-1 LSB means actual output can deviate by +/-12.9 mV from the ideal.
  • Cameras recording at 30-60 fps can capture individual PWM cycles, and human hearing extends to ~20 kHz.
  • DACs are like translators who speak the language of motors and lights!
iotclass.org

Retrieval practice

Recall check 1 of 2

Voltage Vera says: answer from memory, then check your reasoning.

Q1A PWM signal at 5V with a 75% duty cycle produces what average voltage?

A2.50 V
B4.00 V
C3.75 V
D5.00 V
Show answer

Answer: C see answers page

Q2An ESP32 DAC channel outputs a value of 200 with a 3.3V reference. What is the approximate output voltage?

A2.59 V
B1.65 V
C2.00 V
D3.30 V
Show answer

Answer: A V_out = V_ref x (Digital Input / (2^n - 1)) = 3.3V x (200 / 255) = 3.3V x 0.784 = 2.59V. The ESP32 has an 8-bit DAC, so the maximum digital input is 255.

iotclass.org

Retrieval practice

Recall check 2 of 2

Voltage Vera says: answer from memory, then check your reasoning.

Q3You're dimming 10 LEDs to 50% brightness. Your ESP32 has 2 DAC pins and 16 PWM-capable GPIOs, with 10 independent 1 kHz PWM channels available. The LED drivers and viewing conditions meet the project's flicker requirement at 1 kHz. What's the best approach?

AUse ten external DACs for LED dimming because DAC output is needed to meet flicker requirements.
BUse two DAC pins with rapid switching among ten independently dimmed LEDs
CUse only DACs because PWM supposedly cannot control average LED brightness
DUse ten 1 kHz PWM channels after checking LED drivers and flicker requirements
Show answer

Answer: D The ten available 1 kHz PWM channels meet the stated flicker requirement, so each LED can have independent duty-cycle control with a suitable driver and current limit.

iotclass.org

Print reference

Answers 1 of 2

Answer key.

  1. C · V_average = V_high x (Duty Cycle / 100%) = 5V x 0.75 = 3.75V. PWM creates an average voltage proportional to the duty cycle. At 75% duty cycle, the signal is HIGH for 75% of each period, producing an average of 3.75V.
  2. A · V_out = V_ref x (Digital Input / (2^n - 1)) = 3.3V x (200 / 255) = 3.3V x 0.784 = 2.59V. The ESP32 has an 8-bit DAC, so the maximum digital input is 255.
iotclass.org

Print reference

Answers 2 of 2

Answer key.

  1. D · The ten available 1 kHz PWM channels meet the stated flicker requirement, so each LED can have independent duty-cycle control with a suitable driver and current limit.
iotclass.org