LAGSILVA
Published © CC BY-NC-ND

Digital Clock with Arduino, RTC and Shift Register 74HC595

Digital clock with Arduino using Shift Register (74HC595), real time clock, temperature and humidity sensor and 7 Segments display.

BeginnerShowcase (no instructions)2 hours64,870

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Real Time Clock (RTC)
Real Time Clock (RTC)
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Shift Register- Serial to Parallel
Texas Instruments Shift Register- Serial to Parallel
×2
Common Anode LED Display (4 Dig x 7 Seg)
×1
Resistor 150 Ohm
×8

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

LED Display Datasheet

Datasheet of LED Display (4 Digits x 7 Segments)

Schematics

Breadboard Schematic

Breadboard schematic for Digital Clock using shift registers

Code

Digital_Clock_V1_3_English.ino

Arduino
/*
  Project:     Digital Clock (Time - Temperature - Relative Humidity)
  Author:      LAGSILVA
  Hardware:    Arduino UNO/Display 4Dig x 7Seg (Common Anode)/RTC/DHT11/74HC595
  Revision:    1.3
  Date:        01.Oct.2017
  License:     CC BY-NC-ND 4.0
               (Attribution-NonCommercial-NoDerivatives 4.0 International)
  -----------------------------------------------------------------------------
  *** Notes of revision V1.3 ***
     - Introduction of the colon (double dot) flicking in the hours
     - Introduction of brightness control

  *** Notes of revision V1.2 ***
     - Updated DHT library to use Adafruit Unified Sensor
     - DHT_sensor_library

  *** Notes of revision V1.1 ***
     - Translated the code comments to English
     - Added as optional statements to show the Temperature in Fahrenheit (F)
  -----------------------------------------------------------------------------
*/

#include <Time.h>        //Time Library
#include <TimeLib.h>
#include <DS1307RTC.h>   //Real Time Clock Library
#include <Wire.h>        //Auxiliary Library for DS1307RTC (Real-Time Clock) - Pins to Arduino UNO: A4 (SDA), A5 (SCL)
#include <DHT.h>         //Temperature and Humidity Library

#define DHTPIN 11         //Sensor DHT11 conected to the pin 11 on Arduino

// Definition of what DHT sensor type you are using
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

int clockPin = 8; // Pin 8 of Arduino connected in the pin 11 of 74HC595 (Clock)
int latchPin = 9; // Pin 9 of Arduino connected in the pin 12 of 74HC595 (Latch)
int dataPin = 10; // Pin 10 of Arduino connected in the pin 14 of 74HC595 (Data)

int hora, minuto, temp, umid;
int unidadeHora, unidadeMinuto, dezenaHora, dezenaMinuto;
int unidadeTemp, dezenaTemp, unidadeUmid, dezenaUmid;
unsigned long ti;
int brightness; // Brightness of display (Min=1 / Max=20)
int k;

//Digits Matrix - 0 a 9
byte num[] = {
  B01111110, // Zero
  B00110000, // One
  B01101101, // Two
  B01111001, // Three
  B00110011, // Four
  B01011011, // Five
  B01011111, // Six
  B01110000, // Seven
  B01111111, // Eight
  B01111011, // Nine
};


void setup() {

  pinMode(latchPin, OUTPUT); // Define the 3 digital pins as output
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  dht.begin();

  setSyncProvider(RTC.get);   // Update the time with data of RTC (Real Time Clock)
  setSyncInterval(300);       // Set the number of seconds between re-sync

  //setTime(hours, minutes, seconds, days, months, years);
  //setTime(15, 05, 00, 13, 02, 2016);
  //RTC.set(now());           // Set the RTC time

  brightness = 15; // Set the brightness of display (Min=1 / Max=20)

}


