viewof simMode = Inputs.select(["PWM to LED", "PWM to motor", "PWM + RC filter", "True DAC"], {value: "PWM + RC filter", label: "Output method to simulate"})
viewof simVhigh = Inputs.range([1.8, 12], {value: 5, step: 0.1, label: "Supply / reference voltage (V)"})
viewof simDuty = Inputs.range([0, 100], {value: 50, step: 1, label: "PWM duty cycle (%)"})
viewof simFreq = Inputs.range([50, 50000], {value: 1000, step: 50, label: "PWM frequency (Hz)"})Load Experience with PWM/DAC
Load Experience with PWM/DAC
animation
electronics
Try Keep Output method to simulate on PWM + RC filter with PWM duty cycle (%) at 50, then increase RC filter capacitance (uF).
Observe At the opening 5 V supply, PWM average is 2.50 V and RC cutoff is 72.3 Hz.
Explain PWM duty cycle (%) sets the mean as supply times duty fraction; the filter moves each plotted value toward the current pulse voltage.
Technical boundaries. The waveform assumes a first-order RC update across 260 points covering three PWM periods, starting at the mean voltage. Ripple is measured over the last two thirds; motor speed is a duty-based proxy and True DAC ripple is fixed at zero.
TipLoad Experience with PWM/DAC
The controls change duty, frequency, supply, and filter values. Watch how those choices change the modeled waveform; check the actual circuit with its load before building a design.
viewof simR = Inputs.range([100, 100000], {value: 10000, step: 100, label: "RC filter resistance (ohms)"})
viewof simC = Inputs.range([0.01, 10], {value: 0.22, step: 0.01, label: "RC filter capacitance (uF)"})
viewof simDacBits = Inputs.select([8, 10, 12], {value: 8, label: "True DAC resolution (bits)"})simData = {
const periods = 3;
const samples = 260;
const periodMs = 1000 / simFreq;
const durationMs = periods * periodMs;
const dt = durationMs / (samples - 1);
const tauMs = simR * simC * 1e-6 * 1000;
const alpha = tauMs <= 0 ? 1 : dt / (tauMs + dt);
const dutyFrac = simDuty / 100;
const points = [];
let filtered = simVhigh * dutyFrac;
for (let i = 0; i < samples; i++) {
const tMs = i * dt;
const phase = (tMs % periodMs) / periodMs;
const pwm = phase < dutyFrac ? simVhigh : 0;
filtered += alpha * (pwm - filtered);
const dac = simVhigh * simDacCode / simMaxCode;
points.push({tMs, pwm, filtered, dac});
}
const stable = points.slice(Math.floor(samples / 3));
const filteredValues = stable.map(d => d.filtered);
const ripple = Math.max(...filteredValues) - Math.min(...filteredValues);
const cutoffHz = 1 / (2 * Math.PI * simR * simC * 1e-6);
const avgVoltage = simVhigh * dutyFrac;
const dacVoltage = simVhigh * simDacCode / simMaxCode;
const stepMv = simVhigh / simMaxCode * 1000;
const ratio = simFreq / cutoffHz;
return {points, periodMs, tauMs, cutoffHz, ripple, avgVoltage, dacVoltage, stepMv, ratio};
}{
const width = 760;
const height = 290;
const pad = 42;
const plotW = width - pad * 2;
const plotH = height - pad * 2;
const maxT = simData.points[simData.points.length - 1].tMs;
const x = t => pad + (t / maxT) * plotW;
const y = v => height - pad - (v / simVhigh) * plotH;
let pwmPath = "";
simData.points.forEach((d, i) => {
const px = x(d.tMs).toFixed(1);
const py = y(d.pwm).toFixed(1);
if (i === 0) {
pwmPath += `M${px},${py}`;
} else {
const prev = simData.points[i - 1];
pwmPath += ` L${px},${y(prev.pwm).toFixed(1)} L${px},${py}`;
}
});
const filteredPath = simData.points.map((d, i) => `${i ? "L" : "M"}${x(d.tMs).toFixed(1)},${y(d.filtered).toFixed(1)}`).join(" ");
const dacPath = `M${pad},${y(simData.dacVoltage).toFixed(1)} L${width - pad},${y(simData.dacVoltage).toFixed(1)}`;
const ledBrightness = Math.pow(simDuty / 100, 0.45);
const motorSpeed = Math.round(simDuty);
const filterGood = simData.ratio >= 10;
const rippleLabel = simMode === "True DAC" ? "DAC ripple" : simMode === "PWM + RC filter" ? "Filtered ripple" : "Load signal";
const rippleValue = simMode === "True DAC" ? "0.000 V" : simMode === "PWM + RC filter" ? `${simData.ripple.toFixed(3)} V` : "pulsed";
const rippleColor = simMode === "True DAC" || (simMode === "PWM + RC filter" && filterGood) ? "#0a7d42" : simMode === "PWM + RC filter" ? "#b64235" : "#c45f12";
const advice = simMode === "PWM to LED"
? (simFreq >= 1000 ? "Good for LEDs and camera recording: PWM is fast enough to appear steady." : "Works for the eye, but cameras may show flicker. Try 1000 Hz or higher.")
: simMode === "PWM to motor"
? (simFreq >= 20000 ? "Good for motors: ultrasonic PWM avoids audible whine." : "The motor will run, but the switching may be audible. Try 20 kHz.")
: simMode === "PWM + RC filter"
? (filterGood ? "Good smoothing: PWM frequency is at least 10x above the RC cutoff." : "Too much ripple risk: increase PWM frequency or lower the RC cutoff.")
: "A true DAC produces a direct voltage step. Higher bit depth makes the steps smaller.";
return html`<div style="background:linear-gradient(135deg,#f8fbff 0%,#f3fbf8 48%,#fff8ef 100%); border:1px solid #d7e5ef; border-radius:14px; padding:1rem; box-shadow:0 10px 24px rgba(23,48,66,0.08);">
<div style="display:grid; grid-template-columns:repeat(auto-fit,minmax(150px,1fr)); gap:0.75rem; margin-bottom:1rem;">
<div style="background:white; border-radius:10px; padding:0.75rem; border:1px solid #e5edf2;">
<div style="font-size:0.78rem; color:#52616b;">PWM average</div>
<div style="font-size:1.35rem; font-weight:800; color:#c45f12;">${simData.avgVoltage.toFixed(2)} V</div>
</div>
<div style="background:white; border-radius:10px; padding:0.75rem; border:1px solid #e5edf2;">
<div style="font-size:0.78rem; color:#52616b;">RC cutoff</div>
<div style="font-size:1.35rem; font-weight:800; color:#0a7d78;">${simData.cutoffHz.toFixed(1)} Hz</div>
</div>
<div style="background:white; border-radius:10px; padding:0.75rem; border:1px solid #e5edf2;">
<div style="font-size:0.78rem; color:#52616b;">${rippleLabel}</div>
<div style="font-size:1.35rem; font-weight:800; color:${rippleColor};">${rippleValue}</div>
</div>
<div style="background:white; border-radius:10px; padding:0.75rem; border:1px solid #e5edf2;">
<div style="font-size:0.78rem; color:#52616b;">DAC output</div>
<div style="font-size:1.35rem; font-weight:800; color:#256ca8;">${simData.dacVoltage.toFixed(3)} V</div>
</div>
</div>
<svg viewBox="0 0 ${width} ${height}" style="width:100%; height:auto; background:#ffffff; border:1px solid #dce7ee; border-radius:12px;">
<rect x="${pad}" y="${pad}" width="${plotW}" height="${plotH}" fill="#fbfdff" stroke="#e5edf2"/>
<line x1="${pad}" y1="${y(simVhigh)}" x2="${width - pad}" y2="${y(simVhigh)}" stroke="#d7e1e8" stroke-dasharray="4 4"/>
<line x1="${pad}" y1="${y(0)}" x2="${width - pad}" y2="${y(0)}" stroke="#d7e1e8"/>
<line x1="${pad}" y1="${y(simData.avgVoltage)}" x2="${width - pad}" y2="${y(simData.avgVoltage)}" stroke="#a96f22" stroke-dasharray="6 5" opacity="0.65"/>
<path d="${pwmPath}" fill="none" stroke="#f28c28" stroke-width="3" stroke-linejoin="round"/>
<path d="${filteredPath}" fill="none" stroke="${simMode === "PWM + RC filter" ? "#0f9f9a" : "transparent"}" stroke-width="4" stroke-linecap="round"/>
<path d="${dacPath}" fill="none" stroke="${simMode === "True DAC" ? "#2f80c5" : "transparent"}" stroke-width="5" stroke-linecap="round"/>
<text x="${pad}" y="24" font-size="13" fill="#52616b">Voltage waveform over ${maxT.toFixed(2)} ms</text>
<text x="${width - pad}" y="${y(simVhigh) - 8}" text-anchor="end" font-size="11" fill="#52616b">${simVhigh.toFixed(1)} V</text>
<text x="${width - pad}" y="${y(simData.avgVoltage) - 8}" text-anchor="end" font-size="11" fill="#8b5d17">average ${simData.avgVoltage.toFixed(2)} V</text>
<text x="${pad}" y="${height - 12}" font-size="11" fill="#52616b">orange = PWM pulses; teal = RC filtered output; blue = true DAC level</text>
</svg>
<div style="display:grid; grid-template-columns:repeat(auto-fit,minmax(220px,1fr)); gap:0.9rem; margin-top:1rem;">
<div style="background:white; border-radius:12px; padding:1rem; border:1px solid #e5edf2;">
<div style="font-weight:800; margin-bottom:0.5rem;">LED brightness preview</div>
<div style="height:72px; display:flex; align-items:center; gap:1rem;">
<div style="width:58px; height:58px; border-radius:50%; background:#ffd35a; opacity:${Math.max(0.12, ledBrightness)}; box-shadow:0 0 ${Math.round(8 + 42 * ledBrightness)}px rgba(255,181,46,${0.18 + 0.55 * ledBrightness}); border:2px solid #f1a51d;"></div>
<div><strong>${simDuty}% duty</strong><br><span style="color:#52616b;">Perceived brightness is not perfectly linear.</span></div>
</div>
</div>
<div style="background:white; border-radius:12px; padding:1rem; border:1px solid #e5edf2;">
<div style="font-weight:800; margin-bottom:0.5rem;">Motor / fan speed proxy</div>
<div style="height:14px; border-radius:999px; background:#edf2f5; overflow:hidden; margin:0.7rem 0;">
<div style="height:100%; width:${motorSpeed}%; background:linear-gradient(90deg,#79c6a3,#1d8f70);"></div>
</div>
<div><strong>${motorSpeed}% commanded speed</strong><br><span style="color:#52616b;">Real motors need a driver transistor, not a direct GPIO pin.</span></div>
</div>
</div>
<p style="margin:1rem 0 0; padding:0.75rem; border-radius:10px; background:${filterGood || simMode !== "PWM + RC filter" ? "#eef9f3" : "#fff1ed"}; color:${filterGood || simMode !== "PWM + RC filter" ? "#176a3a" : "#9b3b25"}; font-weight:700;">${advice}</p>
<p style="margin:0.65rem 0 0; color:#52616b;">DAC step size at the selected resolution: ${simData.stepMv.toFixed(simDacBits <= 8 ? 1 : 2)} mV. RC settling time: ${(5 * simData.tauMs).toFixed(2)} ms.</p>
</div>`;
}What to try:
- Set duty cycle to 25%, 50%, and 75%; notice how average voltage and LED brightness change.
- 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.
- Switch to True DAC and compare the stable blue voltage level with the PWM pulses.