This tutorial shows how to make a digital real time clock using Arduino UNO and DS3231 RTC module where time & date can be set with two push buttons connected to the Arduino board and they are printed (with RTC chip temperature) on ST7789 TFT display.
The ST7789 TFT module contains a display controller with the same name: ST7789. It’s a color display that uses SPI interface protocol and requires 3, 4 or 5 control pins, it’s low cost and easy to use. This display is an IPS display, it comes in different sizes (1.3″, 1.54″ …) but all of them should have the same resolution of 240×240 pixel, this means it has 57600 pixels. This module works with 3.3V only and it doesn’t support 5V (not 5V tolerant).
TFT: Thin-Film Transistor.
SPI: Serial Peripheral Interface.
IPS: In-Plane Switching.
IIC (or I2C): Inter-Integrated Circuit.
To see how to interface Arduino with ST7789 TFT display, visit this post:
Interfacing Arduino with ST7789 TFT Display – Graphics Test Example
Hardware Required:
- Arduino board
- ST7789 TFT display module (1.3″, 1.54″ …)
- DS3231 module —-> DS3231 datasheet
- 4 x 3.3k ohm resistor (+1 if the display module has CS pin)
- 4 x 2.2k ohm resistor (+1 if the display module has CS pin)
- 2 x push button
- 3V coin cell battery
- Breadboard
- Jumper wires
Interfacing Arduino with DS3231 RTC and ST7789 TFT:
Project circuit schematic diagram is shown below.
The ST7789 display module shown in project circuit diagram has 7 pins: (from right to left): GND (ground), VCC, SCL (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and BLK (back light).
Connecting the BLK pin is optional. The back light turns off when the BLK pin connected to the ground (GND).
As mentioned above, the ST7789 TFT display controller works with 3.3V only (power supply and control lines). The display module is supplied with 3.3V (between VCC and GND) which comes from the Arduino board.
All Arduino UNO board output pins are 5V, connecting a 5V pin to the ST7789 TFT display may damage its controller.
To connect the Arduino to the display module, I used voltage divider for each line which means there are 4 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.
If the display module has a CS pin (Chip Select) then it should be connected to Arduino digital pin 10 through another voltage divider.
So, the ST7789 TFT display is connected to the Arduino board as follows (each one through voltage divider):
RST pin is connected to Arduino digital pin 8,
DC pin is connected to Arduino digital pin 9,
SDA pin is connected to Arduino digital pin 11,
SCL pin is connected to Arduino digital pin 13.
Other pins are connected as follows:
VCC pin is connected to Arduino 3V3 pin,
GND pin is connected to Arduino GND pin,
BL (LED) pin is connected to Arduino 3V3 pin (optional).
The DS3231 RTC module SDA (serial data) and SCL (serial clock) pins are respectively connected to Arduino A4 and A5 pins (ATmega328P hardware I2C module pins).
The DS3231 RTC chip is supplied with 5V from the Arduino board.
The two push buttons which are connected to Arduino digital pins 6 and 7 are for setting time & date of the clock.
Arduino with DS3231 RTC and ST7789 TFT code:
The following Arduino code requires 3 libraries from Adafruit Industries:
The first library is a driver for the ST7789 TFT display which can be installed from Arduino IDE library manager (Sketch —> Include Library —> Manage Libraries …, in the search box write “st7789” and install the one from Adafruit).
The second library is Adafruit graphics library which can be installed also from Arduino IDE library manager.
The 3rd library is for the DS1307 RTC, it may be installed using library manager (in the search box write “rtclib” and choose the one from Adafruit).
The 3 libraries can be installed manually, first download them from the following 3 links:
Adafruit ST7789 TFT library —-> direct link
Adafruit graphics library —-> direct link
Adafruit RTC library —-> direct link
After the download, go to Arduino IDE —> Sketch —> Include Library —> Add .ZIP Library … and browse for the .zip file (previously downloaded).
The same thing for other library files.
Hints:
The 3 libraries are included ( & Arduino Wire library) in the main code as follows:
1 2 3 4 | #include <Wire.h> // include Arduino Wire library (required for I2C devices) #include <Adafruit_GFX.h> // Adafruit core graphics library #include <Adafruit_ST7789.h> // Adafruit hardware-specific library for ST7789 #include "RTClib.h" // Adafruit RTC library |
The ST7789 TFT module pins (CS, RST and DC) connections are defined as shown below (even the display module has no CS pin but its definition is required by the Adafruit ST7789 library):
1 2 3 4 | // ST7789 TFT module connections #define TFT_CS 10 // define chip select pin #define TFT_DC 9 // define data/command pin #define TFT_RST 8 // define reset pin, or set to -1 and connect to Arduino RESET pin |
The other display pins (SDA and SCL) are connected to Arduino hardware SPI module pins (digital pin 11 and digital pin 13 respectively for MOSI and SCLK).
The Adafruit ST7789 library is initialized with this line:
1 2 | // Initialize Adafruit ST7789 TFT library Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); |
And the TFT display is initialized using the following command:
1 2 | // if the display has CS pin try with SPI_MODE0 tft.init(240, 240, SPI_MODE2); // init ST7789 display 240x240 pixel |
The two push buttons are defined in the code as:
1 2 3 | // button definitions #define button1 7 // button B1 is connected to Arduino pin 7 #define button2 6 // button B2 is connected to Arduino pin 6 |
Functions used in the code:
bool debounce (): this function is for button B1 debounce, returns 1 if button is debounced.
void RTC_display(): displays day of the week, date and time on the display.
byte edit(byte parameter): this function is for setting the real time clock, returns the edited parameter.
Rest of code is described through comments.
Full Arduino code:
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 205 206 207 208 209 210 211 212 213 214 215 216 217 | /******************************************************************************** * * Arduino real time clock with chip temperature monitor using ST7789 TFT * display (240x240 pixel) and DS3231 RTC. * This is a free software with NO WARRANTY. * http://simple-circuit.com/ * ********************************************************************************/ #include <Wire.h> // include Arduino Wire library (required for I2C devices) #include <Adafruit_GFX.h> // Adafruit core graphics library #include <Adafruit_ST7789.h> // Adafruit hardware-specific library for ST7789 #include "RTClib.h" // Adafruit RTC library // ST7789 TFT module connections #define TFT_CS 10 // define chip select pin #define TFT_DC 9 // define data/command pin #define TFT_RST 8 // define reset pin, or set to -1 and connect to Arduino RESET pin // Initialize Adafruit ST7789 TFT library Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); // initialize RTC library RTC_DS3231 rtc; DateTime now; // buttons definition #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) { // if the display has CS pin try with SPI_MODE0 tft.init(240, 240, SPI_MODE2); // init ST7789 display 240x240 pixel // if the screen is flipped, remove this command tft.setRotation(2); // fill the screen with black color tft.fillScreen(ST77XX_BLACK); tft.fillRect(0, 32, tft.width(), 2, ST77XX_BLUE); tft.fillRect(0, 111, tft.width(), 2, ST77XX_BLUE); tft.fillRect(0, 190, tft.width(), 2, ST77XX_BLUE); tft.setTextWrap(false); // turn off text wrap option tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK); // set text color to white and black background tft.setTextSize(2); // text size = 2 tft.setCursor(0, 0); // move cursor to position (0, 10) pixel tft.print("ARDUINO + ST7789 TFT"); tft.setCursor(48, 16); // move cursor to position (48, 34) pixel tft.print("+ DS3231 RTC"); tft.setCursor(21, 194); // move cursor to position (28, 27) pixel tft.print("CHIP TEMPERATURE:"); tft.setTextSize(4); // text size = 4 tft.setTextColor(ST77XX_MAGENTA, ST77XX_BLACK); tft.setCursor(64, 120); tft.print("TIME:"); pinMode(button1, INPUT_PULLUP); pinMode(button2, INPUT_PULLUP); rtc.begin(); // initialize RTC chip } // a small function for button1 (B1) debounce bool debounce () { byte count = 0; for(byte i = 0; i < 5; i++) { if ( !digitalRead(button1) ) count++; delay(10); } if(count > 2) return 1; else return 0; } void RTC_display() { char _buffer[11]; char dow_matrix[7][10] = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"}; byte x_pos[7] = {50, 50, 38, 14, 26, 50, 26}; static byte previous_dow = 8; // print day of the week if( previous_dow != now.dayOfTheWeek() ) { previous_dow = now.dayOfTheWeek(); tft.fillRect(14, 41, 216, 28, ST77XX_BLACK); // draw rectangle (erase day from the display) tft.setCursor(x_pos[previous_dow], 41); tft.setTextColor(ST77XX_CYAN, ST77XX_BLACK); // set text color to cyan and black background tft.print( dow_matrix[now.dayOfTheWeek()] ); } // print date sprintf( _buffer, "%02u-%02u-%04u", now.day()%100, now.month()%100, now.year() ); tft.setCursor(1, 76); tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK); // set text color to yellow and black background tft.print(_buffer); // print time sprintf( _buffer, "%02u:%02u:%02u", now.hour()%100, now.minute()%100, now.second()%100 ); tft.setCursor(26, 155); tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK); // set text color to green and black background tft.print(_buffer); } byte edit(byte parameter) { static byte i = 0, y_pos, x_pos[5] = {1, 73, 193, 26, 98}; char text[3]; sprintf(text,"%02u", parameter); if(i < 3) { tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK); // set text color to yellow and black background y_pos = 76; } else { tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK); // set text color to green and black background y_pos = 155; } while( debounce() ); // call debounce function (wait for B1 to be released) while(true) { while( !digitalRead(button2) ) { // while B2 is pressed parameter++; if(i == 0 && parameter > 31) // if day > 31 ==> day = 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); tft.setCursor(x_pos[i], y_pos); tft.print(text); delay(200); // wait 200ms } tft.fillRect(x_pos[i], y_pos, 44, 28, ST77XX_BLACK); unsigned long previous_m = millis(); while( (millis() - previous_m < 250) && digitalRead(button1) && digitalRead(button2)) ; tft.setCursor(x_pos[i], y_pos); tft.print(text); previous_m = millis(); while( (millis() - previous_m < 250) && digitalRead(button1) && digitalRead(button2)) ; if(!digitalRead(button1)) { // if button B1 is pressed i = (i + 1) % 5; // increment 'i' for the next parameter return parameter; // return parameter value and exit } } } // main loop void loop() { if( !digitalRead(button1) ) // if B1 is pressed if( debounce() ) // call debounce function (make sure B1 is pressed) { while( debounce() ); // call debounce function (wait for B1 to be released) byte day = edit( now.day() ); // edit date byte month = edit( now.month() ); // edit month byte year = edit( now.year() - 2000 ); // edit year byte hour = edit( now.hour() ); // edit hours byte minute = edit( now.minute() ); // edit minutes // write time & date data to the RTC chip rtc.adjust(DateTime(2000 + year, month, day, hour, minute, 0)); while(debounce()); // call debounce function (wait for button B1 to be released) } now = rtc.now(); // read current time and date from the RTC chip RTC_display(); // diaplay time & calendar // read chip temperature Wire.beginTransmission(0x68); // start I2C protocol with DS3231 address Wire.write(0x11); // send register address (temperature MSB) Wire.endTransmission(false); // I2C restart Wire.requestFrom(0x68, 2); // request 2 bytes from DS3231 and release I2C bus at end of reading byte t_msb = Wire.read(); // read temperature MSB byte t_lsb = Wire.read(); // read temperature LSB // print chip temperature char _buffer[6]; uint16_t chip_temp = (uint16_t)t_msb << 2 | t_lsb >> 6; if(t_msb & 0x80) { chip_temp |= 0xFC00; sprintf(_buffer, "-%02u.%02u", abs((int)chip_temp * 25) / 100, abs((int)chip_temp * 25) % 100); } else sprintf(_buffer, " %02u.%02u", (chip_temp * 25) / 100, (chip_temp * 25) % 100); tft.setCursor(14, 212); tft.setTextColor(ST77XX_RED, ST77XX_BLACK); // set text color to red and black background tft.print(_buffer); tft.drawCircle(173, 218, 4, ST77XX_RED); // print degree symbol ( ° ) tft.drawCircle(173, 218, 5, ST77XX_RED); tft.setCursor(184, 212); tft.print("C"); delay(100); // wait 100ms } // end of code. |
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
Hello,
I would like to display the temperature with a single digit after the decimal point but I don’t know how to modify the sketch.
Thanks if you can help me.
Best regards.
Everything works fine except it won’t read the chip temperature. Can you suggest a modification
Thank you for your hard work I would like to make this clock but can I use an Arduino Nano and not the UNO as I want to make a small case to mount it in.
Thanks for any help
pretty sure you can just copy for nano