Blog 24

From BitWizard WIKI
Revision as of 12:59, 7 January 2016 by Cartridge1987 (talk | contribs) (The code)

Jump to: navigation, search

Working with RGB leds on the WS2812

For Linux/Raspberry:

apt-get install ckermit

If you use and other device, or want to know more:


For Windows users it's optional to use:


RGB lighted paper Tree

Hardware used:

Software used on my linux pc:

  • C-kermit

Making the construction

The RGB leds:


On ////J7 a connector is added, also an cable from the J1 was connected on pin 15 with the second pin from J7. The image further on explains itself. You still have to look out, that you let the pointers point away from the usb ws2812.

Paper tree: The paper tree is made by folding a green paper. On the folded green paper I put my RGB led connection. Around the RGB led I draw a Christmas tree. At the point every single RGB led were laying I put a dot with a pencil. After that I cut the tree and put holes only on paper with the pencil dots. After that I used some scotch tape to bring it all together. The final result:


You can of course go all crazy with the tree by adding glitters and stuff like that.

The code

The full c-program RGBTree.c can be downloaded: here.

What the code does in short is this: When starting up it will make the the top led (the peak) white. It will then read the for statement where it goes through all the leds under the peak. Every led will randomly get the color green or red. Then the script will keep fading the colors of the leds from to the opposite color. ( red to green or green to red )

The parts of the script that I will give some explanation:

Here I made a list of colors(if you also want other colors search for: color table):

#define WHITE  0xffffff
#define YELLOW 0xffff00
#define RED    0x800000
#define GREEN  0x008000
#define BLUE   0x0000ff
#define BLACK  0x000000

Because it's RGB ( red green blue ) the first ff is making the red led go to it's maximum. The middle ff is for green and the last for blue. It works in hexadecimals so you can lower or make the density higher or lower. And mix them so that you can get different colors as for example yellow.

Here the amount of time and steps in milliseconds is given for to make the fading go fluent.

int nfadesteps = 30;
int delayms = 30; 

int interpolate (int c1, int c2, int shift, int pos, int end)
  c1 >>= shift;
  c2 >>= shift;
  c1 &= 0xff;
  c2 &= 0xff;

  return c1 * (end-pos) / end + c2 * pos / end;

Here the colors are given. void fadeto recieves the information from int main for example: (5, RED, GREEN). It will give the information to every single part of the rgb led. So in this example it would be that red will change from 80 to 00.

void fadeto (int pixnum, int col1, int col2)
   int i; 
   int r, g, b; 

   for (i=0;i <= nfadesteps;i++) {
      r = interpolate (col1, col2, 16, i, nfadesteps);
      g = interpolate (col1, col2,  8, i, nfadesteps);
      b = interpolate (col1, col2,  0, i, nfadesteps);
      printf ("pix %d %06x\n", pixnum, 
	 (r << 16) | (g << 8) | (b << 0)); 

      usleep (delayms*1000);

After that the peak led will get white.

int main (int argc, char **argv)
  int nleds = 10;
  int *pixels;
  int pixnum, newcolor;
  int i;
  if (argc > 1) 
    nleds = atoi (argv[1]);

  pixels = calloc (nleds, sizeof(int));
  printf ("pix %d %06x\n", nleds, WHITE);  

The for statements is the first loop that is getting used. It will count from zero till nine. Every time it counts it will get a random number and use a remainder to divide it with. The result could be a zero or an one. If it is zero the color red will be given else it would be green. After that it will directly be printed, so that i can be changed to the next number.

  for (i=0;i < nleds;i++){  
    if (random () % 2 == 0)
       pixels[i] = RED;
       pixels[i] = GREEN;
    printf ("pix %d %06x\n", i, pixels[i]);  

In the while statement it has to choose a random led. It does this by giving a random number and remainder it with nleds. If it then has chosen the certain led it will look if the led is red. If that is true it will say that the newcolor has to become green. Else the opposite will happen, so that the led will become red. The given values will then be sended to fadeto. ( Example: (5, RED, GREEN). After that it will save the newcolour in the previous led color. so pixels[05] would become green.

  while (1) {
     pixnum = random () % nleds; 
     if (pixels[pixnum] == RED) 
	newcolor = GREEN;
       newcolor = RED; 
     fadeto (pixnum, pixels[pixnum], newcolor);
     pixels[pixnum] = newcolor;
  exit (0);


Useful links