stevie135s
Published © GPL3+

Digital Clock on Arduino Nano

A digital clock using an Arduino Nano with the ST7735 colour display and a DS3231 RTC

IntermediateWork in progress8 hours414
Digital Clock on Arduino Nano

Things used in this project

Hardware components

ST7735 80x160 0.96inch Display
×1
Arduino Nano
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Wiring diagram

Don't Forget the resistors

Code

ST7735S_Digital_Clock_Display

Arduino
Updated to fix Month (single digits) and refresh Dow
/*  **************************************************************************
*    Digital Clock Project using ST7735 80 x 160 (RGB) .96 inch IPS Display
*    With an Arduino Nano and a DS3231 RTC
*    
*    Pin Used With The Arduino Nano :
*    A5 SCL RTC
*    A4 SDA RTC
*    
*    D5 RST  ST7735
*    D6 CS
*    D7 DC
*    D13 SCL/SCK *These markings are confusing*
*    D11 MOSI/SDA
*    
     **************************************************************************
*/




 
   #include <Adafruit_GFX.h>
   #include <Adafruit_ST7735.h>
   #include <SPI.h>
   #include <DS3231.h>      //https://github.com/NorthernWidget/DS3231/releases
   #include <Wire.h>

   #define TFT_RST  5
   #define TFT_CS   6
   #define TFT_DC   7
  
   Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
   DS3231 myRTC;
   
// inverted color definitions
   #define BLACK   0xFFFF
   #define WHITE   0x0000
   #define BLUE    0x07FF
   #define RED     0xFFE0 
   #define GREEN   0xF81F
   #define YELLOW  0xF800 
   #define BROWN   0x9F6D
   #define L_BLUE  0x51E4

byte L_Hour = 18;   // Last Hour
byte L_Min = 43;    // Last Minute
byte L_Sec = 10;    // Last Second
byte L_Dow = 6;     // Last Day Of The Week
byte L_Date = 11;   // Last Date
byte L_Month = 11;  // Last Month
byte L_Temp = 28;   // Last Temperature
byte L_Year;        // Last Year
byte YEAR = 20;     // Year 2000
byte YEAR_2;        // Last 2 digits of the year

byte Sec,Min,HOUR,Dow,Date,MONTH,Temp;    // Variables from RTC
bool h12Flag,pmFlag;                      //Unused 12h pm Flags
bool century = false;

