Project in progress
Arduino Based Binary Clock

Arduino Based Binary Clock © CC BY

My very own binary clock based on Arduino and an 8x8 LED Matrix backpack.

  • 3,054 views
  • 2 comments
  • 15 respects

Components and supplies

About this project

Project

A while ago I acquired an 8x8 LED matrix from Adafruit. I was wondering what to make with it, and I created a nice mini Binary Clock.

The project was based on a couple of other clocks available on the internet. However, a few implemented second counting, which I was really looking for.

I implemented code to display the seconds in the same manner as the minutes. Using a screen draw routine from an Arduino Snake game, I had to fidget around with the coordinate and modulus calculations to get the pixels to show up exactly right.

Steps

First, I had to make sure how to wire things up and make sure the backpack worked. I used the standard Adafruit LED library and demo sketch to check everything.

Then I set about coding a binary clock. It used some code available on the internet (even Hackster.io) to create the sketch. I implemented second counting and a drawing routine that fills two coordinate arrays, so the whole screen gets rewritten every second.

To Do:

I bought a RTC breakout board recently and I am going to see whether I can implement that later on. Now I am seeing a bit of drift in the clock.

It will most probably mean rewriting some of the code because counting does not need to be handled by the Arduino, but I will have to interpret the RTC data to separate integers to use the pixel setting code.

Code

