Module 5 - Analogue Inputs & PWM

15. PWM Output & Duty Cycle

Explain PWM duty cycle, measure it on the oscilloscope, and control LED brightness from a potentiometer with analogWrite.

Estimated time 2-3 hours

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.

PWM Output & Duty Cycle instructional connection diagram

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.

Three PWM waveform traces at about 25 percent, 50 percent and 75 percent duty cycle showing longer HIGH time as duty cycle rises
Same switching period; longer HIGH fraction = higher duty cycle = more average power.
Duty cycleHIGH time shareTypical LED look
0%Always LOWOff
25%Short HIGHDim
50%Half HIGHMedium
75%Long HIGHBright
100%Always HIGHFull 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.

Comparison of a PWM pulse train with a dashed average line versus a true steady mid-level DC voltage
Left: PWM pulses and their average. Right: true steady DC (what Uno PWM pins do not output).

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.

Scale from analogWrite 0 to 255 with cards for 0 always LOW, 127 about half duty, and 255 always HIGH
Map a pot (0-1023) to 0-255 when linking analogRead to LED brightness.
const byte ledPin = 9;  // must be a ~ PWM pin

pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 0);    // off
analogWrite(ledPin, 127);  // about half
analogWrite(ledPin, 255);  // full

See 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 valueDuty (approx.)HIGH time at 490 HzMultimeter average
00%0 ms (always LOW)0 V
6425%0.51 ms1.25 V
12750%1.02 ms2.5 V
19175%1.53 ms3.75 V
255100%Always HIGH5 V

Wiring and safe build sequence

Breadboard wiring for 15. PWM Output & Duty Cycle
Breadboard layout for this lesson. Match colours and pins before powering the circuit. Click the image for a larger view.
  1. D9 -> 330 ohm -> LED anode
  2. LED cathode -> GND
  3. Potentiometer wiper -> A0; outer legs -> 5 V and GND
Power rule: switch off before moving wires. Arduino I/O pins are control signals; high-current loads require a driver and suitable external supply.
const 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

  1. D9 is a PWM (~) pin so analogWrite can set duty cycle.
  2. map converts the ADC reading (0-1023) into a PWM value (0-255).
  3. LED response stays immediate because no delay blocks loop.
  4. The Serial report uses the millis pattern from Lesson 11, so it runs every 500 ms without slowing the LED.

Test and record evidence

Expected result: The potentiometer smoothly controls brightness while Serial reports twice per second. The scope shows a fixed period of about 2 ms with a HIGH time that follows the knob, and the multimeter average rises from 0 V to 5 V.

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.
Extension challenge: Add a second LED on pin 6 whose brightness is the opposite of the first (255 - brightness). View both pins on a two-channel scope and explain why the pin 6 pulses are twice as frequent.

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).