Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
![]() |
|
This is a simple digital clock using the Lilygo TTGO T4 ESP32 and the DS3231 RTC module. It's basically plug and play. The RTC connects directly to the I2C connector on the T4 and after it has been set (code included) to the correct time, the display code can be flashed.
A short video of the project can be found here :
/*
DS3231_set.pde
Eric Ayars
4/11
Test of set-time routines for a DS3231 RTC
*/
#include <DS3231.h>
#include <Wire.h>
DS3231 clok;
byte year;
byte month;
byte date;
byte dOW;
byte hour;
byte minute;
byte second;
void getDateStuff(byte& year, byte& month, byte& date, byte& dOW,
byte& hour, byte& minute, byte& second) {
// Call this if you notice something coming in on
// the serial port. The stuff coming in should be in
// the order YYMMDDwHHMMSS, with an 'x' at the end.
boolean gotString = false;
char inChar;
byte temp1, temp2;
char inString[20];
byte j=0;
while (!gotString) {
if (Serial.available()) {
inChar = Serial.read();
inString[j] = inChar;
j += 1;
if (inChar == 'x') {
gotString = true;
}
}
}
Serial.println(inString);
// Read year first
temp1 = (byte)inString[0] -48;
temp2 = (byte)inString[1] -48;
year = temp1*10 + temp2;
// now month
temp1 = (byte)inString[2] -48;
temp2 = (byte)inString[3] -48;
month = temp1*10 + temp2;
// now date
temp1 = (byte)inString[4] -48;
temp2 = (byte)inString[5] -48;
date = temp1*10 + temp2;
// now Day of Week
dOW = (byte)inString[6] - 48;
// now hour
temp1 = (byte)inString[7] -48;
temp2 = (byte)inString[8] -48;
hour = temp1*10 + temp2;
// now minute
temp1 = (byte)inString[9] -48;
temp2 = (byte)inString[10] -48;
minute = temp1*10 + temp2;
// now second
temp1 = (byte)inString[11] -48;
temp2 = (byte)inString[12] -48;
second = temp1*10 + temp2;
}
void setup() {
// Start the serial port
Serial.begin(57600);
// Start the I2C interface
Wire.begin();
}
void loop() {
// If something is coming in on the serial line, it's
// a time correction so set the clok accordingly.
if (Serial.available()) {
getDateStuff(year, month, date, dOW, hour, minute, second);
clok.setClockMode(false); // set to 24h
//setclokMode(true); // set to 12h
clok.setYear(year);
clok.setMonth(month);
clok.setDate(date);
clok.setDoW(dOW);
clok.setHour(hour);
clok.setMinute(minute);
clok.setSecond(second);
// Test of alarm functions
// set A1 to one minute past the time we just set the clok
// on current day of week.
clok.setA1Time(dOW, hour, minute+1, second, 0x0, true,
false, false);
// set A2 to two minutes past, on current day of month.
clok.setA2Time(date, hour, minute+2, 0x0, false, false,
false);
// Turn on both alarms, with external interrupt
clok.turnOnAlarm(1);
clok.turnOnAlarm(2);
}
delay(1000);
}
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
//#include <Time.h>
#include <DS3231.h>
//#define SDA 21
//#define SCL 22
TFT_eSPI tft = TFT_eSPI();
DS3231 rtc;
int Sec = 1;
int Min = 38;
int Hr = 12;
int Weekday = 1;
int L_Weekday =1;
String L_Sec = "01";
String L_Min = "38";
String L_Hr = "12";
char T_emp = 247;
String T_emp2 = String(T_emp) + "c";
byte Temp = 23;
String L_Temp = "23";
bool h12Flag,pmFlag;
bool century = false;
String Dow []= {"0","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday" };
int Spc []= {0,92,79,53,66,92,66,92};
String MONTH []= {"0"," JANUARY","FEBRUARY"," MARCH"," APRIL"," MAY"," JUNE"," JULY"," AUGUST","SEPTEMBER"," OCTOBER","NOVEMBER","DECEMBER" };
int L_Month = 1;
int Months;
int DAY = 1;
String L_DAY = "01";
void setup() {
//Serial.begin(115200);
Wire.begin();
// rtc.setClockMode(false); // Run Once to set 12 true 24hr false
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(3);
tft.drawRect(0,0,319,239,TFT_RED);
tft.drawLine(0,100,319,100,TFT_RED);
tft.drawLine(0,160,319,160,TFT_RED);
tft.drawLine(160,160,160,239,TFT_RED);
tft.drawLine(160,200,339,200,TFT_RED); //x1,y1,x2,y2
tft.setTextColor(TFT_RED,TFT_BLACK);
tft.drawString("23",40,175,2);//Temperature
tft.drawString(T_emp2,92,175,1);
tft.setTextColor(TFT_YELLOW,TFT_BLACK);
tft.drawString("12:38",50,16,4);// Clock Digits x=50
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString("00",250,16,2);// Seconds Digits x=250
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.drawString("Monday",92,104,2);// x = 100
// tft.setTextColor(TFT_BLACK,TFT_BLACK);
// tft.drawString("38",155,16,4);// Minutes Digits x=155
// tft.drawString("12",50,16,4);// Hour Digits x = 50
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(MONTH [1],161,169,1);// Month
tft.setTextColor(TFT_YELLOW,TFT_BLACK);
tft.drawString("01",221,207,1);
}
void loop() {
Sec = rtc.getSecond(); // Get the seconds
if(L_Sec.toInt() != Sec) {
tft.setTextColor(TFT_BLACK,TFT_BLACK);
tft.drawString(L_Sec,250,16,2);
L_Sec = "0" + String(Sec);
L_Sec = L_Sec.substring(L_Sec.length()-2);
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(L_Sec,250,16,2);
}
Min = rtc.getMinute(); // Get the minutes
if (L_Min.toInt() != Min) {
tft.setTextColor(TFT_BLACK,TFT_BLACK);
tft.drawString(L_Min,155,16,4);
L_Min = "0" + String(Min);
L_Min = L_Min.substring(L_Min.length()-2);
tft.setTextColor(TFT_YELLOW,TFT_BLACK);
tft.drawString(L_Min,155,16,4);
}
Hr = rtc.getHour(h12Flag,pmFlag); //Get the hours
if (L_Hr.toInt() != Hr) {
tft.setTextColor(TFT_BLACK,TFT_BLACK);
tft.drawString(L_Hr,50,16,4);
L_Hr = "0" + String(Hr);
L_Hr = L_Hr.substring(L_Hr.length()-2);
tft.setTextColor(TFT_YELLOW,TFT_BLACK);
tft.drawString(L_Hr,50,16,4);
}
Weekday = rtc.getDoW(); // Get the Day of the week
if (L_Weekday != Weekday) {
tft.setTextColor(TFT_BLACK,TFT_BLACK);
tft.drawString(Dow[L_Weekday],Spc[L_Weekday],104,2);
tft.setTextColor(TFT_GREEN,TFT_BLACK);
tft.drawString(Dow[Weekday],Spc[Weekday],104,2);
L_Weekday = Weekday;
}
Temp = rtc.getTemperature(); // Get the temperature
if (L_Temp.toInt() != Temp) {
tft.setTextColor(TFT_BLACK,TFT_BLACK);
tft.drawString(L_Temp,40,175,2);
L_Temp = "0" + String(Temp);
L_Temp = L_Temp.substring(L_Temp.length()-2);
tft.setTextColor(TFT_BLUE,TFT_BLACK);
tft.drawString(L_Temp,40,175,2);
}
Months = rtc.getMonth(century); // Get the Month
if (L_Month != Months) {
tft.setTextColor(TFT_BLACK,TFT_BLACK);
tft.drawString(MONTH[L_Month],161,169,1);
L_Month = Months;
tft.setTextColor(TFT_WHITE,TFT_BLACK);
tft.drawString(MONTH[L_Month],161,169,1);
}
DAY = rtc.getDate(); // Get the date
if (L_DAY.toInt() != DAY) {
tft.setTextColor(TFT_BLACK,TFT_BLACK);
tft.drawString(L_DAY,221,207,1);
L_DAY = "0" + String(DAY);
L_DAY = L_DAY.substring(L_DAY.length()-2);
tft.setTextColor(TFT_YELLOW,TFT_BLACK);
tft.drawString(L_DAY,221,207,1);
}
}
28 projects • 12 followers
I've been interested in microprocessors for a long time.
Comments
Please log in or sign up to comment.