Sub topic 2
PWM Basics: Period and Duty Cycle
This chapter assumes that you know the structure of an Arduino sketch, can use pinMode() and digitalWrite(), and understand basic breadboard wiring. It also assumes that you have seen a digital output switch between LOW and HIGH.
A digital pin has two normal output states. It cannot directly produce a steady 2 V or 3 V output between those states. Pulse-width modulation (PWM) creates an analog-like control by switching the pin between LOW and HIGH very quickly.
A Repeating PWM Signal
A PWM signal repeats the same pattern:
- The signal stays
HIGHfor part of a cycle. - The signal goes
LOWfor the rest of the cycle. - The cycle repeats many times per second.
The period is the time required for one complete cycle. The frequency is how many cycles occur each second.
The relationship is:
frequency = 1 / periodFor example, a period of 1 millisecond produces a frequency of 1,000 hertz (Hz). A hertz means one cycle per second.
The duty cycle is the percentage of each period that the signal remains HIGH.
duty cycle = (HIGH time / period) × 100%A 25% duty cycle means that the signal is HIGH for one quarter of each cycle and LOW for the other three quarters.
| Duty cycle | HIGH time in one period | Typical average output |
|---|---|---|
| 0% | 0% of the period | Always LOW |
| 25% | One quarter | About one quarter of full output |
| 50% | Half | About half of full output |
| 75% | Three quarters | About three quarters of full output |
| 100% | The entire period | Always HIGH |
Visualizing Period and Duty Cycle
The signal does not become a smooth, lower-voltage signal. It remains either LOW or HIGH. The load responds to the rapid switching. An LED produces light during each HIGH interval, and the eye combines the rapid flashes into an apparent brightness.
Connecting Duty Cycle to Average Output
For a simple PWM signal, the average voltage can be estimated as:
average voltage ≈ supply voltage × duty cycleUse the duty cycle as a decimal in this calculation. With a 5 V supply and a 40% duty cycle:
average voltage ≈ 5 V × 0.40
average voltage ≈ 2 VThis does not mean that the pin continuously outputs 2 V. A multimeter may display a value near 2 V because it averages the rapid changes. An oscilloscope shows the actual square wave: 5 V during the HIGH time and 0 V during the LOW time.
This average model works well for loads such as LEDs and motors, but it does not describe every circuit. A load's electrical and mechanical response also matters. A capacitor, motor, or human eye can smooth rapid changes in different ways.
PWM Timing
The period must be short enough that the load responds as intended. For an LED, a sufficiently high frequency prevents visible flicker. For a motor, the frequency affects sound, torque ripple, and switching losses.
Changing the duty cycle changes the HIGH and LOW portions while the period can remain constant:
- At 10%, the pulse is short and the pause is long.
- At 50%, the pulse and pause have equal lengths.
- At 90%, the pulse is long and the pause is short.
Arduino hardware generates these repeated transitions with timers. The timer counts at a fixed rate and compares its count with a duty-cycle value. When the count passes that value, the output changes state. This lets the hardware continue producing PWM while your program performs other work.
Arduino's Duty-Cycle Values
On a typical Arduino Uno, analogWrite() accepts a value from 0 to 255 on a PWM-capable pin. This is an 8-bit range with 256 possible settings.
const uint8_t LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
analogWrite(LED_PIN, 64); // Approximately 25% duty cycle
}The value is mapped approximately like this:
duty cycle ≈ value / 255 × 100%A value of 0 produces 0% duty cycle. A value of 255 produces 100% duty cycle. A value of 128 produces approximately 50% duty cycle.
The exact waveform and frequency depend on the board's timer configuration. The important model is that analogWrite() chooses a duty-cycle setting, while the timer supplies the repeating period.
Exercise
A PWM signal has a period of 2 milliseconds and stays HIGH for 0.5 milliseconds. What are its frequency and duty cycle?
Show answer
The frequency is 1 ÷ 0.002 seconds = 500 Hz. The duty cycle is 0.5 ÷ 2 × 100% = 25%.