# Binary Clock

There are some hobby projects that happen by accident. Meaning, you have an idea and you start working on it, only to find out one of the following in the midway: (1) it’s going nowhere, your initial plan was flawed or you didn’t take account of certain important things, (2) it’s going somewhere, but it will take much more work than you planned, (3) it’s working (yay!). For (1) and (2), sometimes I end up doing something else (definitely not my original plan) using the work done so far. Sometimes these projects are also quite trivial, as there are constraints put up by the previous plan and existing equipment/code that you already built.

There are some varieties of binary hand watches in the market. Last night I was working on an idea that involves controlling a few servo motors based on some sequences. I hooked up some LEDs instead of the servos to see whether my algorithm is producing the correct sequence. Eventually I got tired trying to debug the algorithm and realized I am taking route (2). :p Then I thought why not leave the current project for the next weekend and do something else with this setup? The first easiest idea that came to mind was to make a binary clock with the LEDs!

Setup

Unfortunately I had only eight LEDs at my disposal (or, I could only find eight at that time), so I had to cut down the resolution of the clock. I allocated four LEDs for keeping count of hours and four for minutes. The decimal number 15 is the maximum, starting from 0, to fit into a 4-element binary vector. Hence, the minutes can be shown with a resolution of 4 min. (60/15 min.), i.e. the time shown will have a total uncertainty of 4 min.

My LEDs have higher power rating compared to normal LEDs, they are really bright once they light up. When working with them I put a paper cap on top just to make sure I can look at them! These caps are made by cutting pieces off from a paper folder, wrapping those around the neck of the LED and gluing the paper ends together.

Figure1 . (Right) Making caps for my LEDs, (Left) Connecting with Arduino.

The circuit is very basic. The LEDs are connected to the digital pins on my Arduino microcontroller through 220 ohms resistors. The LED ground pins are connected to the ground pin on the Arduino through a common ground channel on my breadboard.

Code

I decided to use Matlab for this since I was initially using Matlab to control the servos through the Arduino (using Matlab’s own ArduinoIO library). Here’s the small code that takes the current system time, converts the hours and minutes to binary representation, which is taken as a state vector, and sends signals to the arduino based on the current state of the state vector. All the LEDs representing 0’s are lit up and the 1’s are made to blink.

```% matlab + arduino binary clock

a=arduino('COM3')
pins = [2; 3; 4; 5; 6; 7; 9; 10];
states = zeros(size(pins,1),1);
bstates = zeros(size(pins,1),1);

for i = 1:length(pins)
a.pinMode(pins(i), 'output');
end

ct = 0;

while true
% current time
curt = clock;
if curt(4) &gt; 12
curt(4) = curt(4) - 12;
end
% convert to binary vector
hrs = de2bi(round(curt(4)),4);
mins = de2bi(round(curt(5)/4),4);
states = [hrs mins]';
% send signal to the pins, make the 1's blink
for i = 1:length(states)
if states(i) == 0
a.digitalWrite(pins(i),1);
else
if mod(ct,100) == 0
if bstates(i) == 1
a.digitalWrite(pins(i),0);
bstates(i) = 0;
elseif bstates(i) == 0
a.digitalWrite(pins(i),1);
bstates(i) = 1;
end
end
end
end
ct = ct + 1;
end
```

Pictures and Videos

The first set of LEDs are hours and the second set are minutes. Figure 2.  Binary clock setup at night. Quite handy as a night light + time keeper when you go to sleep? 😉

The following video shows the clock in action.

The advantage of sending signals from PC and not burning a native arduino program in the controller is live and correct time display. To use a native .ino arduino program, I would have to set the clock time manually using some push switches attached to the board. That’s quite easy to do, but again, I don’t have any of those in my parts collection! 🙂