Sub topic 2
PWM basics: period and duty cycle
This chapter builds the working model of pulse-width modulation (PWM). It covers period, duty cycle, and how the two relate to frequency and pulse width. It also covers the mistakes that most often confuse people the first time. It assumes you can write a basic Arduino sketch with setup() and loop(), use pinMode() and digitalWrite(), wire a simple circuit on a breadboard, and apply Ohm's law at an introductory level. The previous chapter explained why a digital pin cannot hold a steady in-between voltage. This chapter shows what you do instead.
The idea in one paragraph
A digital output pin has two states: HIGH (about 5 V on an Uno) and LOW (about 0 V). PWM switches the pin between those two states over and over, fast enough that the load responds to the average rather than to each individual switch. The only thing that changes from one brightness to another is how long the pin stays HIGH within each repeating cycle. The pin never outputs an intermediate voltage. It outputs full voltage for part of the time and zero for the rest.
Period and frequency
A PWM signal repeats the same pattern. One complete repetition is a cycle, and the time it takes is the period, written T. The frequency f is how many cycles happen per second, so the two are reciprocals:
- f = 1 / T
- T = 1 / f
Frequency is measured in hertz (Hz). One hertz is one cycle per second.
Here are a few values worth memorizing, because they appear again in later chapters:
| Frequency | Period |
|---|---|
| 50 Hz | 20 ms |
| 490 Hz | about 2.04 ms |
| 980 Hz | about 1.02 ms |
| 1 kHz | 1 ms |
| 20 kHz | 50 µs |
The Arduino Uno's analogWrite() produces roughly 490 Hz on most PWM pins and roughly 980 Hz on pins 5 and 6. A later chapter covers the hardware reasons for that difference. For now, treat the frequency as a fixed property of the pin that your sketch does not change.
Pulse width and duty cycle
Within each period, the signal is HIGH for some time and LOW for the rest. The time spent HIGH is the pulse width, written t_on. The duty cycle is the fraction of the period spent HIGH, usually given as a percentage:
- duty cycle = t_on / T
- duty cycle (%) = 100 × t_on / T
For a 1 ms period:
| Pulse width | Duty cycle | Description |
|---|---|---|
| 0 ms | 0% | Always LOW |
| 0.25 ms | 25% | HIGH for a quarter of each cycle |
| 0.5 ms | 50% | Square wave, equal HIGH and LOW |
| 0.75 ms | 75% | HIGH for three quarters |
| 1 ms | 100% | Always HIGH |
The two extremes are worth noticing. A duty cycle of 0% is simply a pin held LOW, and a duty cycle of 100% is simply a pin held HIGH. Both are steady levels with no switching at all. PWM covers the full range between them with the same mechanism.
The key point is that the period stays constant while the pulse width changes. If you change the period as well, you have changed the frequency, which is a separate setting with separate effects.
Average value
If the pin swings between 0 V and V_high, and the load reacts slowly compared to the period, the load behaves as if it received the time-weighted average:
- Vavg = duty cycle × Vhigh
On a 5 V Uno:
- 0% gives 0 V
- 25% gives 1.25 V
- 50% gives 2.5 V
- 100% gives 5 V
This is a model of the average, not of what the pin does at any instant. At every instant the pin voltage is 0 V or 5 V. The next chapter looks at why an LED, your eye, and some other loads behave as if they were smoothing the signal. Here the important point is only that the average is proportional to the duty cycle.
Working through a calculation
Suppose a pin outputs PWM at 490 Hz with a duty cycle of 30%.
- Find the period: T = 1 / 490 Hz ≈ 2.04 ms.
- Find the pulse width: t_on = 0.30 × 2.04 ms ≈ 0.61 ms.
- Find the LOW time: 2.04 ms − 0.61 ms ≈ 1.43 ms.
- Find the average voltage on a 5 V pin: 0.30 × 5 V = 1.5 V.
You can check this with a quick sketch that prints the numbers. This one needs no wiring beyond the board's USB connection.
void setup() {
Serial.begin(9600);
const float frequencyHz = 490.0;
const float duty = 0.30;
const float vHigh = 5.0;
float periodMs = 1000.0 / frequencyHz;
float onMs = duty * periodMs;
float offMs = periodMs - onMs;
float vAvg = duty * vHigh;
Serial.print("Period (ms): ");
Serial.println(periodMs, 3);
Serial.print("HIGH time (ms): ");
Serial.println(onMs, 3);
Serial.print("LOW time (ms): ");
Serial.println(offMs, 3);
Serial.print("Average voltage (V): ");
Serial.println(vAvg, 2);
}
void loop() {
// Nothing to repeat
}Open the Serial Monitor at 9600 baud. You should see a period of 2.041 ms, a HIGH time of 0.612 ms, a LOW time of 1.429 ms, and an average of 1.50 V.
A software model of PWM
You can produce a slow PWM signal yourself with digitalWrite() and delayMicroseconds(). This is not how analogWrite() works, because the Uno's hardware generates its PWM using timers. But writing it by hand makes the period and pulse width concrete.
const uint8_t LED_PIN = 9;
const unsigned long PERIOD_US = 2000; // 2 ms period, 500 Hz
const unsigned long ON_US = 500; // 25% duty cycle
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delayMicroseconds(ON_US);
digitalWrite(LED_PIN, LOW);
delayMicroseconds(PERIOD_US - ON_US);
}With an LED and resistor on pin 9 (wiring comes in a later chapter), this appears dimly lit. Change ON_US to 1500 and it appears brighter. The sketch has two serious drawbacks. It occupies the processor entirely, so nothing else can run. It also wobbles slightly, because each digitalWrite() call and each loop iteration takes a small amount of extra time that is not in the delay values. The hardware PWM behind analogWrite() has neither problem, since the timer toggles the pin without the processor's involvement.
Resolution: how many duty cycle levels exist
The Uno's analogWrite() accepts an integer from 0 to 255, which is 256 distinct levels. This is an 8-bit resolution. The value maps linearly to the duty cycle:
- duty cycle ≈ value / 255
| `analogWrite()` value | Duty cycle |
|---|---|
| 0 | 0% |
| 64 | about 25% |
| 127 | about 50% |
| 191 | about 75% |
| 255 | 100% |
Resolution describes how finely you can choose the pulse width. With 256 levels, the smallest step is about 0.39% of the period. At 490 Hz that is roughly 8 µs. You cannot ask for a duty cycle between two adjacent levels. A later chapter returns to this when it discusses how the AVR timers count.
Common mistakes
These errors come up repeatedly. Knowing them in advance saves debugging time.
- Thinking the pin voltage is intermediate. If you measure a PWM pin with a basic multimeter in DC mode, the reading may show something like 2.5 V at 50% duty. The meter is averaging the signal, much like an LED does. An oscilloscope would show a pin that is only ever at 0 V or 5 V.
- Confusing duty cycle with frequency. Raising the duty cycle makes the HIGH portion longer. It does not make the signal switch faster. Changing
analogWrite()values never changes the frequency on a given pin. - Expecting 50% duty to look half as bright. The average power is half, but the eye does not judge brightness linearly. This is the subject of a later chapter on gamma correction.
- Using
analogWrite()on a non-PWM pin. On an Uno, pins without PWM hardware treat the call as a digital write: values below 128 set the pin LOW and values of 128 or more set it HIGH. No error is raised. - Setting the period when you meant the pulse width. In the software model,
PERIOD_USis fixed andON_USvaries. Mixing them up produces the wrong duty cycle or a signal that never goes LOW. - Forgetting that 100% and 0% are static. At the extremes the pin does not switch, so there is no signal to measure on a scope other than a flat line.
When PWM is the right tool
PWM is a good fit when the load either responds slowly to the switching (a motor with inertia, a heating element) or when your eye integrates the flicker (an LED at a high enough frequency). It is a poor fit when you need a genuinely smooth DC voltage, for instance to bias an analog sensor or feed an audio input. In that case you either add a low-pass filter, a resistor and capacitor that smooth the pulses, or use a digital-to-analog converter (DAC) chip. The Uno has no built-in DAC.
Summary of terms
| Term | Meaning | Formula |
|---|---|---|
| Period (T) | Time of one full cycle | 1 / f |
| Frequency (f) | Cycles per second | 1 / T |
| Pulse width (t_on) | HIGH time within a cycle | duty × T |
| Duty cycle | Fraction of the period spent HIGH | t_on / T |
| Average voltage | Time-weighted mean level | duty × V_high |
Exercise
A PWM pin on a 5 V board runs at 1 kHz with a pulse width of 0.2 ms. What are the period, the duty cycle, and the average voltage? Roughly what analogWrite() value would produce this duty cycle on an 8-bit pin?
Show answer
The period is 1 / 1000 Hz = 1 ms. The duty cycle is 0.2 ms / 1 ms = 20%. The average voltage is 0.20 × 5 V = 1 V. On an 8-bit pin the value is about 0.20 × 255 = 51.
Exercise
A colleague says that raising the analogWrite() value from 100 to 200 makes the pin "switch twice as fast". Is this correct? Explain what actually changes.
Show answer
No. The frequency stays the same (about 490 Hz or 980 Hz on an Uno). The duty cycle roughly doubles, from about 39% to about 78%, so each pulse stays HIGH longer and the LOW gap shrinks. The number of cycles per second is unchanged.
Exercise
In the software PWM sketch, you want a 10% duty cycle at 1 kHz. What values should PERIOD_US and ON_US have?
Show answer
A 1 kHz signal has a period of 1000 µs, so PERIOD_US is 1000. Ten percent of that is 100 µs, so ON_US is 100.