Monday, July 1, 2013

Adjustable RGB LED Color Cube

     I took a sculpture class during my last semester at Penn State so I could meet the required number of credits to graduate. One of the last projects in that class was to create an interactive art installation. I went through a few ideas in my head and doodled them in the Paper app for iPad. You can view the PDF of my ideas here. After I had a good idea of what my design would be, I ordered the parts I did not have on hand from Amazon and eBay. I ordered an Arduino Mega clone, the SainSmart MEGA 2560 Board for Arduino, some Common Cathode RGB LEDs, and some slide potentiometers. I also made a trip to the local hardware store for materials for the overall structure. Before the break is a video explaining the operation of the final project. After the break, I’ll explain how I created the three main pieces to this project - the electronics, the code on the Arduino that controls everything, and the casing for both the base and the controller.

     I quickly found out that a single RGB LED wasn’t bright enough to provide the amount of light I wanted for each side. After some experimentation, I discovered that the RGB LEDs can be soldered in parallel and continue to operate in the same manner as a single LED. A lot of delicate soldering and a few headaches later, I created four RGB LED strings consisting of four LEDs each and long enough wires to connect them to a breadboard. I mounted the necessary resistors and wires to a mini breadboard so I could easily attach the LEDs when it’s all said and done. The anodes of each one of the three LEDs are attached to one of the Arduino’s PWM pins through a current limiting resistor, while the common cathode is attached to ground. I used a 180Ω resistor on each of the red LEDs and 100Ω resistors on each of the green and blue LEDs. This produces less current for the red LED which balances the overall intensity of the three LEDs.

Strings of four LEDs

Resistors organized to create the correct intensty for each set of LEDs

     I had to use the Mega variant of the Arduino for this project because it supports the 12 PWM signals I needed for the LEDs, the three analog inputs I needed, and four pins with interrupts. The only thing on this list that an Arduino Leponardo would support is the three analog inputs. I chose to use a SainSmart MEGA 2560 Board for Arduino because I was on a tight college budget for this project. It’s ⅓ of the price, and from my experience with it, functions exactly the same as an official Arduino Mega. I would certainly recommend this board for anyone that plans on making the Mega board a permanent part of a project.

SainSmart Mega 2560

     With the outputs of the project complete, I had to focus on the inputs. I connected four buttons to the Arduino on pins with interrupt support so I could control which side of the cube would be selected to have its color change. The buttons are attached to some pull-up resistors to ensure button pushes are detected. There are three slide potentiometers that each control the intensity of one of the colors (red, green, or blue) of the LEDs on the selected side. These potentiometers are attached to an analog input port that can read the value with 1024 bit precision.

Potentiometers are attached with the green wires to the Analog In
pins in the front and the push buttons are attached through
the messy header to pins in the back

     I created a 3D-printed case for the controller that places the potentiometers and buttons in a logical order to make control of the light box intuitive. The files for the controller can be found on my Thingiverse. The potentiometers are placed side-by-side with red on the left, green in the middle, and blue on the right (RGB). When the potentiometers are at the bottom, the value is zero and the LED is off, and when the potentiometer is at the top, the corresponding LED’s color is at full brightness. With the analog value being read and a PWM signal being sent out, sliding the potentiometer from the bottom to top with increase the brightness of that color with respect to the position of the knob. The buttons are placed in a diamond pattern, with the button on the bottom controlling the LEDs on the side facing you, the button on the top controlling the side facing away from you, and the buttons on the left and right controlling their respective sides. After carefully measuring the buttons and potentiometers, I created a faceplate to hold the various inputs. The potentiometer cutouts were the perfect size for them to “pop” in and stay put and the buttons are threaded and screwed into place so they cannot move. With the inputs installed in the faceplate, I then soldered long wires to each of the pins to connect them to the Arduino which will be located in the base of the sculpture. Next, I created another 3D printed part that holds the faceplate at an easily accessible angle and hides the wiring from view. Unfortunately, the part shifted in the middle of printing when I wasn’t looking (I believe this was a software error, but since I didn’t see it happen, I can’t be too sure). To correct this piece, I cut it with a utility knife at the overhang and glued it back together with hot glue. I used my Black Liquid Electrical Tape to cover any deformities in the print. I was surprised at how well it covered any mistakes in the matching black ABS plastic. I used spiral cable wrap to conceal all of the wires that connected the controller to the Arduino.

