Project tutorial

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

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

  • 22,288 views
  • 16 comments
  • 48 respects

Components and supplies

Apps and online services

About this project

Introduction

Hello, everybody!

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. You only need 3 outputs to control the display. With it you transfer serial data into 8 parallel outputs. There are two breakouts: one for the sensor of temperature (ºC - Celsius / ºF - Fahrenheit degrees) and humidity (% - in percentage) and another one for the RTC.

The assembly is very simple and you just need take care with the wiring. Be patient, follow the schematics and enjoy it.

Note: Code updated to revision V1.3 in Oct.2017

- Introduction of the colon (double dot) flicking in the hours.

- Introduction of brightness control in the code.

Video

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

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.

Programming

For the numbering (0 to 9), I have created a table with binary representation of each segment (A to G) as follows:

  • 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.

How to setup the time of internal clock of RTC module

1. To update or setup 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 - CodeArduino
Arduino Code
/* 
Project:    Digital Clock with LED Display - 4 Digits x 7 Segments
              (Display Time, Temperature and Relative Humidity)
Revision:   1.3
Date:       01.Oct.2017
Author:     LAGSILVA

 *** 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 remarks/comments into English
     - Added optional statements to show the Temperature in Fahrenheit Degrees (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(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

}

Schematics

RTC - DS1307 - Datasheet
Datasheet of DS1307
DHT11 - Datasheet
Datasheet of DHT11 (Temperature & Humidity Sensor)
74HC595 - Datasheet
Datasheet of shift register 74HC595
LED Display - Datasheet
Datasheet of LED Display (4 Digits x 7 Segments)

Comments

Similar projects you might like

Complete Digital Clock Including Alarm and Motion Sensor

Project tutorial by LAGSILVA

  • 13,845 views
  • 12 comments
  • 52 respects

Tri-Mode Digital Clock With ATtiny85 And RTC

Project showcase by LAGSILVA

  • 5,439 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,740 views
  • 12 comments
  • 55 respects

4-Stroke Digital Clock With Arduino

Project showcase by LAGSILVA

  • 11,721 views
  • 14 comments
  • 40 respects

Digital Clock with Mirrored Display Driven by Accelerometers

Project showcase by LAGSILVA

  • 14,778 views
  • 1 comment
  • 39 respects
Add projectSign up / Login