Sub topic 3
From duty cycle to average voltage and brightness
This chapter assumes you know what a PWM period and duty cycle are, can write a basic Arduino sketch with pinMode and digitalWrite, and know Ohm's law at an introductory level. It connects the duty cycle to a number you can measure, the average voltage. It then shows why that number only partly explains what an LED does.
The averaging idea
A PWM pin on a 5 V Arduino Uno is always at one of two levels, 0 V or about 5 V. The duty cycle D is the fraction of each period the pin spends high. Averaged over one full period, the voltage is:
V_avg = D × V_highThe formula follows from the area under the waveform. The pin is at V_high for D × T seconds and at 0 V for the rest of the period T. The area is V_high × D × T, and dividing by T gives V_avg.
For analogWrite(pin, value) with an 8-bit value, D = value / 255. The next chapters cover the function itself. Here you only need the relationship:
V_avg = V_high × value / 255The table shows what this gives on a 5 V board. The current column assumes a red LED with a forward voltage of about 2.0 V behind a 220 Ω resistor. That LED draws about 13.6 mA while the pin is high, which is (5 − 2.0) / 220.
| analogWrite value | Duty cycle | Average pin voltage | Average LED current |
|---|---|---|---|
| 0 | 0% | 0 V | 0 mA |
| 64 | 25.1% | 1.25 V | 3.4 mA |
| 128 | 50.2% | 2.51 V | 6.8 mA |
| 191 | 74.9% | 3.75 V | 10.2 mA |
| 255 | 100% | 5.00 V | 13.6 mA |
The relationship is linear, as the plot shows.
The same formula applies on a 3.3 V board such as many ARM-based Arduino models. Only V_high changes, so 50% duty gives about 1.65 V on average.
Seeing the average with software PWM
You can produce a PWM signal without any hardware timer. This sketch toggles a pin with digitalWrite and delayMicroseconds. It makes the period and duty cycle explicit and uses only functions you already know.
const uint8_t LED_PIN = 9;
const unsigned int PERIOD_US = 2000; // 2 ms period, about 500 Hz
const uint8_t DUTY_PERCENT = 25; // try 5, 25, 50, 90
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
unsigned int onTime = (unsigned long)PERIOD_US * DUTY_PERCENT / 100;
digitalWrite(LED_PIN, HIGH);
delayMicroseconds(onTime);
digitalWrite(LED_PIN, LOW);
delayMicroseconds(PERIOD_US - onTime);
}Each pass through loop() is one period. The actual period is a few microseconds longer than 2000 µs because digitalWrite and the loop itself take time, which does not matter for this demonstration. Change DUTY_PERCENT and upload again. The LED dims as the percentage falls, and at 100% it behaves like a steady HIGH.
Software PWM has a cost. The processor does nothing else while it waits, and any interrupt that fires mid-period stretches the pulse and causes visible jitter. Hardware PWM, which analogWrite() uses, keeps running while your code does other work. That difference matters in the chapters on fading and on millis().
Why averaging works: the load is slow
The average is meaningful only if the load responds slowly compared to the switching. If it follows each pulse, you see the pulses, not an average. Three things smooth the signal in typical hobby circuits:
- Your eye. Vision does not resolve flicker above a threshold that is roughly 60 to 100 Hz for most people in normal conditions. The Uno's default PWM frequencies are about 490 Hz and 980 Hz, well above that.
- The LED's light output. An LED's light follows its current almost instantly, so the LED itself does no smoothing. The averaging happens in the eye.
- The load's physical inertia. A motor's rotor mass and a heater's thermal mass average the pulses mechanically and thermally.
You can add the smoothing in hardware too. A resistor and capacitor in series, with the output taken across the capacitor, form a low-pass filter. The filter turns the pulses into a nearly steady voltage, which gives you a crude digital-to-analog converter. Its corner frequency is f_c = 1 / (2π R C). With R = 1 kΩ and C = 10 µF, f_c is about 16 Hz. A 490 Hz signal sits far above that, so most of the ripple is removed. Only about 0.1 V peak of ripple remains on a 50% duty signal. The cost is a slow response: the output takes tens of milliseconds to settle after you change the duty cycle. A heavily loaded filter output also sags, because the filter has high output resistance.
The LED responds to current, not to average voltage
The "average voltage" model is exact for a filtered output and a useful shorthand for many loads. For an LED it is misleading, and the difference explains behavior that surprises people.
Consider a 25% duty signal. The average pin voltage is 1.25 V. If you applied a steady 1.25 V across the LED and resistor, nothing would light, because 1.25 V is below the LED's forward voltage of roughly 2.0 V. Yet the PWM LED glows visibly. The LED never sees 1.25 V. For 25% of each period it sees the full 5 V, conducts about 13.6 mA, and emits light at the brightness of a fully lit LED. For the other 75% it is off.
The quantity that tracks brightness is the average current:
I_avg = D × I_onwhere I_on is the current while the pin is high, set by the supply, the LED's forward voltage, and the series resistor. LED light output is close to proportional to current across the normal operating range. So light output, averaged over a period, is close to proportional to duty cycle. This is why PWM dims an LED so cleanly. An analog approach that lowers the voltage would not work, because the current-voltage curve of an LED is steeply exponential. A small change in voltage near the forward voltage gives a large change in current, and below the forward voltage you get no light at all.
PWM sidesteps that curve. The LED is always either off or at a known good operating point, and only the time at each state changes.
Here is a sketch that prints the average pin voltage for several analogWrite-style values. It calculates the same numbers as the table.
const float V_HIGH = 5.0; // use 3.3 on 3.3 V boards
float averageVolts(uint8_t value) {
return V_HIGH * value / 255.0;
}
void setup() {
Serial.begin(9600);
const uint8_t values[] = {0, 64, 128, 191, 255};
for (uint8_t i = 0; i < sizeof(values); i++) {
Serial.print(values[i]);
Serial.print(" -> ");
Serial.print(averageVolts(values[i]), 2);
Serial.println(" V");
}
}
void loop() {
}On the Serial Monitor at 9600 baud, you should see 128 -> 2.51 V and 255 -> 5.00 V, among the other lines.
Linear light does not look linear
The physical result is that doubling the duty cycle doubles the light output. Your eye does not report it that way. Human brightness perception is strongly non-linear: roughly, the eye is far more sensitive to differences between dark levels than between bright ones. A 50% duty LED looks noticeably brighter than "half" of full brightness. Going from 0% to 10% duty looks like a big jump, and going from 90% to 100% looks like almost no change.
This has a practical consequence for fades. If you step the value evenly from 0 to 255, most of the visible change happens in the first third of the fade, and the last third looks nearly flat. The fix is gamma correction, which maps a linear "perceptual" level to a duty cycle on a curve. A later chapter builds it. For now, keep the two ideas separate:
- Duty cycle maps linearly to average current and to light output.
- Light output maps non-linearly to perceived brightness.
Common errors and gotchas
- Treating average voltage as what the LED sees. At low duty the average can be below the forward voltage while the LED still lights, as shown above. Do not use the average to predict LED behavior. Use
I_onand the duty cycle. - Sizing the resistor for the average. The resistor limits the current during the on-time, so size it for
I_onat 100% duty. Computing it from a lower average voltage lets full-duty pulses overdrive the LED or the pin. The wiring chapter covers the calculation. - Measuring PWM with a multimeter. A meter on a DC voltage range often shows something near the average, but readings depend on the meter's filtering and the PWM frequency. Some meters bounce or show wrong values. Use an oscilloscope or logic analyzer to check duty cycle, and use the meter only as a rough indication.
- Forgetting the supply level. The average scales with
V_high. On a USB-powered Uno it may be 4.7 to 5.1 V, not exactly 5.0 V. If you need a precise analog voltage, filter the output and measure it. - Assuming 0 and 255 are PWM. At duty 0% and 100% there are no pulses. The pin is a constant LOW or HIGH, so the average is exactly the level of the pin.
- Expecting a filtered output to be instantly adjustable. An RC filter smooths the output but slows its response. A larger time constant gives less ripple and a slower reaction to changes.
- Ignoring low-frequency flicker. The averaging model assumes the signal is fast enough. A 100 Hz software PWM may look steady when you look straight at it. A phone camera, or your peripheral vision, may still show banding or flicker. PWM frequency and its limits are covered in a later chapter.
Check your understanding
Exercise
An Uno-style 5 V board runs analogWrite(pin, 100) on an LED with a 220 Ω series resistor. The LED has a forward voltage of about 2.0 V. What are the duty cycle, the average pin voltage, and the average LED current?
Show answer
Duty cycle is 100 / 255, about 39.2%. Average pin voltage is 5 V × 100 / 255, about 1.96 V. The current while the pin is high is (5 − 2.0) / 220, about 13.6 mA. Average current is 0.392 × 13.6 mA, about 5.3 mA. Note that 1.96 V is below the LED's forward voltage, but the LED still lights, because it sees full 5 V pulses for 39.2% of each period.
Exercise
You build an RC filter with R = 1 kΩ and C = 10 µF on a 490 Hz PWM output. Calculate its corner frequency and explain what happens to the filtered voltage when you change the duty cycle from 25% to 75%.
Show answer
The corner frequency is 1 / (2π × 1000 × 0.00001), about 16 Hz. This is far below 490 Hz, so most of the ripple is removed. When the duty cycle changes from 25% to 75%, the filtered output moves from about 1.25 V to about 3.75 V on a 5 V board, but not instantly. It rises gradually, with a time constant R × C = 10 ms, so it takes several time constants (tens of milliseconds) to settle.
Exercise
Why does stepping analogWrite from 0 to 255 in equal steps not look like an even fade, even though light output rises linearly?
Show answer
Light output is proportional to duty cycle, but the eye's perception of brightness is non-linear. It is more sensitive to changes at low light levels than at high ones. Equal steps in duty therefore look like large changes at the dark end and small changes at the bright end. Gamma correction compensates for this.