Chainable RGB LED

Start 

The Chainable RGB LED module has two universal sockets. One for data in and one for data out. You are able to control many of these modules with just one connection to the Arduino.

Both sides
Both sides
IN side
OUT side

Modules 

Gather the following parts to complete this project.

Parts

All Parts
All Parts
RGB LED
Potentiometer
Cable
PartQuantity
RGB LED 2
Potentiometer 1
Cable 3

Chainable RGB LED setup 

Take a cable and unwrap it. Plug one side into the IN socket of a chainable RGB LED and the other into the D8 socket.

Parts needed
Parts needed
Take a cable
Unwrap it
Plug one side into the IN socket of a chainable RGB LED
The other into the D8 socket

Upload

Upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <ChainableLED.h>

//Amount of chained LEDs
#define NUM_LEDS  2

//Create led object
ChainableLED leds(8, 9, NUM_LEDS);

void setup()
{
  //initialize LEDs
  leds.init();
}

//Variable to keep track of which led to update
byte pos = 0;

void loop()
{
  //loop through each led
  for (byte i=0; i<NUM_LEDS; i++)
  {
    //Toggles between the two leds
    if (i==pos)
      //set led to red
      leds.setColorRGB(i, 255, 0, 0);  
    else
      //set led to blue
      leds.setColorRGB(i, 0, 0, 255);
  }
  //Wait a small time so the leds don't flicker too fast
  delay(250);
}

Observe

The RGB LED on the module will be RED.

Red RGB LEDRed RGB LED

Modify

Take a cable and unwrap it. Plug one side into the OUT socket of the first RGB led and the other side into the IN socket of the second RGB led. Now one led will be red while the other will be blue.

Take another cable
Take another cable
Unwrap it
Plug one side into the OUT socket of the first RGB LED
The other side into the IN socket of the second RGB LED
The first is red and the second is blue

Experiment

The color is set via the leds.setColorRGB() function. It takes four arguments. What led you'd like to change in a chain, the red value, green value, and blue value. The color (255,0,0) is all red while (0,0,255) is all blue. Each color channel goes from 0 to 255. Using these 3 color channels you can create over 16 million different colors! Check out the color picker here to find your favorite and display it on the leds.

Color Spaces 

Red, Green, and Blue values (RGB) are one way to express colors or one color space. There are a couple other ones you might run into. If you've ever changed a color printer cartridge, you won't find RGB you'll find Cyan, Magenta, Yellow and Black which create the CMYK color space. Black is abbreviated with a 'K' because old school printing plates would have to be lined up using a 'key' that would be in black. Hue, saturation, and brightness are another color space. The color is set via the hue value, which usually range from 0 to 360. That range lines up with the degrees found in a circle. The primary colors red, yellow, and blue are space evenly apart at 120 degree intervals. Saturation is the amount of color expressed as a percent. 0 degrees hue would be red and at 100% saturation you have a very red .... red while at 0% saturation you'd have no red. The only colors that exist at 0% saturation are what would be found on a gray scale. The last number is brightness also expressed as a percent. 0% brightness displays black while 100% displays white. So for the red color found on the Arduino you want an HSB value around (0,90,40).

RGB Color SpaceRGB Color Space HSB Color SpaceHSB Color Space

HSB LEDs

We can set the chainable leds using the HSB color space. It's easier to change the color using code when you just need to change one value, the hue, to get a drastic color change. To show a rainbow effect we'd just need one variable and cycle it from 0 to 360. To do this in RGB values we'd need 3 variables and cycles those from 0 to 255 in every permutation. The leds.setColorHSB() function takes 4 arguments. The first is the position of the led in the chain, and the other three are the hue, saturation, and brightness; all ranging from 0 to 1. Check out the code below to see the HSB function in action.

Upload

Upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <ChainableLED.h>

//Amount of chained LEDs
#define NUM_LEDS  2

//Create led object
ChainableLED leds(8, 9, NUM_LEDS);

void setup()
{
  //initialize LEDs
  leds.init();
}

//Color variables
float hue = 0.0;
float amount = 0.025;

//Time to wait or switch color
int delayTime = 50;

//Variable to store color changing direction
boolean up = true;


void loop()
{
  //loop through each led
  for (byte i=0; i<NUM_LEDS; i++)
  //Change color based on HSL value
    leds.setColorHSB(i, hue, 1.0, 0.5);
    //Wait for a time
  delay(delayTime);

//If up is true hue increases
  if (up)
    hue+= amount;
//If up is false hue decreases
  else
    hue-= amount;

//If hue is the highest it can be, up is set false
  if (hue>=1.0 && up)
    up = false;
//If hue is the lowest it can be, up is set true
  else if (hue<=0.0 && !up)
    up = true;
}

Observe

The LEDs loop through the colors of the rainbow.

Modify

Changing the amount variable will change how many colors are shown. Changing delayTime will make the color changes faster or slower.

Adding Some Controls 

Take a cable and unwrap it. Plug one side into the rotary potentiometer socket and the other into the A0 socket.

Take a cable
Take a cable
Unwrap it
Plug one side into the rotary potentiometer socket
The other into the A0 socket

Upload

Upload the following code.

Download file Copy to clipboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <ChainableLED.h>

//Number of chainable LEDs
#define NUM_LEDS  2

//Socket with the dial plugged in it
#define dialSocket A0

//color variables
float hue = 0.0;
float saturation = 0.9;
float brightness = 0.5;

//Variables to store dial values
int currentValue = 0;
int previousValue = 0;

//Variable to count waiting periods
int count = 0;

//Creates LED object
ChainableLED leds(8, 9, NUM_LEDS);

void setup()
{
  //Initialize LEDs
  leds.init();

  //Start Serial Communication
  Serial.begin(9600);
}

void loop()
{
  //Store the dial value
  currentValue = analogRead(dialSocket);

  //If the value has changed
  if (currentValue != previousValue) {
    //Reset count variable
    count = 0;
    //If it hasn't changed...
  } else {
    //Increase count
    count++;
    //Wait a little before checking again
    delay(80);
  }

//If the count variable is bigger than 2
  if (count > 2) {
    //get dial value and convert to a hue
    hue = mapfloat(currentValue, 0, 1024, 0, 1);
    //Print that out to the Serial Monitor
    Serial.println(hue);
    //Set
    leds.setColorHSB(0, hue, saturation, brightness);
    leds.setColorHSB(1, hue, saturation, brightness);
  }

  previousValue = currentValue;
}

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Observe

Turn the dial than wait a moment. The color will change based on the dial's position. You could map the color value instantly but that introduces some flickering.

Color changing based on dial reading
Color changing based on dial reading
Color changing based on dial reading
Color changing based on dial reading

Modify

Try changing the saturation and brightness variables to be able to scroll through more colors.