Binary Clock with Arduino Micro and RTC DS1307

I’ve build a Binary Clock to learn a bit more about Arduino Programming and to make myself keep the binary system in check.
Instead of using input buttons for setting the time, I’m going to use a real time clock (RTC) – the DS1307. I’m not going to explain how to assemble it (the one I bought came in pieces to assemble) and how to set the time the first time. You can find out more with the excellent Adafruit tutorial by Tyler Cooper.
Needed Parts
- 17x LEDs
- 6 yellow (seconds)
- 5 (hours) + 6 (minutes)
- 17x 120 Ohm resistors
- 1x Arduino Micro
- 1x DS1307 RTC breakout
- 1x Adafruit PowerBoost 500 Basic (Optional)
- 1x Lipo Battery (Optional)
The following picture shows how the connections are going to be made. We’re going to use all the digital PINs and all of the analog PINs of the Arduino Micro.
DS1307 RTC
To connect the DS1307, we’re going to use the I2C protocol. More about I2C in the Sparkfun learning system.
DS1307 has 5 PINs, but we’re just going to use 4.
- GND and VCC are self explanatory.
- SCL is the I2C clock PIN
- SDA is the I2C data PIN
In the Arduino Micro, the SDA and SCL pins are PINs 2 and 3 (respectively), which we’re going to use.
More info about the Arduino Micro
You can learn more about the DS1307 RTC in the Adafruit website.
Assembly and wiring
I had laying around a acrylic cube from an early project – Build an LED ambient light with Arduino and a Light/Lux/Luminosity Sensor – and decided to used it for a binary clock.
First, using a small wooden box i create the holes for the LEDs. Although the hours will just take 5 LEDs, I’ve decided to drill six holes nonetheless.
The LEDs inserted in the holes
This next image is the other side of the box, with the LEDs and the resistors soldered to the cathode (the short leg)
The next image are the LEDs already soldered together by the resistors.
This next image shows (according to the code) how to wire which LED to which PIN in the Arduino Micro
Connecting the wires to the LEDs (anodes – Long leg) which will connect to the Arduino Micro
The connections all made
Another shot of the connections
The following image shows the clock running with the Lipo battery and the Adafruit Powerboost 500 Basic
The acrylic cube
Code
The code is based in the elegant and excellent solution provided by Simon Monk on his book “30 Arduino Projects for the Evil Genius“.
My only changes are just using the RTC library and using 24 hours, instead of just 12.
The RTC library, to use with the DS1307, is needed. You can get it from the Adafruit RTClib Github account.
Uncompress it and move it to your Arduino libraries folder.
Code explaining
The code is fair simple to understand:
Three arrays are created – hours, minutes and seconds – each containing the PIN numbers for each LED corresponding to each “part” of time you want.
A simple solution (Thank you Simon) to translate the digit (be it hour, second or minute) into binary is using the bitread function.
Using the bitread function, we get a binary representation of the decimal number we want.
Example: 23
Using bitread (with 6 digits), we get 111010, which is 23 in reverse – 010111, therefor the PINs used in the minutes and seconds arrays are reversed – least significant bits first
When wiring, you wire the LEDs in order, but in the code they’re reversed because of the way bitread returns the result.
//#include <Time.h> #include <Wire.h> #include "RTClib.h" //define RTC RTC_DS1307 rtc; int hourLEDs[] = {1, 4, 5, 6, 7}; //least significant bit first int minuteLEDs[] = {13, 12, 11, 10, 9, 8}; int secondLEDs[] = {A5, A4, A3, A2, A1, A0}; int loopLEDs[] = {A5, A4, A3, A2, A1, A0, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 1}; void setup() { for (int i = 0; i < 5; i++) pinMode(hourLEDs[i], OUTPUT); for (int i = 0; i < 6; i++) pinMode(minuteLEDs[i],OUTPUT); for (int i = 0; i < 6; i++) pinMode(secondLEDs[i],OUTPUT); //Serial.begin(9600); Wire.begin(); rtc.begin(); if (!rtc.isrunning()) Serial.println ("RTC not running!"); } void loop() { /* if (minute() == 0 && second() == 0) spin(hour()); */ updateDisplay(); delay(500); } void updateDisplay() { DateTime now = rtc.now(); /* Serial.print ('H: '); Serial.print (hour(t)); Serial.print (' M: '); Serial.println (minute(t)); */ // Serial.print ("H: "); // Serial.println (now.hour(),DEC); setOutput (hourLEDs, 5, now.hour()); // Serial.print ("M: "); // Serial.println (now.minute(),DEC); setOutput (minuteLEDs, 6, now.minute()); // Serial.print ("S: "); // Serial.println (now.second(),DEC); setOutput (secondLEDs, 6, now.second()); if (now.minute() == 0 && now.second() == 0) spin(now.hour()); } void setOutput(int *ledArray, int numLEDs, int value) { for (int i = 0; i < numLEDs; i++) { digitalWrite(ledArray[i], bitRead(value, i)); //Serial.print(bitRead(value,i)); } //Serial.println(); } void spin(int count) { for (int i = 0; i < count; i++) { for (int j = 0; j < 16; j++) { digitalWrite (loopLEDs[j], HIGH); delay(50); digitalWrite(loopLEDs[j], LOW); } } }
This next image is the Binary clock already running
Here’s some videos that show the Binary Clock working
Hope you had fun !
Happy programming !