Arduino DS3231 Real Time Clock With Color TFT

by educ8s in Circuits > Arduino

28437 Views, 57 Favorites, 0 Comments

Arduino DS3231 Real Time Clock With Color TFT

instru.png
Color Real Time Clock.png

Building a real time clock with a color TFT display is extremely easy using Arduino. The display shows the time, the date, the currect temperature along the MAX and MIN temperature that it has measured. Let's build it!

Get the Parts

instr1.png
Arduino Project: Real time clock DS3231 and TFT

The parts needed in order to build this project are these:

  1. Arduino Uno ▶ http://educ8s.tv/part/ArduinoUno
  2. ST7735 TFT ▶ http://educ8s.tv/part/7735
  3. DS3231 RTC ▶ http://educ8s.tv/part/DS3231
  4. Small Breadboard ▶ http://educ8s.tv/part/SmallBreadboard
  5. Power Bank ▶ http://educ8s.tv/part/Powerbank

The total cost of the parts at the time of writing this text is 12.79$.

Connect the DS3231 Real Time Clock Module to Arduino

instru2.png

First we connect the real time clock module to Arduino.

Vcc -> Arduino 5V

GND -> Arduino GND

SDA -> A4 of Arduino

SCL -> A5 of Arduino

Connect the ST7735S Display

instructubles.png
Arduino Tutorial: Using the ST7735 1.8" Color TFT Display with Arduino.

The next step is connect the ST7735S display. First we plug in the display at the small breadboard and then we connect to the Arduino Uno board. The connection is as this:

Display -> Arduino

LED -> 3.3V

CSK -> D13

SDA -> D11

A0 -> D9

RESET -> D8

CS -> D10

GND -> GND

VCC -> 5V

Please watch the detailed tutorial I have prepared on how to connect the ST7735S to an Arduino Uno here:

Upload the Code to Arduino

<#include  <Adafruit_ST7735.h>
#include  <Adafruit_GFX.h>
#include <Wire.h>
#include "Sodaq_DS3231.h"
#include  <SPI.h>

#define TFT_CS     10
#define TFT_RST    8                      
#define TFT_DC     9

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS,  TFT_DC, TFT_RST);

// Option 2: use any pins but a little slower!
#define TFT_SCLK 13   // set these to be whatever pins you like!
#define TFT_MOSI 11   // set these to be whatever pins you like!

float maxTemperature=0;
float minTemperature=200;
char charMinTemperature[10];
char charMaxTemperature[10];
char timeChar[100];
char dateChar[50];
char temperatureChar[10];

float temperature = 0;
float previousTemperature = 0;

String dateString;
int minuteNow=0;
int minutePrevious=0;

void setup () 
{
    tft.initR(INITR_BLACKTAB);
    tft.fillScreen(ST7735_BLACK);
    Serial.begin(57600);
    Wire.begin();
    rtc.begin();
    printText("TEMPERATURE", ST7735_GREEN,30,65,1);  // Temperature Static Text
    printText("MAX", ST7735_RED,18,130,1);
    printText("MIN", ST7735_BLUE,95,130,1);
    //setRTCTime();
}

uint32_t old_ts;

void loop () 
{
  float temperature = rtc.getTemperature();
  DateTime now = rtc.now(); //get the current date-time
  uint32_t ts = now.getEpoch();

    if (old_ts == 0 || old_ts != ts) {
	old_ts = ts;
  
  minuteNow = now.minute();
  if(minuteNow!=minutePrevious)
  {
    dateString = getDayOfWeek(now.dayOfWeek())+", ";
    dateString = dateString+String(now.date())+"/"+String(now.month());
    dateString= dateString+"/"+ String(now.year()); 
    minutePrevious = minuteNow;
    String hours = String(now.hour());
    if(now.minute()<10)
    {
      hours = hours+":0"+String(now.minute());
    }else
    {
      hours = hours+":"+String(now.minute());
    }
    
    hours.toCharArray(timeChar,100);
    tft.fillRect(10,0,160,65,ST7735_BLACK);
    printText(timeChar, ST7735_WHITE,20,25,3);
    dateString.toCharArray(dateChar,50);
    printText(dateChar, ST7735_GREEN,8,5,1);
  }
  
  if(temperature != previousTemperature)
  {
    previousTemperature = temperature;
    String temperatureString = String(temperature,1);
    temperatureString.toCharArray(temperatureChar,10);
    tft.fillRect(10,80,128,30,ST7735_BLACK);
    printText(temperatureChar, ST7735_WHITE,10,80,3);
    printText("o", ST7735_WHITE,90,75,2);
    printText("C", ST7735_WHITE,105,80,3);

if(temperature>maxTemperature)
    {
      maxTemperature = temperature;
      dtostrf(maxTemperature,5, 1, charMaxTemperature); 
      tft.fillRect(3,142,33,20,ST7735_BLACK);
      printText(charMaxTemperature, ST7735_WHITE,3,145,1);
      printText("o", ST7735_WHITE,35,140,1);
      printText("C", ST7735_WHITE,41,145,1);
    }

if(temperature < minTemperature

{ minTemperature = temperature; dtostrf(minTemperature,5, 1, charMinTemperature); tft.fillRect(75,140,36,18,ST7735_BLACK); printText(charMinTemperature, ST7735_WHITE,80,145,1); printText("o", ST7735_WHITE,112,140,1); printText("C", ST7735_WHITE,118,145,1); } } } delay(1000); }

void setRTCTime() { DateTime dt(2015, 8, 27, 9, 35, 30, 4); // Year, Month, Day, Hour, Minutes, Seconds, Day of Week rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above }void printText(char *text, uint16_t color, int x, int y,int textSize) { tft.setCursor(x, y); tft.setTextColor(color); tft.setTextSize(textSize); tft.setTextWrap(true); tft.print(text); } String getDayOfWeek(int i) { switch(i) { case 1: return "Monday";break; case 2: return "Tuesday";break; case 3: return "Wednesday";break; case 4: return "Thursday";break; case 5: return "Friday";break; case 6: return "Saturday";break; case 7: return "Sunday";break; default: return "Monday";break; } }

Get the code as a file here: http://educ8s.tv/arduino-real-time-clock/

After uploading the code, the first time you run the sketch you have to set the time. Uncomment this line of code here:

<p>//setRTCTime();</p>

Next set current date and time at the following function.

<p>void setRTCTime()<br>{
  DateTime dt(2015, 8, 27, 9, 35, 30, 4); // Year, Month, Day, Hour, Minutes, Seconds, Day of Week
  rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above 
}</p>

After setting the time, upload the sketch and run it once.

Next, comment the line the sets the time and updload the sketch once more.

setRTCTime();

Now, the program can keep time with the embedded battery for years!

Enjoy your color Real Time Clock!

Never miss a project by subscribing to my YouTube channel:

https://www.youtube.com/subscription_center?add_us...