LAGSILVA
Published © CC BY-NC-ND

Pi Day Clock with Arduino

This clock uses the first 192 digits of Pi number to display the time in a 32 x 8 LED matrix in an original way.

BeginnerShowcase (no instructions)30 minutes4,473
Pi Day Clock with Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Real Time Clock (RTC)
Real Time Clock (RTC)
×1
Maxim Integrated Module of 32 x 8 LED matrix with MAX7219
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Paper Template

Paper template (scale 1 : 1) to cover the LED matrix module.

Schematics

Schematic for Pi Clock

Schematic for Pi Clock

Code

Pi_Clock_V1_4.ino

Arduino
/*
   Project:   Pi-Clock (192 digits)
   Author:    LAGSILVA
   Hardware:  Arduino UNO-R3, MAX72XX LED MatriX, RTC DS1307
   Date:      24.Jan.2019
   Revision:  1.4
   License:   CC BY-NC-ND 4.0
              (Attribution-NonCommercial-NoDerivatives 4.0 International)
*/

#include <LedControl.h>                 // Library for LED Display - MAX72XX
#include <Wire.h>                       // Library for DS1307RTC - Pins of Arduino UNO: A4 (SDA), A5 (SCL)
#include <DS1307RTC.h>                  // Library for Real Time Clock
#include <Time.h>                       // Time library
#include <TimeLib.h>

#define DS1307_I2C_ADDRESS 0x68         // This is the I2C address (RTC)

// Global Variables
String pi = "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895";
String piH, piM , piS;
byte hh, mm, ss;
byte lastS = 0;
byte unitH, unitM, unitS, tenH, tenM, tenS;
byte row, col, pos;

/*
  Pin numbers of Arduino to be connected into MAX72XX (LED Matrix controlled by MAX72XX)
  pin 2 is connected to the DataIn (DIN)
  pin 3 is connected to LOAD (CS)
  pin 4 is connected to the CLK (CLK)
*/

LedControl lc = LedControl(2, 4, 3, 4); // LedControl(int dataPin, int clkPin, int csPin, int numDevices)


void setup() {

  setSyncProvider(RTC.get);             // Function to read RTC (Real Time Clock)
  setSyncInterval(29);                  // Set the number of seconds between re-sync
  //setTime(21, 41, 00, 26, 01, 2019);    // Setting Time and Date
  //RTC.set(now());                       // Setting RTC Time

  // Setup of Display "0"
  lc.shutdown(0, false);                // Wakeup Display "0"
  lc.setIntensity(0, 6);                // Set the Brightness of Display (0 to 15)
  lc.clearDisplay(0);                   // Clear Display "0" (Hour)

  // Setup of Display "1"
  lc.shutdown(1, false);                // Wakeup Display "1"
  lc.setIntensity(1, 6);                // Set the Brightness of Display (0 to 15)
  lc.clearDisplay(1);                   // Clear Display "1" (Minute)

  // Setup of Display "2"
  lc.shutdown(2, false);                // Wakeup Display "2"
  lc.setIntensity(2, 6);                // Set the Brightness of Display (0 to 15)
  lc.clearDisplay(2);                   // Clear Display "2" (Second)

  // Setup of Display "3"
  lc.shutdown(3, false);                // Wakeup Display "3"
  lc.setIntensity(3, 6);                // Set the Brightness of Display (0 to 15)
  lc.clearDisplay(3);                   // Clear Display "3" (Pi number: 64 digits)

  for (byte k = 0; k <= 7; k++) {
    piH = piH + pi.substring(k * 24, k * 24 + 8);
    piM = piM + pi.substring(k * 24 + 8, k * 24 + 16);
    piS = piS + pi.substring(k * 24 + 16, k * 24 + 24);
  }

}


void loop() {

  hh = hour();
  mm = minute();
  ss = second();

  if (mm == 0 && ss == 0 && ss != lastS) {
    lc.clearDisplay(0);
  }

  if (ss == 0 && ss != lastS) {
    lc.clearDisplay(1);
    lc.clearDisplay(3);
  }

  if (ss != lastS) {
    lc.clearDisplay(2);
    lastS = ss;
  }

  unitH = hh % 10;
  tenH = hh / 10;
  unitM = mm % 10;
  tenM = mm / 10;
  unitS = ss % 10;
  tenS = ss / 10;

  //Plot Hour
  pos = 0;
  if (tenH > 0) {
    col = 7 - piH.indexOf(String(tenH), 0) % 8;
    row = 7 - piH.indexOf(String(tenH), 0) / 8;
    pos = piH.indexOf(String(tenH), 0) + 1;
    lc.setLed(0, row, col, true);
  }

  col = 7 - piH.indexOf(String(unitH), pos) % 8;
  row = 7 - piH.indexOf(String(unitH), pos) / 8;
  lc.setLed(0, row, col, true);

  //Plot Minute
  pos = 0;
  if (tenM > 0) {
    col = 7 - piM.indexOf(String(tenM), 0) % 8;
    row = 7 - piM.indexOf(String(tenM), 0) / 8;
    pos = piM.indexOf(String(tenM), 0) + 1;
    lc.setLed(1, row, col, true);
  }

  col = 7 - piM.indexOf(String(unitM), pos) % 8;
  row = 7 - piM.indexOf(String(unitM), pos) / 8;
  lc.setLed(1, row, col, true);

  //Plot Second
  pos = 0;
  if (tenS > 0) {
    col = 7 - piS.indexOf(String(tenS), 0) % 8;
    row = 7 - piS.indexOf(String(tenS), 0) / 8;
    pos = piS.indexOf(String(tenS), 0) + 1;
    lc.setLed(2, row, col, true);
  }

  col = 7 - piS.indexOf(String(unitS), pos) % 8;
  row = 7 - piS.indexOf(String(unitS), pos) / 8;
  lc.setLed(2, row, col, true);

  lc.setLed(3, 7 - ss / 8, 7 - ss % 8, true);     //Shows Pi digits all second

}

Credits

LAGSILVA

LAGSILVA

7 projects • 338 followers
Mechanical Engineer in automotive industry since 1989. Coding and Arduino are my hobbies.

Comments