// Include the TFT_eSPI library
#include <TFT_eSPI.h>
// Create an instance of the TFT_eSPI class
TFT_eSPI tft = TFT_eSPI();
// Define the pins for the TFT display
#define TFT_CS 5 // Chip select pin
#define TFT_DC 4 // Data/command pin
#define TFT_RST 2 // Reset pin
/*********
You can use the HSPI port of the ESP32 to connect the touch SPI pins of the TFT. The pinout is as follows1:
TFT Pin ESP32 Pin
T_IRQ No connection required
T_DO 12
T_DIN 13
T_CS 33
T_CLK 14
You can also use the VSPI port of the ESP32 to connect the TFT SPI pins. The pinout is as follows2:
TFT Pin ESP32 Pin
MOSI 23
SCK 18
CS 15
RST 4
DC 2
You can also connect a microSD card to the ESP32 board using the same VSPI port. The pinout is as follows3:
microSD Pin ESP32 Pin
MOSI 23
SCK 18
MISO 19
CS
******************/
// Define the colors for the clock
#define TFT_BLACK 0x0000
#define TFT_WHITE 0xFFFF
#define TFT_RED 0xF800
#define TFT_GREEN 0x07E0
#define TFT_BLUE 0x001F
#define TFT_YELLOW 0xFFE0
#define TFT_CYAN 0x07FF
#define TFT_MAGENTA 0xF81F
// Define the radius and center of the clock
#define CLOCK_RADIUS 100
#define CLOCK_CENTER_X 160
#define CLOCK_CENTER_Y 120
// Define the length and width of the clock hands
#define HOUR_HAND_LENGTH 50
#define HOUR_HAND_WIDTH 5
#define MINUTE_HAND_LENGTH 80
#define MINUTE_HAND_WIDTH 3
#define SECOND_HAND_LENGTH 90
#define SECOND_HAND_WIDTH 1
// Define the angle offset for the clock hands
#define HOUR_ANGLE_OFFSET 270
#define MINUTE_ANGLE_OFFSET 270
#define SECOND_ANGLE_OFFSET 270
// Define the font size and color for the clock numbers
#define CLOCK_FONT_SIZE 2
#define CLOCK_FONT_COLOR TFT_WHITE
// Define the variables for storing the current time
int hour, minute, second;
// Define the variables for storing the previous clock hand positions
int old_hour_x, old_hour_y, old_minute_x, old_minute_y, old_second_x, old_second_y;
// Define the variable for storing the previous second value
int old_second;
void setup() {
// Initialize the TFT display
tft.init();
tft.setRotation(0); // Set the rotation of the display
tft.fillScreen(TFT_CYAN); // Fill the screen with black color
tft.setTextSize(CLOCK_FONT_SIZE); // Set the text size for the clock numbers
// Draw the clock face
drawClockFace();
// Initialize the time variables
hour = 10;
minute = 50;
second = 40;
// Initialize the previous clock hand positions
old_hour_x = CLOCK_CENTER_X;
old_hour_y = CLOCK_CENTER_Y - HOUR_HAND_LENGTH;
old_minute_x = CLOCK_CENTER_X;
old_minute_y = CLOCK_CENTER_Y - MINUTE_HAND_LENGTH;
old_second_x = CLOCK_CENTER_X;
old_second_y = CLOCK_CENTER_Y - SECOND_HAND_LENGTH;
// Initialize the previous second value
old_second = 0;
}
void loop() {
// Get the current time from the ESP32
hour = hour;
minute = minute;
second = second;
// Check if the second value has changed
if (second != old_second) {
// Erase the previous clock hands
eraseClockHands();
// Draw the new clock hands
drawClockHands();
// Update the previous second value
old_second = second;
}
}
// A function to draw the clock face
void drawClockFace() {
// Draw the clock circle
tft.drawCircle(CLOCK_CENTER_X, CLOCK_CENTER_Y, CLOCK_RADIUS, TFT_WHITE);
// Draw the clock numbers
for (int i = 1; i <= 12; i++) {
// Calculate the position of the number
float angle = i * 30 * PI / 180; // Convert the angle to radians
int x = CLOCK_CENTER_X + (CLOCK_RADIUS - 10) * cos(angle); // Calculate the x coordinate
int y = CLOCK_CENTER_Y + (CLOCK_RADIUS - 10) * sin(angle); // Calculate the y coordinate
// Draw the number
tft.setTextColor(CLOCK_FONT_COLOR); // Set the text color
tft.setCursor(x - 5, y - 5); // Set the cursor position
tft.print(i); // Print the number
}
}
// A function to erase the previous clock hands
void eraseClockHands() {
// Erase the previous hour hand
tft.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, old_hour_x, old_hour_y, TFT_BLACK);
// Erase the previous minute hand
tft.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, old_minute_x, old_minute_y, TFT_BLACK);
// Erase the previous second hand
tft.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, old_second_x, old_second_y, TFT_BLACK);
}
// A function to draw the new clock hands
void drawClockHands() {
// Calculate the angle of the hour hand
float hour_angle = (hour % 12 + minute / 60.0) * 30 * PI / 180; // Convert the hour to angle in radians
hour_angle += HOUR_ANGLE_OFFSET * PI / 180; // Add the angle offset
// Calculate the position of the hour hand
int hour_x = CLOCK_CENTER_X + HOUR_HAND_LENGTH * cos(hour_angle); // Calculate the x coordinate
int hour_y = CLOCK_CENTER_Y + HOUR_HAND_LENGTH * sin(hour_angle); // Calculate the y coordinate
// Draw the hour hand
tft.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, hour_x, hour_y, TFT_YELLOW);
// Update the previous hour hand position
old_hour_x = hour_x;
old_hour_y = hour_y;
// Calculate the angle of the minute hand
float minute_angle = minute * 6 * PI / 180; // Convert the minute to angle in radians
minute_angle += MINUTE_ANGLE_OFFSET * PI / 180; // Add the angle offset
// Calculate the position of the minute hand
int minute_x = CLOCK_CENTER_X + MINUTE_HAND_LENGTH * cos(minute_angle); // Calculate the x coordinate
int minute_y = CLOCK_CENTER_Y + MINUTE_HAND_LENGTH * sin(minute_angle); // Calculate the y coordinate
// Draw the minute hand
tft.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, minute_x, minute_y, TFT_CYAN);
// Update the previous minute hand position
old_minute_x = minute_x;
old_minute_y = minute_y;
// Calculate the angle of the second hand
float second_angle = second * 6 * PI / 180; // Convert the second to angle in radians
second_angle += SECOND_ANGLE_OFFSET * PI / 180; // Add the angle offset
// Calculate the position of the second hand
int second_x = CLOCK_CENTER_X + SECOND_HAND_LENGTH * cos(second_angle); // Calculate the x coordinate
int second_y = CLOCK_CENTER_Y + SECOND_HAND_LENGTH * sin(second_angle); // Calculate the y coordinate
// Draw the second hand
tft.drawLine(CLOCK_CENTER_X, CLOCK_CENTER_Y, second_x, second_y, TFT_RED);
// Update the previous second hand position
old_second_x = second_x;
old_second_y = second_y;
}