Thursday, July 26, 2012

The Color Jar - Microcontroller-driven fun for the kids

I finally managed to spend some time on a new electronics project at my workbench. Major credit for the idea goes to Paul, author of the Fangletronics blog. He is also a father and electronics tinkerer, so his blog certainly appealed to me. One post in particular (Pixie-Dust Bottles) stood out as a fun and relatively simple potential project, a good programming exercise, and something I thought my boys would enjoy. Shown at left is the final product, what I've been calling the Color Jar. It's a frosted plastic jar with a knob on top. As you turn the knob, the RGB LED light turns on inside the jar, and it smoothly cycles through all the colors of the rainbow, based on the position of the knob, essentially letting you "dial-in" a color.

The original design was built around an ATTiny13 microcontroller, but it ported over very easily to the ATTiny85 chip which I had on hand, as it has the same pinout but with more memory. I worked out the circuit on a breadboard, and then soldered it up on some proto board, and mounted it to the potentiometer to make it into a nice compact package. I made sure to use a chip socket for easy removal for reprogramming, which came in handy. The other major design change was the battery. Since my plastic jar allowed some more space in the lid, I decided to use a larger 3 volt CR2 "Photo" battery (not a small watch battery) that should last longer.



For the most part, I used the programming from the original pixie dust bottle project, but had to make a couple updates & fixes for my project.

First was a hardware issue. I bought my potentiometer (with switch) from Radioshack, which was convenient, but turns out it was not linear. That is, the voltage change was not directly proportional to the amount of rotation. In practice, this meant that for the first half of the knob's rotation there was almost no voltage change, so for most of the rotation the light would only show red. I took a series of voltage measurements at different degrees of rotation, and used Excel to graph out the signal curve. From there I was able to come up with a formula to take my input signal and adjust it to a more linear value. This allowed me to more evenly spread each color over the full rotation of the knob. The extra-low voltages at the bottom of the range also made it difficult to get consistent readings, which made my light flicker a little. I was able to smooth this out by taking 10 input readings per cycle with a 2 millisecond delay between each and averaging them to get my final input value.

The other issue was a programming bug in the code I was using. At a certain point between green and cyan, the LED would start flashing/flickering red. Looking at the code I had a hunch it was a math error in the way the RGB color was being calculated. To be sure, I did the math and found out it was calculating a Red value of -2, when normally this should be between 0 and 255. It might be fun to refactor the code and come up with my own algorithm for calculating the colors, but for now I just left the main logic in place, and then added a few lines to force any values below zero back to zero, and anything over 255 set back to 255, to keep everything in the appropriate range. This got rid of the flickering, and overall I'm happy with the performance of the Color Jar.

While Nathan may not appreciate the electronics and programming running inside it, he definitely enjoys the Color Jar, and that's the biggest payoff for me :-)

1 comment:

Note: Only a member of this blog may post a comment.