All articles

    PWM: Controlling an LED's Brightness and a Motor's Speed with a Digital Pin

    PWM explained for beginners: duty cycle, frequency and resolution, how a pin that outputs only 0 or 5 V can control brightness and speed, analogWrite on the Arduino, fading without delay and correcting perceived brightness, PWM on the ESP32 with code, choosing the frequency, the multimeter with PWM, and turning it into a steady voltage with an RC filter.

    Updated 5 min read

    A microcontroller's digital pin outputs only two things: 0 V or 5 V (or 3.3). So how do we make an LED glow at half brightness, or a motor spin at half speed? The answer: PWM (pulse width modulation).

    The idea

    Instead of outputting a steady voltage, we switch the pin on and off very fast, and control the proportion of on-time in each cycle. That proportion is called the duty cycle.

    Duty cycles of 25%, 50% and 75%: the average (the dashed line) rises, and the LED gets brighter
    Duty cycles of 25%, 50% and 75%: the average (the dashed line) rises, and the LED gets brighter
    • 0%: always off.
    • 25%: on a quarter of the time, so an average of about 1.25 V out of 5.
    • 50%: an average of 2.5 V.
    • 100%: always on.

    The eye can't keep up with the LED switching off and on hundreds of times a second, so it sees the average brightness. And a motor is mechanically heavy, so it also responds to the average.

    The three numbers

    • Duty cycle: the on-time as a proportion of the cycle.
    • Frequency: how many times per second the cycle repeats. 490 Hz = a cycle about every 2 ms.
    • Resolution: the number of steps you can choose the proportion from. 8-bit = 256 steps (0 to 255), so each step is about 0.4%.

    Work out the relationships between them with the PWM calculator.

    On the Arduino

    analogWrite(9, 128);  // 0 to 255, so 128 ≈ 50%
    • It only works on pins marked with ~ (on the Uno: 3, 5, 6, 9, 10 and 11).
    • The frequency is about 490 Hz (and about 980 on pins 5 and 6).
    • The name analogWrite is a little misleading, because the output isn't truly analog; it's PWM.

    Changing the frequency: possible by modifying the timer settings, but watch out: the timer for pins 5 and 6 is the same one that counts millis() and delay(), so if you change it all your timing breaks. Only change pins 9 and 10 or 3 and 11.

    Fading without delay

    int level = 0, dir = 1;
    unsigned long tFade = 0;
    
    void loop() {
      if (millis() - tFade >= 8) {
        tFade = millis();
        level += dir;
        if (level == 0 || level == 255) dir = -dir;
        analogWrite(9, level);
      }
    }

    It rises and falls in about 4 seconds, and the program keeps running normally. It's the same approach as millis instead of delay.

    Brightness is "non-linear"

    If you try this code you'll notice the LED looks bright very quickly and then barely changes. The eye is far more sensitive to changes at low brightness than at high. The fix: square the value before sending it:

    analogWrite(9, (long)level * level / 255);

    Now the fade looks natural and even to the eye.

    On the ESP32

    The ESP32 has PWM on most pins, and you control the frequency and the resolution (number of bits). The functions differ depending on the version of the Arduino core for the ESP32 (ledc), so check the examples for the version you have.

    // Version 3 and later
    ledcAttach(18, 5000, 10);   // pin 18, 5 kHz, 10-bit (0–1023)
    ledcWrite(18, 512);         // about 50%
    
    // Version 2
    ledcSetup(0, 5000, 10);     // channel 0
    ledcAttachPin(18, 0);
    ledcWrite(0, 512);

    Frequency and resolution trade off: the higher the frequency, the fewer bits are available. 5 kHz with 10 or 12 bits is comfortable, but 1 MHz will give you only a few bits.

    Choosing the frequency

    • LEDs: anything above about 200 Hz usually won't show flicker. In phone video, though, stripes may appear if the frequency is low.
    • Motors: a low frequency produces an audible whine. Above 20 kHz the sound disappears, but the driver must be able to work at that speed. See the DC motor and H-bridge.
    • Servos: need a particular frequency (50 Hz) and a pulse of a specific width, which is a special case.

    Loads bigger than the pin

    A microcontroller pin can't drive a motor or an LED strip directly. The PWM goes to a MOSFET or a driver, which drives the load at the same ratio. And for a MOSFET that works at the Arduino's voltage, see the logic-level MOSFET.

    How do you measure PWM?

    • A multimeter on DC reads roughly the average: 50% of 5 V will read about 2.5. That's enough to confirm the pin is changing.
    • An oscilloscope shows you the waveform itself, the frequency, and the exact ratio.

    If you really need a steady voltage

    If you need a true DC voltage (not an average), put an RC filter after the PWM to turn it into a smooth voltage. Or use a DAC chip, or a DAC pin if the microcontroller has one.

    Example: a 10k resistor and a 10 µF capacitor → a time constant of 0.1 s. With 490 Hz PWM, the output is a nearly smooth voltage, but it takes about half a second to settle when you change the value. A larger capacitor = a smoother, slower output. And this output can't carry a load: for reading only, or followed by an op-amp.

    FAQ

    Why doesn't the LED switch off completely at 0? With analogWrite(pin, 0) the pin goes fully LOW. If the LED still glows faintly, the wiring probably has something else in it, or the pin isn't a PWM pin.

    Does PWM work for ordinary fans? 4-wire computer fans have a dedicated PWM wire at about 25 kHz. And 2-wire fans work with a MOSFET and ordinary PWM.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.