3rd Inning

From LVL1
Jump to navigation Jump to search


Tri-color LED fading

This is the way to hook up a tri-color LED. Basically you are hooking up 3 leds that have a common ground. The average voltage that each led uses to power itself is varied using pulse width modulation. The code basically does a neato nested loop to vary the colors. This is a fun circuit to play with. Make sure to use the resistors or you may burn up your LED - The cost a couple of bucks!

Components Needed

  • Freeduino or Arduino or clone
  • USB cable for Freeduino
  • Freeduino development software - download here!
  • Solderless Breadboard
  • Hookup wire 22gauge solid
  • Common Cathode LED

How to

  1. refer to the fritzing drawing to place the wires
  2. upload the code below using cut and paste
  3. Run and watch the fading of the led!


Lvl1-Inning3 schem.jpg


Lvl1-Inning3 bb.jpg


int RED = 9;    // RED pin of the LED to PWM pin 0 
int GREEN = 10;  // GREEN pin of the LED to PWM pin 1
int BLUE = 11;   // BLUE pin of the LED to PWM pin 2

void setup()
  // nothing for setup 

void loop()
  for(int r = 0; r < 1024; r+=5) {
    for(int g = 0; g < 1024; g+=5) {
      for(int b = 0; b < 1024; b+=5) {
        analogWrite(RED, r);
        analogWrite(GREEN, g);
        analogWrite(BLUE, b);


  • is your led in correctly
  • code for the pins on Arduino correct


Most of this example was taken from here.