Real Time Clock with NodeMCU, ST7789 TFT and DS1307 RTC

This tutorial shows how to build a simple real time clock & calendar (RTCC) using the ESP8266 NodeMCU board and DS1307 RTC integrated circuit where time and date are printed on ST7789 TFT display module.
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 does not support 5V.

TFT: Thin-Film Transistor.
SPI: Serial Peripheral Interface.
IPS: In-Plane Switching.
IIC (or I2C): Inter-Integrated Circuit.

To see how to interface the NodeMCU board with ST7789 TFT display, visit this post:
Interfacing ESP8266 NodeMCU with ST7789 TFT Display

Hardware Required:

  • ESP8266 NodeMCU board (LoLin)
  • ST7789 TFT display module (1.3″, 1.54″ …)
  • DS1307 RTC              —->    datasheet
  • 32.768 kHz crystal oscillator
  • 4 x 4.7k Ohm resistor
  • 2 x push button
  • 3V coin cell battery
  • Micro USB cable (for programming and powering the whole circuit)
  • Breadboard
  • Jumper wires

NodeMCU with ST7789 TFT and DS1307 circuit:
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).

ESP8266 NodeMCU DS1307 RTC ST7789 SPI TFT

The ST7789 TFT display module is connected to the NodeMCU board as follows:
GND is connected to pin GND of the NodeMCU board,
VCC and BL are connected to pin 3V3,
SCL pin is connected to D5 (ESP8266EX GPIO14),
SDA pin is connected to D7 (ESP8266EX GPIO13),
RES pin is connected to D8 (ESP8266EX GPIO15),
DC pin is connected to D4 (ESP8266EX GPIO2).

If the display module has a CS pin (Chip Select) then it should be connected directly to NodeMCU GND pin.

Pins D5 (GPIO14) and D7 (GPIO13) are hardware SPI module pins of the ESP8266EX microcontroller respectively for SCK (serial clock) and MOSI (master-out slave-in).

The DS1307 SCL (serial clock) and SDA (serial data) pins are respectively connected to NodeMCU pins D2 (GPIO4) and D3 (GPIO0).
The DS1307 power input pin VCC is connected to NodeMCU VU pin (5V output).
The DS1307 operating voltage range is 4.5V – 5.5V and it may not work with 3.3V. The good thing is its high logic input starts from 2.2V.

A coin cell battery of 3 Volts is connected between VBAT and GND, it’s used to keep time running even if the main power source is cut. If the battery purpose is not needed then VBAT pin should be connected to GND.

The two push buttons which are connected to NodeMCU pins D0 (GPIO16) and D1 (GPIO5) are for setting time & date.

NodeMCU with ST7789 TFT and DS1307 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 install the one from Adafruit).

The 3 libraries can be installed manually, first download them from the following 2 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 previous 3 libraries (& Wire library) are included in the main code as follows:

The connection of ST7789 TFT display with the NodeMCU is as shown below where the display is connected to hardware SPI module of the NodeMCU (pins: SCK and MOSI):

And the TFT display is initialized using the following command:

The two push buttons are defined in the code as:

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:

The following image shows my hardware circuit:

NodeMCU with ST7789 TFT display and DS1307 RTC

Related Project:
ESP8266 NodeMCU with ST7789 TFT and DS3231 RTC


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

1 thought on “Real Time Clock with NodeMCU, ST7789 TFT and DS1307 RTC”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top