This is my Digital Clock project using the 3v Arduino Pro Mini and the ST7735S SPI 80x160 display.
I find a lot of people have difficulty interfacing the SPI interface on the Pro Mini so this project shows details how.
I have used the 3v Pro Mini as this display uses 3v inputs. If you want to use the 5v Pro Mini, be sure to use 220 ohm resistors on the control lines as well as the VCC.
The DS3231 RTC will run on 3v-5v. I used a breadboard supply of 3.3v for this project.
The Display uses the SPI interface, the RTC uses the I2C interface pins A4 and A5 located on the top of the Arduino.
CODE
/* **************************************************************************
* Digital Clock Project using ST7735 80 x 160 (RGB) .96 inch IPS Display
* With an Arduino Pro Mini and a DS3231 RTC
*
* Pin Used With The Arduino Pro Mini :
* A5 SCL RTC
* A4 SDA RTC
*
* RST RST -1
* 10 CS
* 9 DC
* 13 SCL/SCK *These markings are confusing*
* 11 MOSI/SDA
*
**************************************************************************
*/
#include <Arduino.h>
#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 -1
#define TFT_CS 10
#define TFT_DC 9
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);
}
License
All Rights
Reserved
0
More from this category