Adding PWM For Motor Speed Control

What is PWM?

PWM is a really useful tool that can be used for loads of different applications but for us we will use it for dimming the LED and controlling the speed of the motors.

Take a look at this illustration of PWM:


Let’s look at the 50% duty cycle first. If you take a 5 Volt supply and switch it on and off so that it is on half the time and off half the time you get an average voltage of 2.5V.

If you change this slightly , leaving it turned on for 75% of the time an off for the remaining 25%  that means you have 75% of 5 Volts which would be 3.75 Volts. (75% Duty Cycle in image above)

It’s all about the average voltage when it comes to PWM. Take a look at the red line in each of the graphs above. As the duty cycle increases the red line get’s higher. This is the average voltage increasing as the duty cycle increases.

Basically, the less ‘on’ time you have, the lower the average voltage. The frequency of the PWM signal is application dependent but it’s typically a couple of kHz. This means that the signal is switching on and off several thousand times per second so for things like LED dimming and motor control, you would never be able to tell that the power was getting switched on and off because it’s happening so rapidly.

PWM using the Arduino:

As you may have noticed by now, the Arduino Nano has many GPIO pins and some of them have special functions. Some of them are serial ports, analog-to-digital converters, interrupt enabled etc. Some of the pins also have this hardware PWM function built-in. What this means it that it’s really easy to get the PWM up and running and it required a minimum of code to do it.

the analogWrite() function is used to set up PWM.


As you can see , it takes two arguments. The first one if the pin number and the second is PWM value. The pin number is just the pin you want to use. The PWM value is related to the duty cycle of the PWM signal (See image above for example of duty cycles).

The value of PWM signal can be anything between 0-255.

0 would be 0% duty cycle (fully off)

255 would be 100% duty cycle (fully on)

128 would be 50% duty cycle

191 would be 75% duty cycle

63 would be 25% duty cycle

It would be a bit clearer if the value could just be between 0-100. Since we can choose 0-255 it means we can be more precise in choosing the PWM duty cycle since each number from 0-255 is 0.39% of duty cycle.

For example 128*(0.39%) = 49.92% (roughly 50% duty cycle)


Example 1 : LED Dimming

You’ve already added an LED to pin D11 on the Arduino and blinked it. Now instead of blinking it , let’s use PWM to dim it.


We only need two lines of code to get the PWM going. The first is to set the pin as an output, then call the analogWrite function to set the PWM duty cycle for that pin. Upload this program to your Arduino and look at the brightness of the LED.

Change the duty cycle value in analog write to a different value and upload the program again. Observe what happens to the LED brightness. Try a low value and a high value to see a real difference.


Example 2: Varying the PWM value


In this example we have a for loop with the variable i getting incremented from 0-255. Each time i is incremented by 1, the new value is then written to the PWM pin using analogWrite(). This will give a fade effect.

Upload the program and see what happens.

What happens it you make the delay longer?

What happens if you make the delay shorter?





This entry was posted in Tutorial and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *