15. PWM Output & Duty Cycle
Explain PWM duty cycle, measure it on the oscilloscope, and control LED brightness from a potentiometer with analogWrite.
Learning outcomes
- Explain PWM as rapid digital switching with a duty cycle (not a true analogue voltage)
- Use analogWrite on Uno PWM pins (~) with values 0-255
- Measure PWM frequency and duty cycle on an oscilloscope and relate them to analogWrite values
- Explain why a multimeter shows the average voltage of a PWM signal
- Scale an analogRead value to a PWM value with map()
Parts and preparation
Uno, LED, 330 ohm resistor, 10 k ohm potentiometer, oscilloscope and multimeter.
Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.
What PWM is
PWM means Pulse-Width Modulation. The pin still only drives LOW or HIGH (digital levels), but it switches many times per second. Duty cycle is the percentage of each period spent HIGH.
A larger duty cycle means more average power to a load such as an LED, so it looks brighter. A motor driver or MOSFET can use the same idea for average speed or power. PWM is not a true analogue mid-voltage from a DAC.
| Duty cycle | HIGH time share | Typical LED look |
|---|---|---|
| 0% | Always LOW | Off |
| 25% | Short HIGH | Dim |
| 50% | Half HIGH | Medium |
| 75% | Long HIGH | Bright |
| 100% | Always HIGH | Full on |
PWM is not true analogue
Lesson 14 used the ADC to measure many voltages. PWM goes the other way for control: it fakes a mid level by pulsing. An LED and your eye average the pulses. A slow multimeter may show a mid reading, but the pin is still switching HIGH/LOW.
Do not treat analogWrite as producing a clean DC voltage for precision analogue circuits. Use it for brightness, simple motor enable/PWM speed, and similar average-power jobs.
analogWrite on the Uno
analogWrite(pin, value) sets the PWM duty on pins marked ~ : 3, 5, 6, 9, 10 and 11. value is 0-255 (8-bit). 0 is off, 255 is full on, mid values set the duty cycle.
Call pinMode(pin, OUTPUT) first. Heavy loads still need a driver (Lesson 16) - PWM only controls the pin; it does not raise the pin current limit.
const byte ledPin = 9; // must be a ~ PWM pin
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 0); // off
analogWrite(ledPin, 127); // about half
analogWrite(ledPin, 255); // fullSee PWM on the oscilloscope
Probe D9 against GND with DC coupling, 2 V/div and 1 ms/div. On an Uno R3, pin 9 switches at about 490 Hz, so one period is about 2.04 ms. Pins 5 and 6 run at about 980 Hz (about 1.02 ms).
Turn the potentiometer and watch the HIGH time grow while the period stays the same. Use the scope's measurements for frequency and positive duty, and compare them with the value on Serial: duty % ≈ value / 255 × 100.
Now measure the same pin with the multimeter on DC volts. It shows the average, about 5 V × duty - which is why a slow meter can make PWM look like an analogue voltage.
| analogWrite value | Duty (approx.) | HIGH time at 490 Hz | Multimeter average |
|---|---|---|---|
| 0 | 0% | 0 ms (always LOW) | 0 V |
| 64 | 25% | 0.51 ms | 1.25 V |
| 127 | 50% | 1.02 ms | 2.5 V |
| 191 | 75% | 1.53 ms | 3.75 V |
| 255 | 100% | Always HIGH | 5 V |
Wiring and safe build sequence

- D9 -> 330 ohm -> LED anode
- LED cathode -> GND
- Potentiometer wiper -> A0; outer legs -> 5 V and GND
Worked sketch
Download .ino sketchconst byte ledPin = 9;
unsigned long lastReport = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int raw = analogRead(A0);
byte brightness = map(raw, 0, 1023, 0, 255);
analogWrite(ledPin, brightness);
if (millis() - lastReport >= 500) {
lastReport = millis();
Serial.println(brightness);
}
}How the code works
- D9 is a PWM (~) pin so analogWrite can set duty cycle.
- map converts the ADC reading (0-1023) into a PWM value (0-255).
- LED response stays immediate because no delay blocks loop.
- The Serial report uses the millis pattern from Lesson 11, so it runs every 500 ms without slowing the LED.
Test and record evidence
Practical evidence checklist
Common faults and checks
- Use a pin marked ~ for PWM (3, 5, 6, 9, 10, 11 on Uno).
- Do not connect a motor in place of the LED without a driver and flyback protection.
- If brightness never changes, check pot wiring to A0 and that ledPin is a PWM pin.
- Scope shows a flat line at 5 V or 0 V: the value is 255 or 0, or the pin is not a PWM pin.
Check your understanding
Q1. Is PWM a true analogue output?
Show answer
No; it is a digital pulse train with variable duty cycle.
Q2. What does duty cycle mean?
Show answer
The percentage of each PWM period that the pin is HIGH.
Q3. What range does analogWrite use on the Uno?
Show answer
0 to 255.
Q4. What does a multimeter on DC volts show on a PWM pin?
Show answer
The average voltage - about 5 V × duty cycle.
Q5. About what PWM frequency does pin 9 use on an Uno R3?
Show answer
About 490 Hz (pins 5 and 6 use about 980 Hz).