Skip to content

Pulse width modulation

In this tutorial I cover the following topics

Microcontroller's pins

I explain different type of microcontroller's pins in Different voltage level coexistence part. Please refere to section Type of pins if this topic is new for you or you don't remember this material.

Experiment 1

Turn on and turn off LED with low rate (1000ms for each state). Voltage changes from low level to high level and back.

Turned off (low voltage level, close to 0V):

Turned on (heigh volatge level, close to 5V):

Experiment 2

High speed switching (1ms for each state).

Now voltage is very stable, around 2.2V:

This is in accordance with expectation as 1/(1+1) * 5V = 2.5V. Because as we have seen in Experiment 1 high voltage is not exactly 5V but 4.5V so 1/2*4.5V=2.25V.

Circuit with two "reference" LEDs: one (most left) always turend off, and second (most right) always on:

Experiment 3

High speed switching but with different length of low and high state.

High 1ms, low 2ms. Expected voltage is 1/(1+2)*5V = 1.66V. Taking into account that volatge is rather 4.5V than 5V, expected result should be: 1/3 * 4/5V = 1.5V:

2/3 * 4.5V =3.0V:

1/10 *4.5V = 0.45V = 450mV:


The proportion between HIGH state and LOW state are important as this decide about (averaged) output voltage. All below timings have the same proportion:

but, as you have seen, output volatage was the most stable in the last case, where total time of one HIGH-LOW cycle was the shortest.

In consequence, you can say that both proportion and time window (total time of one cycle) is important.

PWM -- Hardware switching

Pulse Width Modulation

100% duty cycle (always on):

50% duty cycle (on half the time):

10% duty cycle

Application 1: LED

Unfortunately Tinkercad seems to have some problems in this simulation (LED state in case 2 and 3 is incorrect).

0% duty cycle (LED is off)

50% duty cycle (half LED brightness)

100% duty cycle (full LED brightness)

Application 2: servo

Similar approach as for LED can be applied to control servos.

0% duty cycle

50% duty cycle

100% duty cycle