Project showcase

Tri-Mode Digital Clock With ATtiny85 And RTC © CC BY-NC-SA

This is an uncommon digital clock that can run in three different modes: mode digital and two different modes of binary clock.

  • 4,733 views
  • 0 comments
  • 19 respects

Components and supplies

Apps and online services

About this project

- Introduction

This is my first project using ATtiny85 microcontroller and also including a Real Time Clock (RTC) working with it.

The use of ATtiny85 is a very interesting way to shrink your Arduino projects in a final tiny version.

The Three-Modes Digital Clock includes a version with common decimal clock and also with two versions of binary clock.

One version of binary clock is traditional where each digit of hours and minutes are converted into binary for a LED display of 7 segments.

The last one is my favorite of an unusual version of binary clock where the first digit represents the hour, the second, the minutes and the third the seconds.

An interesting additional feature is about the use of internal memory of RTC that you can save the status of the mode of clock.

This feature is less known and is not applied commonly but give you an extra bonus card for your projects.

Let's see the project!

Video

- Material List

You will need for this project:

  • ATtiny85 microcontroller
  • Display TM1637 (4 Digits x 7 Segments)
  • RTC module (DS1307)
  • Tactile button switch
  • Breadboard
  • Wires
  • Power supply (3V to 5V)

Note:

You can use AA batteries (02x or 03x), for example, or any power supply of 5 Volts at maximum.

- Schematics & Program

Programming ATtiny

Many programs of Arduino can be loaded to ATtiny, but in some situations you need an specific library to work on it.

In this project I needed to update the Wire library (support of I2C communications) to TinyWireM library.

With this library you can normally use the DS1307RTC.

- ATtiny85

Basic information about ATtiny

The ATtiny has 5 pins/ports to be used as digital, analogic or PWM as shown on diagrams.

The reset Pin#1 works as Reset and it can also be used as additional port, but you need to setup it with care using specific programs to manage the internal fuses and configuration of the microcontroller.

There are many information about ATtiny85 available on Internet and also tutorials.

With these information you will learn the steps to configure the Arduino to setup the first time the ATtiny.

At first time you will burn the fuses of ATtiny to set the clock (internal or external and the speed) and will also load the bootloader.

Take a look on these instructions:

Note: I did my own version of ATtiny programming shield for Arduino UNO as you can see in the pictures.

- Loading program into ATtiny

To load a program to ATTiny using Arduino as ISP you just need follow these steps:

  • Load the program ArduinoISP into Arduino.
  • Change the board on Arduino as ATTiny, set the processor as ATtiny85 and set the clock as 8 MHz (internal) according the first setup of microcontroller.
  • Load to Arduino the program that you want to transfer to ATtiny.
  • Compile and load the program to ATtiny

- Using

The clock is very simple to be used.

Each time you turn on the clock or you press the push button switch, the clock mode will be moved to the next status.

Tri-Modes of the clock:

  • Mode 1: Digital / Decimal digits

This is the traditional mode showing hours and minutes with direct reading.

  • Mode 2: Binary #1 (digits of hours and minutes are coded to binary)

In this mode you can see each decimal digit converted into its binary corresponding number as follows:

Segments x Decimal Values:

A = 0 / B = 1 / C = 2 / D = 4 / E = 8 / F = 16 / G = 32

Example:

Time 14:21 hs is:

  • 1 = B
  • 4 = D
  • 2 = C
  • 1 = B
  • Mode 3: Binary #2 (hours, minutes and seconds coded to binary)

In this mode the calculation is similar to mode 2 but here the hour is coded to binary, also the minutes and the seconds.

Note that in this case we can save two digits to represent hour and minutes.

With this I can include the seconds in the third digit of display and keeping one digit free of usage.

Example:

Time 14:21:29 hs is:

  • 14 hours = C + D + E
  • 21 minutes = B + D + F
  • 29 seconds = B + D + E + F

Code

Binary_Clock_with_ATTINY_RTC_and_7_Segments_Display_TM1637_V4_0.inoArduino
Attiny code
/*                         Tri-Mode Digital Clock
     Binary Clock with RTC & 7 Segments Display - TM1637 (4 Digits x 7 Segments)
                  by LAGSILVA - V4.0 - 24.Jun.2016
*/