Arduino Sketch for Binary ClockC/C++
Use this Sketch to upload to your Arduino. Connect an Adafruit LED matrix backpack to your Arduino and add two buttons (don't forget the resistors). Use the buttons to adjust the time.
/*************************************************** 
  Binary clock created by Solino C. de Baay
  Based on libraries Time (ArduinoClock Sketch by
  Techno (sǝɹoɟ ǝǝןuuɐ) 
  And AdaFruit 8x8 LED Backpack library.
  Implemented clock setting and two buttons.
 ****************************************************/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"

int sunit = 0;
int munit = 0;
int hunit = 0;
int hour=0; 
int minute = 0; 
int second = 0; 
int TIME; 
const byte P1=2;
const byte P2=3;
const int pixelsToDraw = 20;
int pixelX[pixelsToDraw];
int pixelY[pixelsToDraw];
uint16_t pixelCMD[pixelsToDraw];

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

void setup() {
  Serial.begin(9600);
  Serial.println("Arduino Clock");
  
  matrix.begin(0x70);  // pass in the address

  pinMode(P1, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(P1), changehours, FALLING);
 
  pinMode(P2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(P2), changeminutes, FALLING);
}  

void loop() {
  // set up a local variable to hold the last time we moved forward one second
  // (static variables are initialized once and keep their values between function calls)
  // move forward one second every 1000 milliseconds
  static unsigned long lastTick = 0; 
  
  if (millis() - lastTick >= 1000) {
    lastTick = millis();
    second++; 
  }
  
  // move forward one minute every 60 seconds
  if (second >= 60) {
    minute++;
    second = 0; // reset seconds to zero
  }
  
  // move forward one hour every 60 minutes
  if (minute >=60) {
    hour++;
    minute = 0; // reset minutes to zero
  }
  
  if (hour >=24) {
    hour=0;
    minute = 0; // reset minutes to zero
  }
  
  //sets the variable sunit, munit and hunit for the unit digits using a       //modulus function
  sunit = second%10;
  munit = minute%10; 
  hunit = hour%10;
  //begin selecting pixels and draw them on the LED Matrix
  selectPixels(sunit,second,munit,minute,hunit,hour);
  matrix.clear();
  drawTime();
  matrix.writeDisplay(); 
  //For debuging:
  //Serial.println(displayTimeInString());
  //Serial.println(String(sunit));
}
  
void selectPixels(int sunit, int second, int munit, int minute, int hunit, int hour){  
  //seconds units
  if(sunit == 1 || sunit == 3 || sunit == 5 || sunit == 7 || sunit == 9) {
  addTimePixel(1,3,7,LED_ON);
  addTimePixel(2,3,5,LED_ON); } else { addTimePixel(1,3,7,LED_OFF);
  addTimePixel(2,3,5,LED_OFF);}//8LED (7,5)  
  if(sunit == 2 || sunit == 3 || sunit == 6 || sunit == 7) {
  addTimePixel(1,2,7,LED_ON);
  addTimePixel(2,2,4,LED_ON); } else { addTimePixel(1,2,7,LED_OFF);
  addTimePixel(2,2,4,LED_OFF);}//4LED (7,4)  
  if(sunit == 4 || sunit == 5 || sunit == 6 || sunit == 7) {
  addTimePixel(1,1,7,LED_ON);
  addTimePixel(2,1,3,LED_ON); } else { addTimePixel(1,1,7,LED_OFF);
  addTimePixel(2,1,3,LED_OFF);}//2LED (7,3)  
  if(sunit == 8 || sunit == 9) {
  addTimePixel(1,0,7,LED_ON);
  addTimePixel(2,0,2,LED_ON); } else { addTimePixel(1,0,7,LED_OFF);
  addTimePixel(2,0,2,LED_OFF); }//1LED (7,2)    

  //seconds 
  if((second >= 10 && second < 20) || (second >= 30 && second < 40) || (second >= 50 && second < 60))  {
  addTimePixel(1,6,6,LED_ON);
  addTimePixel(2,6,5,LED_ON); } else { addTimePixel(1,6,6,LED_OFF);
  addTimePixel(2,6,5,LED_OFF);}//4LED (6,5)
  if(second >= 20 && second < 40)  {
  addTimePixel(1,5,6,LED_ON);
  addTimePixel(2,5,4,LED_ON); } else { addTimePixel(1,5,6,LED_OFF);
  addTimePixel(2,5,4,LED_OFF);}//2LED (6,4)
  if(second >= 40 && second < 60) {
  addTimePixel(1,4,6,LED_ON);
  addTimePixel(2,4,3,LED_ON);  } else { addTimePixel(1,4,6,LED_OFF);
  addTimePixel(2,4,3,LED_OFF);}//1LED (6,3)

  //minutes units
  if(munit == 1 || munit == 3 || munit == 5 || munit == 7 || munit == 9) {
  addTimePixel(1,10,4,LED_ON);
  addTimePixel(2,10,5,LED_ON); } else { addTimePixel(1,10,4,LED_OFF);
  addTimePixel(2,10,5,LED_OFF);}//8LED (4,5)
  if(munit == 2 || munit == 3 || munit == 6 || munit == 7) {
  addTimePixel(1,9,4,LED_ON);
  addTimePixel(2,9,4,LED_ON);  } else { addTimePixel(1,9,4,LED_OFF);
  addTimePixel(2,9,4,LED_OFF);}//4LED (4,4)
  if(munit == 4 || munit == 5 || munit == 6 || munit == 7) {
  addTimePixel(1,8,4,LED_ON);
  addTimePixel(2,8,3,LED_ON);  } else { addTimePixel(1,8,4,LED_OFF);
  addTimePixel(2,8,3,LED_OFF);}//2LED (4,3)
  if(munit == 8 || munit == 9) {
  addTimePixel(1,7,4,LED_ON);
  addTimePixel(2,7,2,LED_ON);  } else { addTimePixel(1,7,4,LED_OFF);
  addTimePixel(2,7,2,LED_OFF);}//1LED (4,2)

  //minutes 
  if((minute >= 10 && minute < 20) || (minute >= 30 && minute < 40) || (minute >= 50 && minute < 60))  {
  addTimePixel(1,13,3,LED_ON);
  addTimePixel(2,13,5,LED_ON); } else { addTimePixel(1,13,3,LED_OFF);
  addTimePixel(2,13,5,LED_OFF);}//4LED (3,5)
  if(minute >= 20 && minute < 40)  {
  addTimePixel(1,12,3,LED_ON);
  addTimePixel(2,12,4,LED_ON); } else { addTimePixel(1,12,3,LED_OFF);
  addTimePixel(2,12,4,LED_OFF);}//2LED (3,4)
  if(minute >= 40 && minute < 60) {
  addTimePixel(1,11,3,LED_ON);
  addTimePixel(2,11,3,LED_ON); } else { addTimePixel(1,11,3,LED_OFF);
  addTimePixel(2,11,3,LED_OFF);}//1LED (3,3)

  //hour units
  if(hunit == 1 || hunit == 3 || hunit == 5 || hunit == 7 || hunit == 9) {
  addTimePixel(1,17,1,LED_ON);
  addTimePixel(2,17,5,LED_ON); } else { addTimePixel(1,17,1,LED_OFF);
  addTimePixel(2,17,5,LED_OFF);}//8LED (1,5)
  if(hunit == 2 || hunit == 3 || hunit == 6 || hunit == 7) {
  addTimePixel(1,16,1,LED_ON);
  addTimePixel(2,16,4,LED_ON); } else { addTimePixel(1,16,1,LED_OFF);
  addTimePixel(2,16,4,LED_OFF);}//4LED (1,4)
  if(hunit == 4 || hunit == 5 || hunit == 6 || hunit == 7) {
  addTimePixel(1,15,1,LED_ON);
  addTimePixel(2,15,3,LED_ON); } else { addTimePixel(1,15,1,LED_OFF);
  addTimePixel(2,15,3,LED_OFF);}//2LED (1,3)
  if(hunit == 8 || hunit == 9) {
  addTimePixel(1,14,1,LED_ON);
  addTimePixel(2,14,2,LED_ON); } else { addTimePixel(1,14,1,LED_OFF);
  addTimePixel(2,14,2,LED_OFF);}//1LED (1,2)

  //hour
  if(hour >= 10 && hour < 20)  {
  addTimePixel(1,19,0,LED_ON);
  addTimePixel(2,19,5,LED_ON); } else { addTimePixel(1,19,0,LED_OFF);
  addTimePixel(2,19,5,LED_OFF);}//2LED (0,5)
  if(hour >= 20 && hour < 24)  {
  addTimePixel(1,18,0,LED_ON);
  addTimePixel(2,18,4,LED_ON); } else { addTimePixel(1,18,0,LED_OFF);
  addTimePixel(2,18,4,LED_OFF);}//1LED (0,4)
}

//Fill the matrix memory with the next set of coordinates for the LED's to be
//switched on
void drawTime(){
  for(int i=0; i<pixelsToDraw; i++){
    matrix.drawPixel(pixelX[i], pixelY[i], pixelCMD[i]);
    }
}

//fill the coordinate arrays pixelX and pixelY with the correct values and 
//set the command to either switch the LED on or off
void addTimePixel(int pixel, int pos, int val, uint16_t cmd){  
  pixelCMD[pos] = cmd;
  switch(pixel){
    case 1:
    pixelX[pos] = val;
    case 2:
    pixelY[pos] = val;
  }
  
}

//Debugging function, use this to write the current time to the Serial output
String displayTimeInString(){
  String TimeString = String(hour);
  TimeString += ":";
  TimeString += String(minute);
  TimeString += ":";
  TimeString += String(second);
  return TimeString;
  }

//interrupt function to change the hours
void changehours() {
  hour = hour + 1;
  if(hour==24)hour=0;  
  Serial.println("Hours adjusted");
  Serial.println(displayTimeInString());
}

//interrupt function to change the minutes
void changeminutes() {
  second = 0;
  minute = minute + 1;
  Serial.println("Minutes adjusted");
  Serial.println(displayTimeInString());  
  }

//LED matrix coordinate reference ;o)  
//    01234567
//  0 00000000
//  1 00000000
//  2 01001001
//  3 01011011
//  4 11011011
//  5 11011011
//  6 00000000
//  7 00000000
  

Schematics

Arduino Breadboard for Binary Clock
Connections for the Arduino Binary Clock
Arduinoclock1 0 bb f0rifs2jh6

Comments

Similar projects you might like

Binary Clock with Arduino and Plywood

Project tutorial by Kalle Wallin

  • 2,869 views
  • 1 comment
  • 9 respects

Amazing Binary Clock using LED Matrix Module

Project tutorial by LAGSILVA

  • 9,351 views
  • 10 comments
  • 50 respects

eDOT - Arduino Based Precision Clock and Weather Station

Project tutorial by antiElectron

  • 9,119 views
  • 2 comments
  • 24 respects

Digital Clock

Project in progress by Team SharKode

  • 5,038 views
  • 2 comments
  • 21 respects

Simple Arduino Digital Clock Without RTC

Project in progress by Annlee Fores

  • 96,159 views
  • 23 comments
  • 80 respects

Clock Set Date Time

Project tutorial by Tittiamo

  • 34,198 views
  • 26 comments
  • 43 respects
Add projectSign up / Login