Pulse width modulation (PWM) is a technique used to reduce the average power delivered by an electrical voltage. This voltage variation is obtained by rapidly switching this voltage source on and off.

The pwm is used for components that work with inertia like motors because it will not be affected by fast changes as its reaction time is long due to its inertia.

The pwm is also used a lot to vary the brightness of a led on Arduino.

The led is turned on and off very quickly so that we don’t see it blinking thanks to persistence but just a lower brightness.

How PWM works?

The purpose of the pulse width modulation is to create an alternative between the highs and lows of the Arduino board, in order to have half the power for example. For this there will be cycles of periods and the pin will be turned on and then off.

For example if we take a period of 20 ms, which gives a frequency of 200hz (f=1/T), if the led is on for 10ms and off for the same time, then it will be a power divided by two or 50% of its maximum brightness.

If for the same period the led is on for 5 ms and off for 15 ms, the brightness will now be 25% of its maximum power.

PWM on Arduino

On an Arduino uno board, you have 6 pins that delivered the pwm. They are recognizable by the symbol ~. These are pins 3,5,6,9,10,11.

To use the pwm, there is a function made for that, called analogwrite. This function takes as parameters the pin you want to turn on and a number between 0 and 255 corresponding to the voltage. 0 corresponds to 0V and 255 to 5V.

analogWrite(pin, value)

Why a number between 0 and 255?

The value used for the voltage is 1 byte. A byte contains 8 bits which can all have a value between 0 and 1. 2^8 = 255 possible values of tension.

Motorshield Card

The motorshield card is used to control a motor. It will allow you to control its speed and direction (clockwise or not).

With this card you can control up to two DC motors at the same time, vary their speed, change direction with an h-bridge. Some boards are even able to control a stepper motor.