#include <TM1637Display.h>              //Display TM1637 library
#include <DS1307RTC.h>                  //DS1307RTC (Real-Time Clock Library) - Pins to ATTINY85: #5 (SDA), #7 (SCK/SCL)
#include <TinyWireM.h>                  //Library of Wire to support DS1307RTC (Real-Time Clock) - Pins to ATTINY85: #5 (SDA), #7 (SCK/SCL)

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

// Module connection pins (Digital Pins)
#define CLK 3          //ATTINY85 conection on Pin #2  = CLK of Display Module
#define DIO 4          //ATTINY85 conection on Pin #3  = DIO of Display Module

//
//      A
//     ----
//  F |    | B
//     - G-
//  E |    | C
//     ----
//      D
//

//
//      0
//     ----
// 16 |    | 1
//     -32-
//  8 |    | 2
//     ----
//      4
//


uint8_t data[] = {      // Data with colon

  0b10111111, // 0
  0b10000110, // 1
  0b11011011, // 2
  0b11001111, // 3
  0b11100110, // 4
  0b11101101, // 5
  0b11111101, // 6
  0b10000111, // 7
  0b11111111, // 8
  0b11101111, // 9
  0b00000000, // 10

};

int hh, ht, hu, mm, mt, mu, ss, k1;
unsigned long ti;
byte clockMode;

TM1637Display display(CLK, DIO);


void setup() {

  display.setBrightness (0x0a);//(0x0f) is the max brightness;

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

  TinyWireM.begin();                    // initialize I2C lib

  // Read datum of Clock Mode (Decimal or Binary)stored in RTC
  TinyWireM.beginTransmission(DS1307_I2C_ADDRESS);               // Open I2C line in write mode
  TinyWireM.write((byte)0x08);                                   // Set the register pointer to (0x08)
  TinyWireM.endTransmission();                                   // End Write Transmission
  TinyWireM.requestFrom(DS1307_I2C_ADDRESS, 1);                  // In this case read only 1 byte
  clockMode = TinyWireM.read();                                  // Read the Mode of Clock (Decimal=0 / Binary Traditional =1 / Binary with Seconds = 2) stored at RTC memory

  clockMode = (clockMode + 1) % 3;

  // Write data of Clock Mode (Decimal or Binary) in RTC
  TinyWireM.beginTransmission(DS1307_I2C_ADDRESS);               // Open I2C line in write mode
  TinyWireM.write((byte)0x08);                                   // Set the register pointer to (0x08)
  TinyWireM.write(clockMode);                                    // Record at RTC memory the Mode of Clock
  TinyWireM.endTransmission();                                   // End Write Transmission


  //Clear all digits of display
  display.setSegments(data + 10, 1, 0);                          // Clear digit #1
  display.setSegments(data + 10, 1, 1);                          // Clear digit #2
  display.setSegments(data + 10, 1, 2);                          // Clear digit #3
  display.setSegments(data + 10, 1, 3);                          // Clear digit #4


  //setTime(14, 35, 0, 24, 6, 2016);          // Initial time & date setup of RTC
  //RTC.set(now());                           // Update the RTC with current time

}


