Wednesday, January 9, 2013

Day 5: Analog Write and Pulse Width Modulation (~)

Circuit #4 in the Sparkfun Inventor's Kit isn't anything too exciting in terms of introducing you to new stuff. It basically teaches you how to create a circuit with eight LEDs which you can program to turn on and off in various patterns. 

Like I said, there is almost nothing new introduced in this project, and yet, it brings a lot of things together. In fact, this is probably the first circuit that really gave me a view into a small fraction of the things that the Arduino is capable of. You see, even with just eight LEDs, you can make them light up one after another, light up all at once, or light up completely randomly. The program provided with the circuit provides a nice array of functions that allow you to generate some fun patterns with the LEDs. It's a great exercise, and a great opportunity to play around with programming a simple circuit like this one to create some fun results.

I decided that for my own project related to circuit 4, I would replace the digitalWrite function that is used in all of the various patterns with the analogWrite function. This means that instead of having the lights simply turn on and off in succession, the lights would pulse on and off slowly. I thought it would be simple enough. Basically, I took the for loops that had the lights turn on and off in succession, and added another for loop within it to gradually raise the brightness of the LED. See code below.

void glowOneAfterAnother()
{
  int index;
  int x;
  int delayTime = 10; // milliseconds to pause between LEDs

  for(index = 0; index <= 5; index++) // this loop goes through each LED
  {
    for(x=0; x <=255; x++) //this loop gradually raises the level of analogWrite from 0 to 255
    {
      analogWrite(ledPins[index], x);
      delay(delayTime);
    }      
  }

You may notice that I mentioned 8 LEDs before, but only 6 are mentioned in this code. This is due to a small hitch I encountered along the way.

When I first ran my program with the original 8 LEDs, I encountered the strange situation where about half of my LEDs gradually turned on as expected, and the other half simply turned on as if I had used digitalWrite. After double checking my code and double checking all my circuit connections, I was left scratching my head. After staring at my board for a while, I realized that all of the pins that were working as expected were plugged into digital pins with a tilde(~) next to the pin number. Apparently, only these pins are capable of what is called Pulse Width Modulation. While I don't know the exact details, pulse width modulation is basically the rapid flickering on and off of the pin that allows for the simulation of analog output required to use analogWrite. Apparently this is also useful for doing things like controlling the speed of motors. 

In the end, to get the circuit to work properly, I had to restrict the pins I used to the ones with a ~ beside them. Unfortunately there were only 6 so I had to remove two of my LEDs.

So that was my adventure with analogWrite. I'm glad it gave me the chance to understand the Arduino a little better. Now I can sit and bask in the glory of my 6 gently pulsing LED lights.

No comments:

Post a Comment