String DAYS_OF_WEEK [] {"Dummy","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

void setup () {

   tft.initR (INITR_BLACKTAB);
   Wire.begin();
   tft.setRotation (3);    // Display Lasdscape
   tft.fillScreen (BLACK);
   LinesDraw();            // Draw Lines Routine
   MONTH = myRTC.getMonth(century);
   if (century) {         // Year 2100
    YEAR = 21; 
   }
   YEAR_2 = myRTC.getYear();
   L_Year = YEAR_2;    
   TextDraw();            // Display All Screen Text
   tft.setTextSize(3);    // Time Text Size


   
}

void loop (){

Sec = myRTC.getSecond();
if (Sec != L_Sec) {
  Sec_Print();
}  
  Min = myRTC.getMinute();
  if (Min != L_Min) {
   Min_Print();
}
  HOUR = myRTC.getHour(h12Flag, pmFlag);
  if (HOUR != L_Hour) {
  Hour_Print();
}
  Dow = myRTC.getDoW();
  if (L_Dow != Dow) {
  tft.setTextSize(2);
  Dow_Print();
  tft.setTextSize(3);  
}
   
  
 

Date = myRTC.getDate();
if (L_Date != Date) {
  tft.setTextSize(2);
  Date_Print();


MONTH = myRTC.getMonth(century);
if (L_Month != MONTH) {
  Month_Print();

  YEAR_2 = myRTC.getYear();
  if (YEAR_2 != L_Year) {
    if (century) {
      YEAR = 21;
    }
    Year_Print();
    
  }
 }
   tft.setTextSize(3);
}

Temp = myRTC.getTemperature();
if (L_Temp != Temp) {
  tft.setTextSize(2);
  Temp_Print();
  tft.setTextSize(3);
 }
}

void LinesDraw() {    //Draw The Screen Borders

  tft.drawLine(3,54,157,54,RED);
  tft.drawLine(3,55,157,55,RED);
  tft.drawLine(3,80,157,80,RED);
  tft.drawLine(3,81,157,81,RED);
  tft.drawLine(112,56,112,79,RED);
  tft.drawLine(113,56,113,79,RED);
 
  tft.drawRect(1,26,159,80,RED);
  tft.drawRect(2,27,157,78,RED);

  tft.fillRect(114,55,44,25,BROWN);  // Temperature Box
}
 void TextDraw() {     //Set up Startup Screen
     tft.setTextSize(3);
     tft.setTextColor(WHITE);
     tft.setCursor(10,30);
     tft.print(L_Hour);
     tft.print(":");
     tft.print(L_Min);
     tft.print(":");
     tft.print(L_Sec);

     tft.setTextSize(2);
     tft.setTextColor(GREEN);
     tft.setCursor(4 +((108-(DAYS_OF_WEEK [L_Dow].length())*12)/2),60);
     tft.print(DAYS_OF_WEEK [L_Dow]);
     
     tft.setTextColor(L_BLUE);
     tft.setCursor(20,86);
     tft.print("11/11/");
     tft.print(YEAR);
     tft.print(L_Year);
     
     tft.setTextColor(YELLOW);
     tft.setCursor(120,60);
     tft.print("28c");
 }

 void Sec_Print () {      // Print Seconds
  tft.setTextColor(BLACK);
  tft.setCursor(118,30);
  if (L_Sec < 10) {
    tft.print("0");
  }
  tft.print(L_Sec);
  L_Sec = Sec;
  tft.setTextColor(WHITE);
  tft.setCursor(118,30);
  if (L_Sec < 10) {
    tft.print("0");
  }
  tft.print(L_Sec);
 }

 void Min_Print () {    // Print Minutes
  tft.setTextColor(BLACK);
  tft.setCursor(64,30);
  if (L_Min < 10) {
    tft.print("0");
  }
  tft.print(L_Min);
  L_Min = Min;
  tft.setTextColor(WHITE);
  tft.setCursor(64,30);
  if (L_Min < 10) {
    tft.print("0");
  }
  tft.print(L_Min);
 }

 void Hour_Print () {   // Print Hours
  tft.setTextColor(BLACK);
  tft.setCursor(10,30);
  if (L_Hour < 10) {
    tft.print("0");
  }
  tft.print(L_Hour);
  L_Hour = HOUR;
  tft.setTextColor(WHITE);
  tft.setCursor(10,30);
  if (L_Hour < 10) {
    tft.print("0");
  }
  tft.print(L_Hour);
 }

 void Dow_Print() {     // Print Days Of The Week String
  tft.setTextColor(BLACK);
  tft.setCursor(4 +((108-(DAYS_OF_WEEK [L_Dow].length())*12)/2),60);
  tft.print(DAYS_OF_WEEK [L_Dow]);
  L_Dow = Dow;
  tft.setTextColor(GREEN);
  tft.setCursor(4 +((108-(DAYS_OF_WEEK [L_Dow].length())*12)/2),60); 
  tft.print(DAYS_OF_WEEK[L_Dow]);
 }

void Date_Print() {     // Print Date
  tft.setTextColor(BLACK);
  tft.setCursor(20,86);
  if (L_Date < 10) {
    tft.print("0");
  }
  tft.print(L_Date);
  L_Date = Date;
  tft.setTextColor(L_BLUE);
  tft.setCursor(20,86);
  if (L_Date < 10) {
    tft.print("0");
  }  
  tft.print(L_Date);
}

void Month_Print(){     // Print Month
  // 56x  86y
  tft.setTextColor(BLACK);
  tft.setCursor(56,86);
  if (L_Month < 10) {
    tft.print("0");
  }
  tft.print(L_Month);
  L_Month = MONTH;
  tft.setTextColor(L_BLUE);
  tft.setCursor(56,86);
  if (L_Month < 10) {
    tft.print("0");
  }  
  tft.print(L_Month);
}

void Temp_Print() {     // Print Temperature
  tft.setTextColor(BROWN);
  tft.setCursor(120,60);
  if (L_Temp < 10) {
    tft.print("0");
  }
  tft.print(L_Temp);
  L_Temp = Temp;
  tft.setTextColor(YELLOW);
  tft.setCursor(120,60);
  if (L_Temp < 10) {
    tft.print("0");
  }
  tft.print(L_Temp);
}

void Year_Print() {       // Print the Year
  tft.setTextColor(BLACK);
  tft.setCursor(91,86);
  tft.print(YEAR);
  tft.print(L_Year);
  tft.setTextColor(L_BLUE);
  tft.setCursor(91,86);
  tft.print(YEAR);
  L_Year = YEAR_2;
  tft.print(L_Year);
}

Credits

stevie135s

stevie135s

24 projects • 10 followers

Comments