void loop() {

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

  switch (clockMode) {

    case 0: //DECIMAL CLOCK

      display.setSegments(data + (hh / 10),  1, 0);              //Display 1 (Hours - Ten)
      display.setSegments(data + (hh % 10),  1, 1);              //Display 2 (Hours - Unit)
      display.showNumberDec(mm, true, 2, 2);                     //Display 3 & 4 (Hours)

      break;


    case 1:  //BINARY CLOCK (Traditional Mode - Without Seconds)

      ht = hh / 10;         //Houts - Tens
      hu = hh % 10;         //Hours - Units
      mt = mm / 10;         //Minutes - Tens
      mu = mm % 10;         //Minutes - Units

      // Plot Hours - Tens

      for (k1 = 0; k1 <= 7; k1++) {
        if (bitRead(ht, k1) == 1) {
          bitWrite(data[0], k1 + 1, 1);
        }
        else {
          bitWrite(data[0], k1 + 1, 0);
        }
      }
      if (ht == 0) {
        bitWrite(data[0], 0, 1);
      }
      else {
        bitWrite(data[0], 0, 0);
      }
      display.setSegments(data + 0, 1, 0);    //Display 1


      // Plot Hours - Units

      for (k1 = 0; k1 <= 7; k1++) {
        if (bitRead(hu, k1) == 1) {
          bitWrite(data[0], k1 + 1, 1);
        }
        else {
          bitWrite(data[0], k1 + 1, 0);
        }
      }
      if (hu == 0) {
        bitWrite(data[0], 0, 1);
      }
      else {
        bitWrite(data[0], 0, 0);
      }
      bitWrite(data[0], 7, 1);
      display.setSegments(data + 0, 1, 1);    //Display 2


      // Plot Minutes - Tens
      for (k1 = 0; k1 <= 7; k1++) {
        if (bitRead(mt, k1) == 1) {
          bitWrite(data[1], k1 + 1, 1);
        }
        else {
          bitWrite(data[1], k1 + 1, 0);
        }
      }
      if (mt == 0) {
        bitWrite(data[1], 0, 1);
      }
      else {
        bitWrite(data[1], 0, 0);
      }
      display.setSegments(data + 1 , 1, 2);   //Display 3


      // Plot Minutes - Units
      for (k1 = 0; k1 <= 7; k1++) {
        if (bitRead(mu, k1) == 1) {
          bitWrite(data[2], k1 + 1, 1);
        }
        else {
          bitWrite(data[2], k1 + 1, 0);
        }
      }
      if (mu == 0) {
        bitWrite(data[2], 0, 1);
      }
      else {
        bitWrite(data[2], 0, 0);
      }
      display.setSegments(data + 2 , 1, 3);   //Display 4

      break;


    case 2:  //BINARY CLOCK (With Seconds)

      // Plot Hours (hh)
      for (int k1 = 0; k1 <= 7; k1++) {
        if (bitRead(hh, k1) == 1) {
          bitWrite(data[0], k1 + 1, 1);
        }
        else {
          bitWrite(data[0], k1 + 1, 0);
        }
      }
      if (hh == 0) {
        bitWrite(data[0], 0, 1);
      }
      else {
        bitWrite(data[0], 0, 0);
      }
      display.setSegments(data + 0, 1, 1);    //Display 2


      // Plot Minutes (mm)
      for (int k1 = 0; k1 <= 7; k1++) {
        if (bitRead(mm, k1) == 1) {
          bitWrite(data[1], k1 + 1, 1);
        }
        else {
          bitWrite(data[1], k1 + 1, 0);
        }
      }
      if (mm == 0) {
        bitWrite(data[1], 0, 1);
      }
      else {
        bitWrite(data[1], 0, 0);
      }
      display.setSegments(data + 1 , 1, 2);   //Display 3


      // Plot Seconds (ss)
      for (int k1 = 0; k1 <= 7; k1++) {
        if (bitRead(ss, k1) == 1) {
          bitWrite(data[2], k1 + 1, 1);
        }
        else {
          bitWrite(data[2], k1 + 1, 0);
        }
      }
      if (ss == 0) {
        bitWrite(data[2], 0, 1);
      }
      else {
        bitWrite(data[2], 0, 0);
      }
      display.setSegments(data + 2 , 1, 3);   //Display 4

      break;

  }

}

Schematics

Schematics - Three-Modes Digital Clock
Schematics

Comments

Similar projects you might like

Arduino Bluetooth Basic Tutorial

by Mayoogh Girish

  • 454,633 views
  • 42 comments
  • 236 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 285,512 views
  • 95 comments
  • 671 respects

Security Access Using RFID Reader

by Aritro Mukherjee

  • 229,362 views
  • 38 comments
  • 236 respects

OpenCat

Project in progress by Team Petoi

  • 196,152 views
  • 154 comments
  • 1,363 respects
Add projectSign up / Login