Raspberry Pi Binary Clock 
Sunday, July 14, 2013, 12:08
I have been fascinated by number systems for most of my life. I especially like binary since it is so incredibly fundamental to the deep underpinnings of our technological society yet incredibly simplistic. I take a special delight whenever technology is combined with art and what better way to demonstrate this concept than through a binary clock.

I had been formulating this project in my head for months and gradually assembled the materials required. Amazon had a great deal for buying 100 white LED's + resistors so I made sure to snatch those up. I also accumulated a couple solderless breadboards and variety of m/m m/f wires to make everything easier.

To drive the logic behind this project I decided on going the easiest route possible by using a Raspberry Pi since their GPIO handling Python libraries are so pleasant to work with. I also decided on not doing any sort of multiplexing to drive the LED's and just addressing them all individually through separate GPIO pins. At first I was going to include seconds on the clock but decided against it due to both the additional wires that would be required and to the aesthetic appeal of not having lights constantly blinking on the finished clock.

I also decided against my original idea of using ping pong balls to be light diffusers for the LED's. While this technique does work fairly well the light distribution proved not to be as uniform as I would have liked. The LEDs I have tend to do a sort of short distance projection effect which leaves a circle of light on whatever is in front of them. Without the ping pong balls the final design was also able to be quite a bit smaller and more compact.

After getting some time off of work and a few days of getting bored of not tinkering with any code I decided to get around to building out the binary clock. The first thing I did was take some black construction board and drill holes for all of the LEDs. I placed this board inside of a picture frame and then attached all the appropriate wires. An unexpected bonus was that the picture frame backing was able to be remounted on-top of the wires which served to prevent them from dislodging. One of the lights had to be replaced as it was DOA.

Image Removed

After this I started assembling all of the ingredients into the breadboard and connecting all of the wires into free GPIO pins on the Raspberry Pi. This proved to be the most tedious part of the project. Since I used a spare IDE cable to access the GPIO pins everything is in reverse on the other end of the cable and about halfway down due to the extra pins that an IDE cable has. I tested these out one by one and recorded what light went to what GPIO pin on a notebook. During this initial prototyping phase I duct taped everything down to a dresser as seen below.

Image Removed

Finally everything was in place and it was time to get to programming. I had expected this to take most of the time of the project as I have not worked with Python very much in the past but it ended up taking the least time of all. I sat down to start it on a Saturday night around 21:00 and ended up finishing it off well before 22:00. Most of the time spent was in learning more about Python conditionals and not building the clock itself. Every time I write anything in Python it amazes me how few lines are necessary and how easy it is to learn the small amount of overhead needed to write the code.

Admittedly things were made much easier for me since I was able to make use of the script I had written in the past for my last bash binary clock project. I removed much of the code and limited it to producing just a string of the current hours and minutes in binary.

Image Removed

After this it was just a matter of initialising all the GPIO ports to be in their OFF state as a number of them are turned on by default when the Raspberry Pi boots up. An array of all the lights in order is used to address all the lights as they correspond to the output of the bash script. The lights are set on and off accordingly and this process is repeated every three seconds. Three seconds seems like a good trade off on accuracy vs system load as I don't like the idea of this having to run every second since I still intend to use it as the primary Raspbmc media player in the bedroom.

The code for the clock came out much shorter than anticipated.

Image Removed

I also made another small script to turn all the lights off. It is quite similar to the one above except it basically just goes through up past the initialization step and stops.

Too make things really fancy I decided I wanted to be able to control the operation of the light with an Android smartphone. A free app called LazierGeek made this pretty easy to accomplish. I've set up one shortcut to run the remote ssh command:

sudo nohup python clock.py &

Which starts a background process of the clock without the need for an active session. To stop the clock and turn off all the lights I run another appropriately lazy command:

sudo pkill python; sudo python alloff.py

Which kills Python ( Effectively stopping the clock ) and then runs the initialization script on all the lights to get them in their off state.

At this point the clock was still duct taped down to my dresser. After a few days of taking extra care not to disturb any of the wires I decided that it was time to put it in a box for extra mobility and durability. A spare empty shipping package was selected for this purpose and I began to cover the entire thing with black duct tape. I also cut a hole in one side of the box to fit the picture frame screen as well as a smaller hole in the back for all the wires to come out. After a great deal of coaxing I got everything off of the dresser and smushed into the box where I taped everything down.

Image Removed

The box was then sealed with more tape.

Image Removed

At this point I'm debating leaving it like this, or putting this box within a spare computer case to make it look even sharper. In some ways I like the duct tape's charm and how it serves to draw attention away from some of the lights being positioned a bit wonkily.

Thoughts and comments are welcome.


Tuesday, September 10, 2013, 09:43
Neat project!
In the future if you need to make the LEDs diffuse light in a broader area you can simply take sandpaper to the top and side of the bulb, just enough to blur the light coming out.
Monday, July 15, 2013, 07:43
Quite a clock! Nice complete job writing it up, and I like the pictures too.

Administrator (Spike Snell) 
Sunday, July 14, 2013, 20:19
I modified my old temperature grabbing script to output in binary. Since I'm using Celsius there should be plenty enough range on just the bottom row of lights to display the right temperature no matter what it is like outside ( I don't expect it to be more than positive or negative 63 C! ). To distinguish from positive and negative temperatures I have the top row all off if the temperature is positive, and all on if it is negative.

The index counters are arranged in such a way that the time and temperature alternate every ten seconds and display for a respective ten seconds each. This allows for enough time to do the mental conversion/reading of the display. Another counter acts as a delay for grabbing the temperature from wunderground. When the clock is started it grabs the temperature, but then only refreshes the temperature every 10 minutes. This is done so that we aren't bombarding the nice folks at wundeground with temperature requests every few seconds.

The code for the temp clock: http://pastie.org/8141237

And the additional script to grab the temperature: http://pastie.org/8141238

This code could be simplified quite a bit by using some functions for the temperature display settings and I may do that in the future.

Add Comment
Fill out the form below to add your own comments.

Insert Special:

Moderation is turned on for this blog. Your comment will require the administrators approval before it will be visible.