void loop() {

  for (k = 1; k <= 8; k++) { // Repeat 8 times 0.5 s

    ti = millis(); // Initial time for the Timer of Hour/Time

    while ((millis() - ti) < 500) { //Timer of 0.5 second to show the Hour with the colon On

      delay(20 - constrain(brightness, 1, 20)); //Brightness control

      hora = hour();
      minuto = minute();
      unidadeHora = hora % 10;
      dezenaHora = hora / 10;
      unidadeMinuto = minuto % 10;
      dezenaMinuto = minuto / 10;

      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 8);                  //Set DISPLAY 1 (top view from left to right)
      shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaHora]);   //Set the Hour (ten)
      digitalWrite(latchPin, HIGH);

      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 4);                  //Set DISPLAY 2 with the colon On
      shiftOut(dataPin, clockPin, LSBFIRST, ~(num[unidadeHora] + 128 * k % 2)); //Set the Hour (unit)
      digitalWrite(latchPin, HIGH);

      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 2);                  //Set DISPLAY 3
      shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaMinuto]); //Set the Minute (ten)
      digitalWrite(latchPin, HIGH);

      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 1);                   //Set DISPLAY 4
      shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeMinuto]); //Set the Minute (unit)
      digitalWrite(latchPin, HIGH);

      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, 1);                  //Set DISPLAY 4
      shiftOut(dataPin, clockPin, LSBFIRST, 255);                //Reset the DISPLAY 4 (to avoid some flicking)
      digitalWrite(latchPin, HIGH);

    }

  }

  delay(500);  //Wait for half second before go ahead to show the next feature

  ti = millis(); //Initial time for the Timer of Temperature

  temp = dht.readTemperature(false);   //Reading the Temperature in Celsius degree (C)

  //Optional Temperature in Fahrenheit (F). Remove the comments ("//") of following statement before use it.
  //temp = dht.readTemperature(true); //Reading of Temperature in Fahrenheit degree (F)

  while ((millis() - ti) < 3000) { //Timer of 3 seconds for the Temperature

    delay(20 - constrain(brightness, 1, 20)); //Brightness control

    unidadeTemp = temp % 10;
    dezenaTemp = temp / 10;

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 8);                  //Set DISPLAY 1 (top view from left to right)
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaTemp]);   //Set the Temperature (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 4);                  //Set DISPLAY 2
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeTemp]);  //Set the Temperature (unit)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 2);                  //Set DISPLAY 3
    shiftOut(dataPin, clockPin, LSBFIRST, ~B01100011);         //Set the degree symbol []
    digitalWrite(latchPin, HIGH);

    //Show the symbol of Celsius degrees (C)
    //Set the following statements as comments with "//" to show the Temperature in Fahrenheit (F)
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                  //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, ~B01001110);         //Set the symbol of Celsius [C]
    digitalWrite(latchPin, HIGH);

    //Show the symbol of Fahrenheit degrees (F)
    //Remove the indication of comments "//" on following statements to show the Temperature in Fahrenheit (F)
    //digitalWrite(latchPin, LOW);
    //shiftOut(dataPin, clockPin, LSBFIRST, 1);                 //Set DISPLAY 4
    //shiftOut(dataPin, clockPin, LSBFIRST, ~B01000111);        //Set the symbol of Fahrenheit [F]
    //digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                   //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, 255);                 //Reset the DISPLAY 4 (to avoid some flicking)
    digitalWrite(latchPin, HIGH);
  }
  delay(500);  //Wait for half second before go ahead to show the next feature

  ti = millis(); //Initial time for the Timer of Humidity

  umid = dht.readHumidity();    //Reading the Humidity

  while ((millis() - ti) < 3000) { //Timer of 3 seconds for the Humidity

    delay(20 - constrain(brightness, 1, 20)); //Brightness control

    unidadeUmid = umid % 10;
    dezenaUmid = umid / 10;

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 8);                  //Set DISPLAY 1
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[dezenaUmid]);   //Set the Humidity (ten)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 4);                  //Set DISPLAY 2
    shiftOut(dataPin, clockPin, LSBFIRST, ~num[unidadeUmid]);  //Set the Humidity (unit)
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 2);                  //Set DISPLAY 3
    shiftOut(dataPin, clockPin, LSBFIRST, ~B01100011);         //Set the upper symbol of percentage [%] of Humidity
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                  //Set DISPLAY 4
    shiftOut(dataPin, clockPin, LSBFIRST, ~B00011101);         //Set the lower symbol of percentage [%] of Humidity
    digitalWrite(latchPin, HIGH);

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, 1);                  //Set Display 4
    shiftOut(dataPin, clockPin, LSBFIRST, 255);                //Reset the DISPLAY 4 (to avoid some flicking)
    digitalWrite(latchPin, HIGH);
  }
  delay(500);  //Wait for half second before to restart

}

Credits

LAGSILVA

LAGSILVA

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

Comments