Building an Arduino Binary Clock
- 4 minutes read - 845 wordsWith all of my software projects I’ve been working on lately, I decided about a month ago to get into some hardware hacking as well. Arduino seemed like the obvious choice due to its fast growing popularity, great support, and the fantastic starter kit. I ordered up the starter kit and over the past month, worked through the 15 sample projects to learn the ins and outs. After completing these, I decided a binary clock would be a cool first self led project.
For those not familiar, a binary clock tells time in binary, through lights rather than digital display. A row of lights represents each digit in the time, so for 10:55, the clock would represent (from left to right) 1, 0, 5, 5. In binary (from top to bottom), the lights would therefore show 01, 0000, 0101, 0101. Once you get used to reading it, it’s as easy as a regular clock to use.
I had wanted to represent seconds as well, but ran out of output pins on the arduino, as well as space on the breadboard. So minutes would have to be as precise as it could get. Since the arduino doesn’t know the current time, I also added to switches, one for hours and one for seconds, to set the time.
Wiring up the board
I won’t go through every connection because there are just too many. Essentially, I used output pins 1-13 for each led. Red is minutes and green hours. Starting with pin 1, each increasing pin goes to the next order led. So in order, minutes 1, minutes 2, minutes 4, minutes 8, minutes 10, minutes 20, minutes 40, hours 1, hours 2, hours 4, hours 8, hours 10, hours 20.
All of the leds are connected to ground through a resistor. I essentially connected those I could easily reach to the resistor directly, and those that were harder to the grounds of other leds. This means that they are in parallel, which means if many are lit, they dim a bit, but it also meant I didn’t need 13 resistors. The hardest part about the whole project was wedging my fingers in to connect all the wires, 13 more would have been impossible.
Next, I added the two switches. Both connect to 5v and out to the analog input pins A0 (minutes) and A1 (hours). Because of the way I implemented the timings, you essentially have to hold down the switches until the time refreshes after a second, but this is easily fixed. It worked fine for me though as it makes setting the time easier, you can just hold down the buttons until the time is correct.
I also had a problem with pin 1 which is the tx (transmission) pin. Every second or so it would write to it, flashing the minute led. I didn’t figure out how to get around this. Perhaps disabling the serial writes would do it. I needed these for debugging though.
Software
Source code is available on my github: arduino-binary-clock/binaryclock/binaryclock.ino
https://gist.github.com/Tylopoda/652191f152173684f3a7.js
What we’re doing in the code is fairly simple. In setup, we are setting pins 1-13 as output and turning on the serial bridge for debugging.
In the loop, we check if a second has elapsed since the last update. If so, we add a second to the time. We then read the input from A0 and A1 to see if either switch is being pressed. If so, we add a minute or hour depending on the switch being pressed.
Next we roll over seconds, minutes, and hours if necessary. I.e. 60 seconds should be 0 seconds and +1 minute.
Then the fun part, if your definition of fun is mapping digits to leds. There’s a better way of calculating this based on powers of two, division and modulo arithmetic, but I figured a more readable version was better. For the hours and minutes, we take the value, divide by 10 to get the tens value, then take the remainder or modulo to get the 1s position value. Then, for each, we determine the leds to turn on (set power to high) based on the values for each. So for a value of 7, we don’t turn on the 8 because it is less, turn on the 4 because it is more, subtract the 4, giving us 3, illuminate the 2 led because 3 is greater than 2, subtract the 2, giving us 1, illuminate the 1 led because 1 is greater than or equal to 1, and get down to 0. It’s like a special kind of binary magic.
Last, we print the values of each to serial for debugging. This also makes setting the time much easier. Here’s the example output.
Seconds: 16
Minutes: 39
Hours: 23
Hook it up, start it up, and set the time. It’s not the most accurate clock in the world, but mine’s been running for two days and is still on the right time. Pretty fun. Now you just need to frame your creation or stick it on the refrigerator.