This topic shows how to build a real time clock with temperature monitor using Arduino, DS3231 RTC and ST7735R (ST7735S) 1.8″ color TFT display.
Building this project with Arduino is so easy, in the circuit there are two pushbuttons to set the time as well as the date, time, date and temperature are displayed on 1.8″ (128 x 160) TFT screen with ST7735 controller.
Previously, I made some projects with Arduino and DS3231, links are below:
Arduino and DS3231 real time clock
Arduino real time clock with alarm and temperature monitor using DS3231
Arduino + DS3231 RTC with alarms, temperature monitor & remote control
And the link below shows a simple interfacing example of the Arduino and the ST7735 TFT:
Arduino ST7735 1.8″ TFT display example
Components Required:
To build this project we need the following components.
- Arduino board
- DS3231 board
- ST7735R (ST7735S) TFT screen
- 5 x 1K ohm resistor
- 2 x push button
- 3V coin cell battery
- Breadboard
- Jumper wires
Arduino with DS3231 RTC and ST7735 TFT circuit:
Project circuit schematic diagram is shown below.
(All grounded terminals are connected together)
In the circuit there are 2 push buttons (B1 & B2) connected to Arduino pins 7 and 6 respectively, the two push buttons are used to set time and date parameters (minutes, hours, day of the week, date, month and year). Button B1 selects the parameter and B2 increments the selected parameter.
Arduino with DS3231 RTC and ST7735 TFT code:
We need 2 libraries (Adafruit_ST7735 and Adafruit_GFX) from Adafruit industries in order to compile the Arduino code below, these libraries can be downloaded from the link below:
Download
Place both libraries in Arduino IDE libraries folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | // Arduino with DS3231 and ST7735R TFT display // Real time clock & calendar with temperature monitor and 1.8" TFT screen // include Wire library code (needed for I2C protocol devices) #include <Wire.h> // include ST7735 library #include <Adafruit_GFX.h> // Core graphics library #include <Adafruit_ST7735.h> // Hardware-specific library #include <SPI.h> #define TFT_CS 9 // TFT CS pin is connected to arduino pin 9 #define TFT_DC 10 // TFT DC pin is connected to arduino pin 10 Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC); #define button1 7 // Button B1 is connected to Arduino pin 7 #define button2 6 // Button B2 is connected to Arduino pin 6 void setup(void) { pinMode(button1, INPUT_PULLUP); pinMode(button2, INPUT_PULLUP); tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab tft.fillScreen(ST7735_BLACK); Wire.begin(); // Join i2c bus tft.drawFastHLine(0, 53, tft.width(), ST7735_BLUE); // Draw horizontal blue line at position (0, 53) tft.drawFastHLine(0, 106, tft.width(), ST7735_BLUE); // Draw horizontal blue line at position (0, 106) draw_text(52, 64, "TIME", 1, ST7735_MAGENTA); draw_text(30, 117, "TEMPERATURE", 1, ST7735_RED); } char Time[] = " : : "; char Calendar[] = " / /20 "; char temperature[] = " 00.00"; char temperature_msb; byte i, second, minute, hour, day, date, month, year, temperature_lsb; void display_day(){ switch(day){ case 1: draw_text(40, 10, " SUNDAY ", 1, ST7735_CYAN); break; case 2: draw_text(40, 10, " MONDAY ", 1, ST7735_CYAN); break; case 3: draw_text(40, 10, " TUESDAY ", 1, ST7735_CYAN); break; case 4: draw_text(40, 10, "WEDNESDAY", 1, ST7735_CYAN); break; case 5: draw_text(40, 10, "THURSDAY ", 1, ST7735_CYAN); break; case 6: draw_text(40, 10, " FRIDAY ", 1, ST7735_CYAN); break; default: draw_text(40, 10, "SATURDAY ", 1, ST7735_CYAN); } } void DS3231_display(){ // Convert BCD to decimal second = (second >> 4) * 10 + (second & 0x0F); minute = (minute >> 4) * 10 + (minute & 0x0F); hour = (hour >> 4) * 10 + (hour & 0x0F); date = (date >> 4) * 10 + (date & 0x0F); month = (month >> 4) * 10 + (month & 0x0F); year = (year >> 4) * 10 + (year & 0x0F); // End conversion Time[7] = second % 10 + 48; Time[6] = second / 10 + 48; Time[4] = minute % 10 + 48; Time[3] = minute / 10 + 48; Time[1] = hour % 10 + 48; Time[0] = hour / 10 + 48; Calendar[9] = year % 10 + 48; Calendar[8] = year / 10 + 48; Calendar[4] = month % 10 + 48; Calendar[3] = month / 10 + 48; Calendar[1] = date % 10 + 48; Calendar[0] = date / 10 + 48; if(temperature_msb < 0){ temperature_msb = abs(temperature_msb); temperature[0] = '-'; } else temperature[0] = ' '; temperature_lsb >>= 6; temperature[2] = temperature_msb % 10 + 48; temperature[1] = temperature_msb / 10 + 48; if(temperature_lsb == 0 || temperature_lsb == 2){ temperature[5] = '0'; if(temperature_lsb == 0) temperature[4] = '0'; else temperature[4] = '5'; } if(temperature_lsb == 1 || temperature_lsb == 3){ temperature[5] = '5'; if(temperature_lsb == 1) temperature[4] = '2'; else temperature[4] = '7'; } draw_text(4, 27, Calendar, 2, ST7735_YELLOW); draw_text(16, 81, Time, 2, ST7735_GREEN); draw_text(14, 134, temperature, 2, ST7735_WHITE); tft.drawCircle(90, 134, 2, ST7735_WHITE); // Degree symbol (°) draw_text(96, 134, "C", 2, ST7735_WHITE); } void blink_parameter(){ byte j = 0; while(j < 10 && digitalRead(button1) && digitalRead(button2)){ j++; delay(25); } } byte edit(byte x_pos, byte y_pos, byte parameter){ char text[3]; uint16_t color = ST7735_YELLOW; sprintf(text,"%02u", parameter); if(i == 3 || i == 4) color = ST7735_GREEN; while(!digitalRead(button1)); // Wait until button B1 released while(true){ while(!digitalRead(button2)){ // If button (pin #9) is pressed parameter++; if(i == 0 && parameter > 31) // If date > 31 ==> date = 1 parameter = 1; if(i == 1 && parameter > 12) // If month > 12 ==> month = 1 parameter = 1; if(i == 2 && parameter > 99) // If year > 99 ==> year = 0 parameter = 0; if(i == 3 && parameter > 23) // If hours > 23 ==> hours = 0 parameter = 0; if(i == 4 && parameter > 59) // If minutes > 59 ==> minutes = 0 parameter = 0; sprintf(text,"%02u", parameter); draw_text(x_pos, y_pos, text, 2, color); delay(200); // Wait 200ms } tft.fillRect(x_pos, y_pos, 22, 16, ST7735_BLACK); blink_parameter(); draw_text(x_pos, y_pos, text, 2, color); blink_parameter(); if(!digitalRead(button1)){ // If button (pin #8) is pressed i++; // Increament 'i' for the next parameter return parameter; // Return parameter value and exit } } } void draw_text(byte x_pos, byte y_pos, char *text, byte text_size, uint16_t text_color) { tft.setCursor(x_pos, y_pos); tft.setTextSize(text_size); tft.setTextColor(text_color, ST7735_BLACK); tft.print(text); } void loop() { if(!digitalRead(button1)){ // If button B1 is pressed i = 0; while(!digitalRead(button1)); // Wait for button B1 release while(true){ while(!digitalRead(button2)){ // While button B2 pressed day++; // Increment day if(day > 7) day = 1; display_day(); // Call display_day function delay(200); // Wait 200 ms } tft.fillRect(40, 10, 54, 8, ST7735_BLACK); // Draw rectangle (earase day from the display) blink_parameter(); // Call blink_parameter function display_day(); // Call display_day function blink_parameter(); // Call blink_parameter function if(!digitalRead(button1)) // If button B1 is pressed break; } date = edit(4, 27, date); // Edit date month = edit(40, 27, month); // Edit month year = edit(100, 27, year); // Edit year hour = edit(16, 81, hour); // Edit hours minute = edit(52, 81, minute); // Edit minutes // Convert decimal to BCD minute = ((minute / 10) << 4) + (minute % 10); hour = ((hour / 10) << 4) + (hour % 10); date = ((date / 10) << 4) + (date % 10); month = ((month / 10) << 4) + (month % 10); year = ((year / 10) << 4) + (year % 10); // End conversion // Write data to DS3231 RTC Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address Wire.write(0); // Send register address Wire.write(0); // Reset sesonds and start oscillator Wire.write(minute); // Write minute Wire.write(hour); // Write hour Wire.write(day); // Write day Wire.write(date); // Write date Wire.write(month); // Write month Wire.write(year); // Write year Wire.endTransmission(); // Stop transmission and release the I2C bus delay(200); // Wait 200ms } Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address Wire.write(0); // Send register address Wire.endTransmission(false); // I2C restart Wire.requestFrom(0x68, 7); // Request 7 bytes from DS3231 and release I2C bus at end of reading second = Wire.read(); // Read seconds from register 0 minute = Wire.read(); // Read minuts from register 1 hour = Wire.read(); // Read hour from register 2 day = Wire.read(); // Read day from register 3 date = Wire.read(); // Read date from register 4 month = Wire.read(); // Read month from register 5 year = Wire.read(); // Read year from register 6 Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address Wire.write(0x11); // Send register address Wire.endTransmission(false); // I2C restart Wire.requestFrom(0x68, 2); // Request 2 bytes from DS3231 and release I2C bus at end of reading temperature_msb = Wire.read(); // Read temperature MSB temperature_lsb = Wire.read(); // Read temperature LSB display_day(); DS3231_display(); // Diaplay time & calendar delay(50); // Wait 50ms } // End of code. |
Arduino with DS3231 RTC and ST7735 videos:
The first video shows a hardware circuit of the project.
and the video below shows the simulation of the circuit using Proteus:
Proteus simulation file download:
Proteus simulation file can be downloaded from the link below, use it with version 8.6 or higher.
Download
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
This code does not run on Proteus. It requires that pin 11 SDA be defined as TFT_RST… Here is what I changed.
// For the breakout, you can use any 2 or 3 pins
// These pins will also work for the 1.8″ TFT shield
#define TFT_CS 9
#define TFT_RST 11 // you can also connect this to the Arduino reset
// in which case, set this #define pin to 0!
#define TFT_DC 10
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); //NOTE THAT THIS CODE HAD TO BE REPAIRED ITS REQUIRES THAT THE TFT_RST BE DEFINED to run on newer versions of proteus
//Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC);
Good job,
I have adapted your code for another display, details: https://github.com/vlad-gheorghe/Clock-and-meteo-on-tft-7575