Binary Clock Arduino Tutorial

Binary clock is a relatively popular gadget among programmers in many stores you can buy a version of alarm clocks or to wear wrist. However, nothing gives more satisfaction than create such a watch yourself. This post will show you how to build your own binary clock using Ardunio and several electronic components.

 
Materials:
1 x Atmega 328P-PU
20 x led best in 3 different colors
20 x 200 ohm resistor
3 x 4,7k 200 ohm resistor
3x integrated circuit 74HC595
1x RTC DS1307
3x Button
1x switch on-off
2x 22nf capacitator
1x 16 MHz crystal
1x 5V power source
 
Below you can see a diagram of the entire structure:

binary clock

binary clock

The diagram shows an extended version of a binary watch with the ability to change the time using the buttons and clock moduke that saves our time. But let’s start from the beginning. Most of the operations are performed by the module atmega 328P-PU. Part of it is really Breduino about which you can read here httpss://www.arduino.cc/en/Main/Standalone
 
Nevertheless, so that the module is able to work we have to connect the power supply with our atmega module. 5 V + connect to pins 7,20,21. Minus to pins 8 and 22. The whole is connected to the on-off switch. Under the pins 9,10 connect 16 MHz crystal and the capacitors which are finally grounded. Unfortunately atmega module has a limited number of outputs and we have to handle 20 LEDs, buttons and clock module. Therefore, we must use the 74HC595 IC. More about them you can read at this address: httpss://www.arduino.cc/en/Tutorial/ShiftOut Thanks to 74HC595 IC, we can share our signal to 8 streams using only 3 inputs of atmega which is particularly useful for our LEDs.
 
In fact, we already have enough to build a binary clock. Unfortunately, hour in such toy could only be set in the atmega program and it would be always reseted when power would be turned off. Also precision would get smaller with time. For many people, this may be sufficient but we can hook up to it set of buttons which will set our houre and a RTC DS1307 clock module which will be more accurate and as well will remember time, even when the device is not connected to power source.
 
The buttons are substantially straight they are ordinary Buttonswhich when pressed send a pulse to atmega. When it receives it Software adds one unit to the selected hour, minutes or seconds. Each input is protected by 4,7k ohm resistor. RTC 1307DS Clock has its own battery through even without power will be able to continue monitor the passage of time. His connection is as follows. 5V power supply and the ground has to be connected to a properly signed pins. SDA and SCL pins connected to the atmegi according to the scheme. SCL is the clock line. SDA and the data line.
 
Prepared above project is ready for etching on double sided board with dimensions of 120×160 mm. The red lines indicate the upper layer the blue bottom.
 
In this project we will need a RTClib library responsible for the operation of the DS 1307 clock. It can be downloaded from httpss://github.com/adafruit/RTClib
 
Below the source code for this project:

#include <Wire.h>
#include "RTClib.h"

volatile RTC_DS1307 RTC;

int SH_CP1 = 13; //clock pin
int ST_CP1 = 12; // latch pin
int Data1 = 11; // data pin
int SH_CP2 = 10; //clock pin
int ST_CP2 = 9; // latch pin
int Data2 = 8; // data pin
int SH_CP3 = 5; //clock pin
int ST_CP3 = 6; // latch pin
int Data3 = 7; // data pin

//8=18 latchpin,12=19 clockpin,11=17 datapin

int analog1 = 4;
int analog2 = 3;
int analog3 = 2;
volatile boolean on = false;

void setup() {
   Serial.begin(57600);
   Wire.begin();
   RTC.begin();

   if (! RTC.isrunning()) {
   // following line sets the RTC to the date & time this sketch was compiled
      RTC.adjust(DateTime(__DATE__, __TIME__));
   }

   pinMode(SH_CP1, OUTPUT);
   pinMode(ST_CP1, OUTPUT);
   pinMode(Data1, OUTPUT);
   pinMode(analog1, INPUT);
   pinMode(SH_CP2, OUTPUT);
   pinMode(ST_CP2, OUTPUT);
   pinMode(Data2, OUTPUT);
   pinMode(analog2, INPUT);
   pinMode(SH_CP3, OUTPUT);
   pinMode(ST_CP3, OUTPUT);
   pinMode(Data3, OUTPUT);
   pinMode(analog3, INPUT);

}

void loop() {

   DateTime now = RTC.now();

   if (digitalRead(analog3)==HIGH){//if button pressed
      plusTime(1,0,0);
   }
   else if(digitalRead(analog2)==HIGH){
      plusTime(0,1,0);
   }
   else if(digitalRead(analog1)==HIGH){
      plusTime(0,0,1);
   }
   else{
      on=false;

      digitalWrite(ST_CP1, LOW);
      int addNumber1=6*(now.second()/10); //add 6(00010000) after every 10 seconds
      shiftOut(Data1, SH_CP1, LSBFIRST, now.second()+addNumber1);
      digitalWrite(ST_CP1, HIGH);

      digitalWrite(ST_CP2, LOW);
      int addNumber2=6*(now.hour()/10); //add 6(00010000) after every 10 seconds
      shiftOut(Data2, SH_CP2, LSBFIRST, now.hour()+addNumber2);
      digitalWrite(ST_CP2, HIGH);

      digitalWrite(ST_CP3, LOW);
      int addNumber3=6*(now.minute()/10); //add 6(00010000) after every 10 seconds
      shiftOut(Data3, SH_CP3, LSBFIRST, now.minute()+addNumber3);
      digitalWrite(ST_CP3, HIGH);
   }
}

void plusTime(int secunds, int minutes, int hours){

   DateTime now = RTC.now();
   if(on==false){
      DateTime newTime = DateTime(now.year(), now.month(), now.day(), now.hour()+hours, now.minute()+minutes, now.second()+secunds);
      RTC.adjust(newTime);
      on=true;
   }
}

 

The finished project is as follows:


 
For anyone who wants to in any way modify this solution in the references I leave eagle files and source code.
 
Interesting references:
Eagle schematics
Source code

Leave a Comment

WordPress Video Lightbox Plugin