The “Hello World” of Arduino Programming: Fading RGB LEDs

January 7, 2012 | Arduino | By: Mark VandeWettering

Allright, I was playing Skyrim most of the day, and didn’t really have my brain firing at it’s highest level when I sat down and decided to try to put something together. We’ve all been there, right? I realized that I had some of this cool RGB LED strip that I ordered from Tayda Electronics, and it was just lying there, mocking me. So, I embarked upon quite easily the simplest things that I have ever done: cross fading RGB LEDs. Going boldly where everyone has gone before: it’s my motto.

First of all, complete credit must go to Lady Ada and her terrific tutorial on this very subject. I shamelessly copied what she did, and it worked out great. So go there, and read everything she had to say. I’ll just add a few comments:

  • The RGB strip cuts very easy, and strips pretty easy too. Be careful to use good ventilation when soldering leads onto this stuff though, as the plastic/rubbery stuff residue doesn’t smell very nice when heat is applied.
  • I went ahead and ordered the STP16NF06 MOSFETs that she recommended when I placed my last Digikey order. Price was about $.85 each, which is quite reasonable.
  • Tayda’s RGB strip seems like a very good deal, and you could easily use this trip for a wide variety of craft, art, or electronics projects.

To experiment, I cut off four segments (a little under a foot) and soldered on some leads, and wrapped them with electrical tape. Rather than swipe Lady Ada’s code, I quickly penned this up:

const int rPin = 3 ;
const int gPin = 5 ;
const int bPin = 6 ;

void
ramp(int pin, int from, int to)
{
  int i ;
  
  if (from < to) {
    for (i=from; i<= to; i++) {
      analogWrite(pin, i) ;
      delay(5) ;
    }
  } else {
    for (i=from; i>= to; i--) {
      analogWrite(pin, i) ;
      delay(5) ;
    }
  }    
}

void
setup()
{
   pinMode(rPin, OUTPUT) ;
   pinMode(gPin, OUTPUT) ;
   pinMode(bPin, OUTPUT) ;
   
   analogWrite(rPin, 0) ;
   analogWrite(gPin, 0) ;
   analogWrite(bPin, 0) ;
   
   ramp(rPin, 0, 255) ;
}
   
   
void
loop()
{

  ramp(gPin, 0, 255) ;
  ramp(rPin, 255, 0) ;
  ramp(bPin, 0, 255) ;
  ramp(gPin, 255, 0) ;
  ramp(rPin, 0, 255) ;
  ramp(bPin, 255, 0) ;
}

And it worked the first time! Here’s the YouTube video:



It’s really easy to get the electronics working, and the Arduino and it’s software environment make it so simple. Too simple. I’m sorry to have embarrassed you with this, but perhaps you’ve had a craft or art project that could use some LEDs kicking around in your head, but you haven’t been kicked into action. Perhaps the easiness of this will inspire. If so, my work is done!

Have a good one.

Share Button
Be Sociable, Share!

Write a comment






− 5 = four