Controlling LEDs with Arduino

Pulse Width Modulation

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment introduces the concept of Pulse Width Modulation (PWM) and how it relates to Arduino and LEDs.


  • pwm
  • pulse width modulation
  • fading
  • led
  • arduino

About this video

Liz Clark
First online
09 April 2019
Online ISBN
Copyright information
© Liz Clark 2019

Video Transcript

In this section, we’ll go over controlling LEDs with pulse width modulation. We’ll be using the same materials from our previous section on blinking LED, as shown here. Now that we’ve blinked an LED, we can begin to move onto the next stage of LED control, beginning with pulse width modulation. But first, what is pulse width modulation?

Pulse width modulation, commonly referred to as PWM, is a way to simulate analog effects using digital means. As we discussed in the last segment digital control allows for a state of either on or off, with nothing in between. Analog control, though, allows for on and off, of course, but all states in between as well. This comes in handy when using things like potentiometers to adjust values in increments, which we’ll be looking at later.

A digital pin can simulate these attributes with pulse width modulation by pulsing on and off signals at varying frequencies to create the illusion of analog control. This happens so quickly, though, that to our human eye, we simply see smooth animation of whatever peripheral we’re affecting with our code. In this case, we’ll be using PWM to smoothly fade an LED on and off.

To use PWM on our Arduino, we’ll need to take note of which digital pins we’re using, as only certain digital pins have the ability to use PWM– specifically, pins 3, 5, 6, 9, 10, and 11. Don’t worry about memorizing those, as they are clearly labeled on the board with a squiggly line next to the PIN number to denote PWM.

Let’s take our breadboard from the blink circuit, and for now, keep one LED connected to the positive and ground rails. We can keep the wire for ground connected to the Arduino, but we’ll need to move the wire connected to pin 13, since pin 13 does not have PWM enabled. Instead, we’ll move the wire to digital pin 9. Now, much like how the code for blink was included as an example, you’ll find a simple PWM example as well that we’ll use to demonstrate this technique. Go to file, examples, analog, and then select fading.

In looking at the code, we can see that the pin for the LED is initialized differently than it was in the blink code– this time, being set up as an integer. There’s no set-up portion for this code, so we can go directly to the loop, which contains two functions to control the PWM. The first function allows for the LED to fade up to an on status by increasing the bness gradually.

PWM values are expressing in our Arduino as ranging between 0 and 255, with 255 being fully on. We see in the function that the bness of the LED is going to gradually increase from 0 to 255 in increments of 5. We can also tell that the purpose this function is to increase the value with the use of a plus equal sign before the 5.

This PWM value is stored in the integer fade value, and is then written to the pin with the analog right command. It’s for the purposes of the function that the LED’s pin on the Arduino is set up as an integer, since analog right is looking for two integers in order to work. There’s then a delay for 30 milliseconds, meaning that after the LED has reached its peak brightness, it’ll remain at that brightness for 30 milliseconds.

The second function essentially reverses the process that the first function introduced. Instead of increasing the brightness, the brightness is decreased by increments of 5, denoted with the minus equals expression. The same range of values are in use– zero to 255– and you’ll notice that the same statement of analog right is being utilized, since fade value is still in use in this function. It’s just holding a different value.

Again, there’s a delay of 30 milliseconds, so the LED will be off for that period of time. Once both of these functions are running in the loop, it will have the effect of constantly alternating fade up and down for the LED. Let’s see this in action by compiling the code and uploading it to the Arduino. You should see the LED fading nicely over and over again.