Raspberry Pi Mood Cube

A PWM RGB colour changer using WiringPi : projects.drogon.net/raspberry-pi/wiringpi/ and it’s software PWM handler.

Ooh pretty colours! This is an old ‘mood cube’ bought years ago because I quite liked the perspex box and have a thing for shiny cubes. It has 8 sets of red, green and blue LEDs along with some electronics to randomly change the colours. I gutted it, leaving just the LEDs. Wired together; all R+ to pin 15, G+ to pin 9, B+ to pin 7 on the Raspberry Pi.

A simple C program loops forever reading RGB values (0 to 100) from a text file and sends PWM signals via WiringPi’s library to pins 7,15,9.

Web front end (running from lighttpd on the RPi). Displays R, G and B jquery sliders; on change posts to php script that saves the RGB values to the mentioned text file.

 * mood_cube.c
 * Simple PWM test program to read RGB values from a text file and send as PWM 

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <softPwm.h>

int main ()
  int fr,fg,fb;
  char line[80];

  FILE *fbuf;            /* declare the file pointer */

  printf ("Raspberry Pi PWM test program.\n") ;

  if (wiringPiSetup () == -1)
    fprintf (stdout, "oops: %s\n", strerror (errno)) ;
    return 1 ;

  softPwmCreate(7,0,100); // set pin 7 as pwm range 0 - 100
  softPwmCreate(15,0,100); // set pin 15 as pwm range 0 - 100
  softPwmCreate(9,0,100); // set pin 9 as pwm range 0 - 100

    fbuf = fopen ("/var/www/rgb/rgb.txt", "rt");  /* open the file for reading */

    while(fgets(line, 80, fbuf) != NULL)
      /* get a line */
      sscanf (line, "%d %d %d", &fr,&fg,&fb);
      printf ("%d %d %d\n", fr,fg,fb);

    fclose(fbuf);  /* close the file */

    softPwmWrite (7, fb);
    softPwmWrite (15, fr) ;
    softPwmWrite (9, fg) ;

  return 0 ;

One thought on “Raspberry Pi Mood Cube

Leave a Reply

Your email address will not be published. Required fields are marked *