Like many light bulbs, LEDs can not only be turned on and off; they can be dimmed (or made to look dimmed) as well.
By changing the intensity of LEDs, you can represent the value of a sensor, create cool effects like pulsing and breathing, or signal that your Netduino app has important information to share.
You can also group LEDs together to represent numbers. And you can combine colored LEDs together to form new colors. You can even dim those colored LEDs, generating millions of color combinations.
To dim an LED (i.e., change its intensity), you simply turn it on and off quickly. If the LED is on half the time and off half the time, it will appear about half as bright to the human eye. If you turn it on only 10% of the time, it will seem very dim. While it is possible to simply turn LEDs on and off rapidly using looping on and off commands in code, the Netduino’s microcontroller has an integrated feature especially suited for this task. This feature is called Pulse Width Modulation (PWM) and, if you tell it the percentage of time to toggle the LED on, it will automatically dim the LED by pulsing it rapidly. This is all done in the background, which frees up both the microcontroller and your code to focus on more important tasks.
Previously, you turned on and off an LED using the
OutputPort
feature of .NET Micro Framework. Now you’re
ready to update that code to set the LED’s intensity using the PWM feature
instead.
First, you’ll need to wire up an LED and a potentiometer using a breadboard.
If you have a MakerShield, you can use its integrated potentiometer and one of its LEDs for this example. Chapter 5 tells you everything you need to know about the MakerShield.
3.15.26.221