Dimming

In our programs so far, we have been simply turning the LEDs on and off.

But as we saw when we were playing with LEDs and chips earlier, we can use Pulse Width Modulation to control the brightness of the LED, so we can make it brighter or dimmer as we please.

With the computer program, we have several advantages over the 555 timer chip. For one, we can control 16 LEDs at once (I only used 8 in this program, but Port 2 is also available).

In this program, we loop 256 times, and the first LED is only on for two of those cycles, so it is less than 1% on. The next LED is on for 4 cycles out of 256, or about 1.5% on. By the time we get to LED 6, the LED is on for 50% of the time.

Hardware Pulse Width Modulation

We don't have to rely on software to do our Pulse Width Modulation. The chip in the Launchpad has three built-in timers that can be connected to some of the pins, and those timers can do all the work for us, if we are willing to live with the limitation that only some pins can be used.

Unfortunately, these limitations make discussing hardware Pulse Width Modulation on the Launchpad look quite complicated, and make the analogWrite() function behave in a funny way. We will keep things simple for now, and leave the complicated stuff for the next section, which you are free to skip over.

The simple way to use hardware Pulse Width Modulation is to simply pretend that the Launchpad can only change the brightness of three LEDs. These are the Port 1 Bit 2, Port 2 Bit 1, and Port 2 Bit 4. These are connected to pins 4, 9, and 12.

The program shown above sets the brightness on three LEDs to low, medium, and bright (levels 4, 16, and 64). Note that since the hardware is doing all the work, all we do is set up the levels in the setup() function. There is nothing we need to do in the loop() function, so we leave it empty. Of course in a more useful program we might have something interesting happen there, and it would probably do something to change the brightness of the LEDs.

Since we can control the brightness of three LEDs at once, we can make an RGB LED look like any color we wish. An RGB LED is actually three LEDs in the same device, a Red one, a Green one, and a Blue one (hence the name RGB). When these colors are mixed, they can create all the colors of the rainbow. Turning them all on at once makes white. Turning red and green on makes yellow. Turning the LEDs on at different brightnesses gives us all of the colors in between.

 

Here are some programs that dim LEDs in interesting ways:

Comet

Comet 16 pins

Throb

 

Arduino versions:

PWM LEDs

Simple Hardware Dimming

Comet

​Throb