ESP32 + ILI9341 touch display + DS3231 = touch + time problems

Hello everybody,

My project is to build a clock on a touch display : set alarm, hour and date through touch display.
Time is given by a RTC module : DS3231

My display is a 2.8 inch ILI9341 240×320

To connect the touch display, I have followed a tutorial here : ESP32: TFT Touchscreen - 2.8 inch ILI9341 (Arduino) | Random Nerd Tutorials

So i have pins connections as followed

TFT LCD 	Touchscreen	ESP32
T_IRQ		GPIO 36
T_OUT		GPIO 39
T_DIN		GPIO 32
T_CS		GPIO 33
T_CLK		GPIO 25
SDO(MISO)	GPIO 12
LED			3.3V
SCK			GPIO 14
SDI(MOSI)	GPIO 13
D/C			GPIO 2
RESET		EN/RESET
CS			GPIO 15
GND			GND
VCC			3.3V

And for the DS3231 :

DS3231	 	ESP32
GND			GND
VCC			3.3V
SDA			GPIO 22
SCL			GPIO 21

Here is the code :

/*  Rui Santos & Sara Santos - Random Nerd Tutorials
    THIS EXAMPLE WAS TESTED WITH THE FOLLOWING HARDWARE:
    1) ESP32-2432S028R 2.8 inch 240×320 also known as the Cheap Yellow Display (CYD): https://makeradvisor.com/tools/cyd-cheap-yellow-display-esp32-2432s028r/
      SET UP INSTRUCTIONS: https://RandomNerdTutorials.com/cyd/
    2) REGULAR ESP32 Dev Board + 2.8 inch 240x320 TFT Display: https://makeradvisor.com/tools/2-8-inch-ili9341-tft-240x320/ and https://makeradvisor.com/tools/esp32-dev-board-wi-fi-bluetooth/
      SET UP INSTRUCTIONS: https://RandomNerdTutorials.com/esp32-tft/
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/

#include <SPI.h>

/*  Install the "TFT_eSPI" library by Bodmer to interface with the TFT Display - https://github.com/Bodmer/TFT_eSPI
    *** IMPORTANT: User_Setup.h available on the internet will probably NOT work with the examples available at Random Nerd Tutorials ***
    *** YOU MUST USE THE User_Setup.h FILE PROVIDED IN THE LINK BELOW IN ORDER TO USE THE EXAMPLES FROM RANDOM NERD TUTORIALS ***
    FULL INSTRUCTIONS AVAILABLE ON HOW CONFIGURE THE LIBRARY: https://RandomNerdTutorials.com/cyd/ or https://RandomNerdTutorials.com/esp32-tft/   */
#include <TFT_eSPI.h>

// Install the "XPT2046_Touchscreen" library by Paul Stoffregen to use the Touchscreen - https://github.com/PaulStoffregen/XPT2046_Touchscreen
// Note: this library doesn't require further configuration
#include <XPT2046_Touchscreen.h>

TFT_eSPI tft = TFT_eSPI();

// Touchscreen pins
#define XPT2046_IRQ 36   // T_IRQ
#define XPT2046_MOSI 32  // T_DIN
#define XPT2046_MISO 39  // T_OUT
#define XPT2046_CLK 25   // T_CLK
#define XPT2046_CS 33    // T_CS

SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define FONT_SIZE 2

// Touchscreen coordinates: (x, y) and pressure (z)
int x, y, z;

// Print Touchscreen info about X, Y and Pressure (Z) on the Serial Monitor
void printTouchToSerial(int touchX, int touchY, int touchZ) {
  Serial.print("X = ");
  Serial.print(touchX);
  Serial.print(" | Y = ");
  Serial.print(touchY);
  Serial.print(" | Pressure = ");
  Serial.print(touchZ);
  Serial.println();
}

// Print Touchscreen info about X, Y and Pressure (Z) on the TFT Display
void printTouchToDisplay(int touchX, int touchY, int touchZ) {
  // Clear TFT screen
  tft.fillScreen(TFT_WHITE);
  tft.setTextColor(TFT_BLACK, TFT_WHITE);

  int centerX = SCREEN_WIDTH / 2;
  int textY = 80;
 
  String tempText = "X = " + String(touchX);
  tft.drawCentreString(tempText, centerX, textY, FONT_SIZE);

  textY += 20;
  tempText = "Y = " + String(touchY);
  tft.drawCentreString(tempText, centerX, textY, FONT_SIZE);

  textY += 20;
  tempText = "Pressure = " + String(touchZ);
  tft.drawCentreString(tempText, centerX, textY, FONT_SIZE);
}


// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"

RTC_DS3231 RTC;

char Week_days[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

//RTC pins
#define RTC_DS3231_SDA 21
#define RTC_DS3231_SCL 22
 


void setup() {
  Serial.begin(115200);

  if (!RTC.begin()) {
    Serial.println("Couldn't find RTC");
    while(1);
  }


  RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));


  // Start the SPI for the touchscreen and init the touchscreen
  touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
  touchscreen.begin(touchscreenSPI);
  // Set the Touchscreen rotation in landscape mode
  // Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 3: touchscreen.setRotation(3);
  touchscreen.setRotation(3);

  //pinMode(XPT2046_CS, OUTPUT);

  // Start the tft display
  tft.init();
  // Set the TFT display rotation in landscape mode
  tft.setRotation(3);

  // Clear the screen before writing to it
  tft.fillScreen(TFT_WHITE);
  tft.setTextColor(TFT_BLACK, TFT_WHITE);
  
  // Set X and Y coordinates for center of display
  int centerX = SCREEN_WIDTH / 2;
  int centerY = SCREEN_HEIGHT / 2;

  tft.drawCentreString("Hello, world!", centerX, 30, FONT_SIZE);
  tft.drawCentreString("Touch screen to test", centerX, centerY, FONT_SIZE);

}

void loop() {
  // Checks if Touchscreen was touched, and prints X, Y and Pressure (Z) info on the TFT display and Serial Monitor
  if (touchscreen.tirqTouched() && touchscreen.touched()) {
    // Get Touchscreen points
    TS_Point p = touchscreen.getPoint();
    // Calibrate Touchscreen points with map function to the correct width and height
    x = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
    y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
    z = p.z;

    printTouchToSerial(x, y, z);
    printTouchToDisplay(x, y, z);

    delay(100);
  }

  DateTime now = RTC.now();

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(Week_days[now.dayOfTheWeek()]);
  Serial.print(") ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  delay(1000);

}

Result is

  1. if they are connected separately, both of the display or the RTC module work fine
  2. if connected simultaneously, it do not work fine:
  • display is ok but not as reactive
  • time is not correct : i have this info in the serial monitor :
    10:00:19.730 -> 2000/0/7 (Friday) 45:162:0

Is anybody have the same problem ?

I guess this maybe due to Spi, the touch display is using Vspi but the rtc module is connected via i2c. Am i wrong here ?

Thanks a lot in advance for your help

you are using delay() which stops program execution for the specified time
try replacing with millis()
have a look at how-to-avoid-using-the-delay-function-in-arduino-sketches

Thanks @horace for your reply
On my first attempt i even forgot to use delay() and i had the same problems

i will try to use millis anyway and update here

Why? ESP32 has RTC built in. (Unless there is no WiFi/internet access where you will use the clock.)

T_DIN		GPIO 32
SDI(MOSI)	GPIO 13
...
T_CLK		GPIO 25
SCK			GPIO 14

Why are these pairs of pins not shared? Do you know that SPI is a bus?

Are any of these pins the SPI bus pins of ESP32? Using hardware SPI will dramatically improve the update speed of the LCD.

Thanks @PaulRB for your tips

Internal RTC of esp32 is not so accurate according what i read
I can't use wifi

I know that Spi is a bus, thank you
And i read that sharing some pins while using touch screen would not work.

I am trying to have the best options

i will also try what you said

The internal RTC is not so accurate if you can't use WiFi. If you can use WiFi, it is more accurate than DS3231. Plus you don't have to set it or adjust it for DST.

If you can't use WiFi, why choose ESP32? I guess they are cheap...

I agree with you and i knew about wifi + time sync

ESP32 was my first option and i was trynig to make it work

but as you suggest, i'd better choose an arduino uno or micro

also not possible to support the internal RTC when power fails
in a recent project had to have an external RTC with battery backup

1 Like

absolutely right @horace :+1:

1 Like

Fair enough, then. Most forum visitors have no idea about such things, and you didn't explain why you weren't doing them, so you can understand why I asked those questions.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.