This circuit integrates an ESP32 microcontroller with a 1.8-inch Adafruit TFT display and a Real-Time Clock (RTC) module. The ESP32 is responsible for controlling the display and maintaining accurate time using the RTC. The display shows the current time, which is updated every second. The circuit is designed to be powered by a 5V supply connected to the ESP32, which also provides 3.3V to the RTC module.
#include <RtcDS1302.h>
#include <ThreeWire.h>
/*
* This Arduino Sketch is for an ESP32 microcontroller to create a clock
* with a TFT display and an RTC module. The code initializes the TFT
* display and the RTC module, then displays the current time on the TFT
* display. The time is updated every second.
*/
#include <TFT_eSPI.h> // Include the graphics library
#include <SPI.h> // Include the SPI library
#include <Wire.h> // Include the Wire library for I2C
#include <RTClib.h> // Include the RTC library
const int IO = 27; // DAT
const int SCLK = 14; // CLK
const int CE = 26; // RST
ThreeWire myWire(IO, SCLK, CE);
RtcDS1302<ThreeWire> Rtc(myWire);
TFT_eSPI tft = TFT_eSPI(); // Create TFT object
RTC_DS3231 rtc; // Create RTC object
void setup() {
tft.init();
tft.setRotation(1); // Set the rotation of the display
tft.fillScreen(TFT_BLACK); // Clear the screen
tft.setTextColor(TFT_WHITE, TFT_BLACK); // Set text color
tft.setTextSize(2); // Set text size
tft.println("Kello");
if (!rtc.begin()) {
tft.println("Couldn't find RTC");
Serial.print("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
tft.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
tft.fillScreen(TFT_BLACK); // Clear the screen
tft.setCursor(0, 0); // Set cursor to top-left corner
tft.printf("%02d:%02d:%02d", now.hour(), now.minute(), now.second());
tft.println("Kellooo");
Serial.printf("%02d:%02d:%02d", now.hour(), now.minute(), now.second());
delay(1000); // Wait for 1 second
}
Filename: sketch.ino
This code is designed to run on the ESP32 microcontroller. It initializes the TFT display and the RTC module, then enters a loop where it continuously updates the display with the current time. The time is formatted as hours, minutes, and seconds, and is updated every second. If the RTC has lost power, the time is set to the time the code was compiled.