Project tutorial

Digital Clock with Arduino, RTC and Shift Register 74HC595 © CC BY-NC-ND

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

  • 29,634 views
  • 23 comments
  • 67 respects

Components and supplies

Apps and online services

About this project

1- Introduction

This is my project of a digital clock with RTC (Real Time Clock) using a LED display of 4 digits and 7 segments including interesting features of temperature and humidity.

In the control of display I have used an Arduino Uno R3 and 02 ICs of 74HC595 (8 bit shift register with output latches).

The use of shift register is important to save output ports of Arduino and with it is necessary only 3 outputs of Arduino to control the display.

There are two breakouts: one for the sensor of temperature (ºC - Celsius / ºF - Fahrenheit degrees) and humidity (in %) and another one for the RTC (Real Time Clock).

The assembly is very simple, but is necessary to pay attention with the jumpers connections.

Video

2- Material List

  • 1 x DS1307 (Real Time Clock)
  • 1 x DHT11 (Temperature and Humidity sensor)
  • 2 x 74HC595 (8 bit shift register with output latches - 3 state)
  • 8 x Resistors of 150 Ohms (Brown, Green, Brown)
  • 1 x FYQ-5642BX (Common Anode LED Display 04 digits and 07 segments or equivalent but take care with the pins numbers)
  • 1 x Arduino Uno
  • 1 x Breadboard
  • 1 x Kit of jumpers

3- Assembly

The assembly of components is very simple but you need to follow the schematics with care due to the wire connections. There are 2 shift registers in this project: one to control each segment of the display and another one to control what is the display on.

Important: If you want to use another kind of LED display, you need to check its datasheet in order to update the output wiring (jumpers) of 74HC595 and also you must update the corresponding logics of Arduino's software.

4- Coding

The following binary data represent the relation between the digits (0 to 9) and the segments (A to G) of LED display:

  • B01111110 - 0
  • B00110000 - 1
  • B01101101 - 2
  • B01111001 - 3
  • B00110011 - 4
  • B01011011 - 5
  • B01011111 - 6
  • B01110000 - 7
  • B01111111 - 8
  • B01111011 - 9

To show in the LED display all the data in sequence for the time, temperature and humidity, I have used a "timer" with the functions millis() and while(). In this case, each information is presented on the display in sequence after 03 seconds of each one. Simple and very efficient.

You can apply this function millis() in several different ways to manage the time during the program running. In many situations you can use it in the place of some timer library.

Another usefull function is digitalWrite() that can simplify the writting of data into the shift registers.

Take a look in the Arduino's home page.

5- First adjustment of RTC module

1. To adjust the time of RTC module, load and run the program "SetTime " (you can find it on the library/example of DS1307RTC on Arduino IDE).

2. Reload and run again the program of "Digital Clock".

Following this procedure, the RTC module will keep the right time due its battery pack attached and you do not need to recompile the "Digital Clock" program every time you use it.

Code

Digital_Clock_V1_3_English.inoArduino
Code for Arduino UNO
/*
  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

}

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

Comments

Similar projects you might like

Complete Digital Clock Including Alarm and Motion Sensor

Project tutorial by LAGSILVA

  • 12,678 views
  • 11 comments
  • 50 respects

Tri-Mode Digital Clock With ATtiny85 And RTC

Project showcase by LAGSILVA

  • 5,207 views
  • 0 comments
  • 19 respects

Digital And Binary Clock With Two LED Matrix And RTC

Project tutorial by LAGSILVA

  • 6,117 views
  • 10 comments
  • 27 respects

Amazing Binary Clock using LED Matrix Module

Project tutorial by LAGSILVA

  • 9,352 views
  • 10 comments
  • 50 respects

4-Stroke Digital Clock With Arduino

Project showcase by LAGSILVA

  • 11,103 views
  • 9 comments
  • 39 respects

Digital Clock with Mirrored Display Driven by Accelerometers

Project showcase by LAGSILVA

  • 14,365 views
  • 1 comment
  • 38 respects
Add projectSign up / Login