Fade an LED in and Out
In the prior Blink lab, we turned an LED on an off at different speeds.
But what if we want to slowly turn on our LED on and off?
In this lab we will show you how to dim your LED to any brightness level you want.
Using Pulse Width Modulation (PWM)
Although digital computers are good at quickly turning signals on and off, they don't really allow us to easily set an output to a given voltage level without complex circuits. But there is an easier way to adjust the brightness of an LED! We can quickly turn the signal to the LED on and off. We can do this so quickly that you can't even see it flicker. Controlling the amount of time a signal is on is all about controlling the width of the ON pulse. That is why this is called Pulse Width Modulation or PWM for short.
With a PWM design there are two things we need to tell the microcontroller:
- How often do you want a square wave to go on and off?
- How wide should the on part of the pulse be (relative to the total width). This is called the duty cycle.
The rate of change of the pulse is call the frequency. You can set the frequency to be 1,000 changes per second (1K), which is much faster than the human eye can detect. This is done using the following line:
1 |
|
Note that we can slow the frequency way down and the dimming effect will still work. As an experiment you can change the PWM frequency to around 20 and you will see a distinct flicker as the LED turns on.
Using a 1K frequency is a good practice when you are using LEDs. If you use a very low frequency of around 20 chances per second the human eye will start to see flicker. Keeping the frequency well above 20 is a good idea and using frequency higher that 1K will not help and can cause additional noise in your system.
Here is the sample program that will slowly dim the builtin LED that is on pin 25:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Note that the duty cycle starts at 0 (always off) and moves slowly up to 65,025 (always on). It then does the reverse and slowly dims the LED and then repeats. There is only a 1/10,000 of a delay between these changes so the LED will completely turn on in about six seconds before it starts to dim again.
PWM Functions
Here is a list of the PWM functions.
1 2 3 4 5 6 7 8 |
|
PWM Cleanup
If you are using PWM to control sound and motors, make sure you deinit()
to de-initialize the PWM controller after you are done. You may have to trap the stop to do this. For example if a PWM is driving motors, your Stop must send deinit()
to each motor controller. See the Interrupt Handlers for details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Full PWM Example with Init and Cleanup
If you have many PWM objects then you can centralize all the hardware initialization and cleanup functions so they are created and shut down in a consistent way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
Suggested Exercises
- Change the frequency from 1,000 to 500, 100, 50, 40, 30, 25, 20, and 10. When can you just barley see it flicker? What does this tell you about the human eye?
- Can you add a delay so that the LED stays on at full brightness for one second before it starts to dim again?
- Can you add a delay so that the LED is completely off for five seconds and then goes to full brightness and off in one second?
- What lights in your home would you like to see slowly dim on and off? How could you modify a light (safely) so that it slowly dimmed on and off. Would PWM work with all lightbulb types such as tungsten filament bulbs that take a long time to heat up and cool down?
- Can you hook up a set of red, green and blue LEDs program them to fade in and out to display all the colors of the rainbow (red, orange, yellow, green, blue, indigo and violet)?
- When you stop the program does the LED stop changing brightness? Does it retain the value that it had when you pressed the Stop function? What does that tell you about how main CPU and the role of PWM? Note that we will cover up doing "cleanup" events that stop all PWM activity in our Interrupt Handlers Lab