Potentiometers and buttons mounted in the faceplate

Before (left) and after (right) fixing the print with liquid electrical tape

Final casing for the controller

     The case for the LEDs and the Arduino was the most time-consuming part of the project. I used Plexiglas for the windows into each of the LEDs and for the top of the case. Unfortunately, the Plexiglas that I had was over twenty years old and took me many hours to remove the protective paper backing from each side. If I didn’t already have the pieces cut to size and had no access to any cutting tools, I would have certainly thrown this junk away and bought some new stuff. After I managed to remove all of the protective paper, I rubbed the sides down with Scotch-Brite to make the Plexiglas matte and help spread the light from the LEDs. Next, I cut down a sheet of white corrugated plastic that I bought at the hardware store for the remaining support of the structure. Using pieces of the Plexiglas, white plastic, and white electrical tape, I slowly built out the frame for the structure. I carefully measured space for the holes that the LEDs will be inserted into and created an internal brace for all of the LEDs. The structure was designed so you could only see the color of the LED of the face you are looking at or could see the color of all four sides through the top window.

LEDs mounted in the internal brace

LED brace mounted in the center of the Plexiglas cube

     The code that controls all of the pins via the Arduino is fairly simple. The code is located on my GitHub as a gist. It seems long because there’s so many pins, but it boils down to about four pieces. The first piece is initializing the pins and some variables. The potentiometers and LEDs are connected in a row to make things less confusing to build the circuit. I have a brightness variable that stores the analog input from the potentiometer that is used to send a PWM signal to the LEDs. The other variable is used to pick which side of the cube to control. In the setup() function, I set all of the LEDs to outputs and set up the interrupts for the buttons. Using interrupts on other microcontrollers seems daunting, but Arduino makes this very simple. One thing that tripped me up initially was how to fill out the attachInterrupt() function. Using the reference documentation from the Arduino website, you can see that there are three parameters – interrupt, function, and mode. Using the table from the top of that website, you first pick out the board you’re using (in my case, it’s the Mega2560), and then go across the row and find which pin you want, and finally go up the column to find the interrupt number. I’ll explain how the attachInterrupt(5, one, FALLING); statement in my code works. The interrupt in that function is int.5, which is pin 18 on the Arduino Mega. The mode “FALLING”, triggers the function “one”, when the signal on pin 18 transitions from high (5V) to low (0V). The function “one” has the sole responsibility to set the variables that determine the side of the cube to the right position. In this example, it sets oneON high and makes the other three sides of the cube low. I have four of these functions to go with each of the four push-buttons that could trigger an interrupt. The final piece of code is the loop() function, where the fun stuff takes place. The first thing that happens is the 1024 bit precision analog value read from the potentiometers is scaled to a 256 bit precision analog value that can be output on a PWM pin. Next, are four if() statements that allow control of each of the four sides. This code only functions because the condition is controlled by the interrupt and as soon as a button is pressed to pick a side, the interrupt function is called and changes the value of the four conditions. That's how those 112 lines of code work. This was a pretty long-winded explanation of fairly simple code, but I wanted to make sure that anybody could understand what’s going on here.

     What started out as a simple art assignment turned into a fun tinkerering project. I really like how the final result turned out.  It was fun to take some low-cost materials and create a nice-looking "piece of art" in the end. There's a lot more photos of the project as it was in-progress available via this photo album. I hope this post will inspire someone to grab an Arduino and get their hands dirty on a LED project of their own. Below is another video of the Color Cube in action. Enjoy!