19.01.2026

What is PWM?

What is PWM?

Table of contents

A digital lie What is it Cycles Duty cycles Calculating the percentage of the duty cycle Calculating the output voltage PWM on Arduino boards Connections The code
Table of contents
A digital lie What is it Cycles Duty cycles Calculating the percentage of the duty cycle Calculating the output voltage PWM on Arduino boards Connections The code

A digital lie

When working with digital signals, there are only two states in which that signal can be: on or off. For instance, with a digital signal we can light up an LED or turn it off, there is no in-between.

However, what if we want to make an LED fade in and out, or if we want to change the speed of a motor? The answer is quite simple: we need an analog signal. However, it is quite complex (and expensive) to have a Digital to analog converter, especially on a microcontroller, so the analog signal must be "faked". This is where Pulse-width modulation (PWM) comes in handy!

What is it

Pulse-width modulation is a technique of changing a digital signal’s state rapidly so that it closely mimics an analog signal.

The image below shows how a consistent and rapid pulse can give a virtually infinite number of values between a high and low state:

There are a few terms important to understanding PWM, and those are: cycles and duty cycles.

Cycles

A cycle defines one repetition of an on and off state. A pulse cannot happen more than once in a cycle. A period is the amount of time a single cycle lasts. The frequency of a cycle can be measured by dividing 1 by the period value.

Amplitude defines the difference between the maximum voltage and minimum voltage that can be generated by the PWM signal.

Duty cycles

Pulse-width modulation has a very important concept that makes it work the way it does, called a duty cycle. It represents the amount of time the signal is high during a period. This is called “on” time. Likewise, the amount of time the signal is low is called “off” time. The duty cycle is measured in percentage. When a digital signal is on half of the time, and off the other half, we would say it has a duty cycle of 50%. The on and off periods would be identical lengths so it would look like an ideal square wave. If the duty cycle is, let’s say, 75%, the digital signal will be high longer. If it was 25%, it would be high much shorter. You can see a graphical representation of these situations in the illustration below.

Another thing to keep in mind is that changing the frequency at which cycles happen does not change the duty cycle. Let’s say we have two 50% duty cycles, one at 10 kHz frequency and the other at 1000 kHz. You’ll notice that the one at a higher frequency appears smaller. However, if you looked closely at both cycles, you’d see that both have their signal high and low equally. The signals are high half of the time and low the other half. Thus, even though the duty cycles look different, they are still equal.

Calculating the percentage of the duty cycle

Knowing the percentage of duty cycle is a must when working with PWM, but how can we find it out? To calculate it, you will first need to know how long your period is. To make our calculations simple, we used 10 ms for the period in our example. The next thing you’ll need to know is either “on” or “off” time. In our example, we set the “on” time at 6 ms, meaning that the “off” time is 4 ms.

To get the percentage of duty cycle you just need to divide the “on” time by period. This is the same as you would calculate any percentage. So in our case, we would divide 6 (our “on” time) by 10 (our period) and get 0.6 as a result. Now we just need to multiply it by 100 to get the result of 60%. Thus, our duty cycle is 60%, meaning that our signal is high 60% of the time.

You can also multiply the “on” time by 100 before you divide it by period. This saves you some time when calculating and makes everything more legible. The formula in our case would then be x = (6*100)/(10) = 60%.

Calculating the output voltage

The output voltage of the PWM depends on how long the signal was set to high in the cycle, i.e. its duty cycle. The image below shows a signal that oscillates between 0V and 5V. The duty cycle is 60%, meaning the signal is set to high 60% of the time. We must have these two values to calculate the output voltage. To find the value of it, we simply multiply the maximum voltage by the duty cycle. So that would be 5 * 60%, resulting in 3V.

 

PWM on Arduino boards

On most Arduino boards, it is possible to use PWM via the analogWrite built-in function. The function takes a pin and the duty cycle value, ranging from 0 (0%) to 255 (100%), as parameters for making the PWM signal.

Note: not all pins are PWM signal capable. Check your board’s pinout before connecting. They are usually marked with a tilde sign (~) or an apostrophe (‘).

Connections

Let’s build a small Arduino project to understand better what PWM does. We will connect an LED to an Arduino board and fade it using the PWM technique. For this project, you will need:

  • An Arduino Board (we're using the Soldered NULA Mini)
  • A generic LED
  • A 220-ohm resistor
  • A breadboard
  • Generic jumper cables

The code

#define LED_PIN 19 // Pin to which the LED is connected

void setup()
{
  pinMode(LED_PIN, OUTPUT);
}

void loop()
{
  // Go from a 0% duty to a 100% duty cycle
  for (int i = 0; i <= 255; i++)
  {
    analogWrite(LED_PIN, i);
    delay(30); // Small delay of 30ms
  }

  // Reverse the duty cycle so that it dims
  for (int i = 255; i >= 0; i--)
  {
    analogWrite(LED_PIN, i);
    delay(30); // Small delay of 30ms
  }
}

If all goes well, you should be able to see your LED changing its brightness at a constant rate!

Products mentioned in this article

Related Articles