Working with the Real Time Clock (RTC) on evive | Arduino IDE

Description
Learn how to work with the Real Time Clock (RTC) on evive, using the PCF8563 IC and the evive.h library. Make projects such as a digital clock, attendance system, and more.

Introduction

A real-time clock (RTC) is a clock IC that keeps track of current time and date. It is used in the making of computer clocks, digital clocks, attendance systems, etc.

evive has an inbuilt RTC IC, PCF8563. The IC gets power from the internal battery of evive, so it can continue to update time while the external source of power is off or unavailable.

PCF8563

The PCF8563 is a CMOS logic-based Real-Time Clock and Calendar optimized for low power consumption. For the RTC available on evive we can set the starting date and time on it. The RTC IC communicates with evive on I2C protocol(Two Wire Communication).

It consists of a 32.768 kHz quartz crystal that provides year, month, day, weekday, hours, minutes, and seconds. It also has a century flag, alarm, and timer functions, an integrated oscillator capacitor, and an internal Power-On Reset.

Read/Write operations on RTC with evive

evive communicates with RTC via IIC-bus. Below is a list of a few read-write functions used for RTC  present in evive.h library:

FunctionDescription
initclock()Clears all the RTC registers.
setDate(day,weekDay,month,century,year);Used to set date on RTC. Numeric entries corresponding to day, weekDay,month,century,year are to be given.
setTime(h,m,s)Set the current hour,minutes,seconds for setting start time of RTC.
formatDate(byte style)This function returns date in mm/dd/yyyy or dd-mm-yyyy or yyyy-mm-dd format depending upon hex value of style variable written in it.
formatTime(byte style)Gives time in HH:MM or HH:MM:SS format depending on hex value of style variable written in it.
getWeekDay()returns a number depending upon which day of the week it is. For example this function will return 6 for Saturday.

Example

Below is a sample Arduino IDE sketch demonstrating how to write and read the time on RTC:

/*
  evive RTC Write Operation

  This code demonstrate how to set time on PCF8563 (a Real Time Clock) and get updated time from it.
  Here intially time of RTC is set.After that time and date updated by rtc are printed on evive screen.
  Created by Mimansa Maheshwari
  This code is in public domain.
  Explore more on: https://thestempedia.com/tutorials/rtc/
*/
#include <evive.h>
Rtc_Pcf8563 rtc;
String weekDay[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup() {
  tft.init(INITR_BLACKTAB);
  tft.setRotation(1);
  tft.fillScreen(ST7735_BLACK);
  
  //Draw round rectangle border on evive screen
  tft.drawRoundRect(2, 2, 156, 123, 10, ST7735_BLUE);
  
  //Print evive logo on screen
  tft.drawBitmap(45, 20, evive_logo, 71, 71, ST7735_BLUE);

  //clears all registers
  rtc.initClock();   //initialize  clock
  /* set a date to start with.
     day, weekday, month, century, year */
  rtc.setDate(28, 6, 7, 0, 18);            // century 0 for 2000 and 1 for 1999.  
  /*set time in  hr, min, sec  format*/
  rtc.setTime(1, 15, 40);
  
}

void loop() {
  //Get Date from RTC and print it on evive Screen
  tft.setCursor(7, 7);
  tft.setTextSize(1);
  tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
  tft.print(rtc.formatDate(0x01));             //0x01 for dd-mm-yyyy, 0x02 for yyyy-mm-dd ,0x04 for mm/dd/yyyy format
  
  //Get weekDay from RTC and print it on evive Screen
  tft.setCursor(135, 7);
  tft.setTextSize(1);
  tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
  tft.print(weekDay[rtc.getWeekday()]);
  
  //Get Time from RTC and print it on evive Screen
  tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
  tft.setTextSize(2);
  tft.setCursor(33, 100);
  tft.print(rtc.formatTime(0x01));            //0x01 for Hours:minutes:seconds , 0x02 for Hours:Minutes 
}

Conclusion

In this lesson, we learned about the RTC available on evive and how to use it to write and read the time from it. We saw how to use the RTC IC PCF8563 present on evive and the various read/write functions available in the evive.h library. We also saw how to display the time and date on the evive TFT screen. With this knowledge, you can now make various projects such as a digital clock, attendance system, etc.

Table of Contents