ESP32 +TFT_eSPI + RTC DS3231 not working properly if compiled in PlatformIO but work in Arduino IDE

Hi, I’m trying to do a title project on a ESP32 using a ILI9341 + RTC DS3231, but the result in Platform IO is not the expected.
If I put the same Sketch in arduino IDE it work as expected.
On PlatformIO using only the RTC DS3231 or the ILI9341 it work correctly, but not the 2 together on the same sketch.

This is the sketch:

    #include <TFT_eSPI.h>
    #include <SPI.h>
    #include "Free_Fonts.h"

    #include <RTClib.h> // for the RTC
    #include <Wire.h>

    TFT_eSPI tft = TFT_eSPI();
    RTC_DS3231 rtc;
    char t[34];

    char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    void setup()
    {
      Serial.begin(115200);
      Wire.begin();
      rtc.begin();

      if (rtc.lostPower())
      { // uncomment this line on the second time upload
        rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
      } // uncomment this close curly braces

      tft.init();
      tft.setRotation(1);

      tft.fillScreen(TFT_NAVY);
      tft.setTextColor(TFT_SKYBLUE, TFT_SKYBLUE);

      //tft.setTextSize(1);

      tft.drawRect(0, 20, 107, 100, TFT_SKYBLUE);
      tft.drawRect(106, 20, 108, 100, TFT_SKYBLUE);
      tft.drawRect(213, 20, 107, 100, TFT_SKYBLUE);
      tft.drawLine(0, 220, 320, 220, TFT_SKYBLUE);
    }

    void loop()
    {
      DateTime now = rtc.now();

      sprintf(t, ", %02d/%02d/%02d  and it's %02d:%02d:%02d", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
      tft.setFreeFont(FF21); 
      tft.drawString(t, 0, 1);

      Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
      Serial.println(t);
       delay(1000);
    }

Compiled result in platformIO serial monitor is:

        Wednesday, 03/82/2045  and it's 45:161:00
        Wednesday, 03/82/2045  and it's 45:161:00

Compiled result in Arduino IDE show is:

    Friday, 04/02/2022 and it's 00:55:57
    Friday, 04/02/2022 and it's 00:55:58

What I’m doing bad in PlatformIO ?
This is my Platformio.ini

    [env:esp32dev]
    monitor_speed = 115200
    platform = espressif32
    board = esp32dev
    framework = arduino
    lib_deps = TFT_eSPI
        adafruit/RTClib

Sorry if my english is not the best :blush:

Which ESP32 core version do you have installed in the Arduino IDE? (Tools → Boards → Board Manager)

Hi maxgerhardt and thanks for your reply, sorry for the delay but just got to home now.
On Arduino IDE its show:

ESP32 Dev Module
-CPU Frequency 240MHz (WiFi/BT)
-Core debug - None
-Flash Freqeuncy - 80MHz
-Flash Mode - QIO
-Flash Size - 4MB (32Mb)
-PSRAM - Disabled
-Partition Scheme - Default 4MB with spiffs...
-Ulpload Spedd - 921600

On the output windows it show:

Using board 'esp32' from platform in folder: C:\Users\Pastilhas\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6
Using core 'esp32' from platform in folder: C:\Users\Pastilhas\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6

I think Platformio is using the same as it show on the TERMINAL windows:

CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (3.5.0) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 3.10006.210326 (1.0.6)

And which version of the RTCLib is installed in the Arduino IDE? (Sketch → Library manager I think)

Arduino IDE is showing in the output windows he is using:

Using library TFT_eSPI at version 2.4.2 in folder: D:\Arduino - projects\libraries\TFT_eSPI 
Using library SPI at version 1.0 in folder: C:\Users\Pastilhas\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\SPI 
Using library FS at version 1.0 in folder: C:\Users\Pastilhas\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\FS 
Using library SPIFFS at version 1.0 in folder: C:\Users\Pastilhas\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\SPIFFS 
Using library RTClib at version 2.0.2 in folder: D:\Arduino - projects\libraries\RTClib 
Using library Adafruit_BusIO at version 1.11.0 in folder: D:\Arduino - projects\libraries\Adafruit_BusIO 
Using library Wire at version 1.0.1 in folder: C:\Users\Pastilhas\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\Wire

Checking what PlatformIO is using and they are:

Dependency Graph
|-- <TFT_eSPI> 2.4.32
|   |-- <SPIFFS> 1.0
|   |   |-- <FS> 1.0
|   |-- <FS> 1.0
|   |-- <SPI> 1.0
|-- <RTClib> 2.0.2
|   |-- <Adafruit BusIO> 1.11.0
|   |   |-- <Wire> 1.0.1
|   |   |-- <SPI> 1.0
|   |-- <Wire> 1.0.1
|-- <SPI> 1.0
|-- <Wire> 1.0.1

The only diference i see, are in the TFT_eSPI version, going to try to force the use off the older version in PlatformIO…

In platformIO i did try

<TFT_eSPI> 2.4.21 --- 0, Sunday, 00/00/2000  and it's 00:00:00
                      Wednesday, 03/82/2045  and it's 45:161:00
                      Wednesday, 03/82/2045  and it's 45:161:00
<TFT_eSPI> 2.4.22 --- Sunday, 00/00/2000  and it's 00:00:00
                      Wednesday, 03/82/2045  and it's 45:161:00
                      Wednesday, 03/82/2045  and it's 45:161:00
<TFT_eSPI> 2.4.2x --. Do the Same
<TFT_eSPI> 2.4.34 --- Sunday, 00/00/2000  and it's 00:00:00
                      Monday, 03/82/2045  and it's 45:161:00
                      Monday, 03/82/2045  and it's 45:161:00

I don’t know it helps, but on every compilation I get this yellow message:

C:\Users\Pastilhas\.platformio\packages\framework-arduinoespressif32\cores\esp32\esp32-hal-spi.c: In function 'spiTransferBytesNL':
C:\Users\Pastilhas\.platformio\packages\framework-arduinoespressif32\cores\esp32\esp32-hal-spi.c:922:39: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
                 uint8_t * last_out8 = &result[c_longs-1];
                                       ^
C:\Users\Pastilhas\.platformio\packages\framework-arduinoespressif32\cores\esp32\esp32-hal-spi.c:923:40: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
                 uint8_t * last_data8 = &last_data;
                                        ^

Can you reduce the sketch to just the RTCLib part, printing the time over and over again? Does it behave the same?

Ok, changed PlatfermIO.ini and my Sket to use only TRCLib part:

#include <Arduino.h>

//#include <TFT_eSPI.h>

#include <SPI.h>

//#include "Free_Fonts.h"

#include <RTClib.h> // for the RTC

#include <Wire.h>

//TFT_eSPI tft = TFT_eSPI();

RTC_DS3231 rtc;

char t[34];

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

void setup()

{

  Serial.begin(115200);

  Wire.begin();

  rtc.begin();

  if (rtc.lostPower())

  { // uncomment this line on the second time upload

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

  } // uncomment this close curly braces

  /*tft.init();

  tft.setRotation(1);

  tft.fillScreen(TFT_NAVY);

  tft.setTextColor(TFT_SKYBLUE, TFT_SKYBLUE);

  tft.setTextSize(1);

  tft.drawRect(0, 20, 107, 100, TFT_SKYBLUE);

  tft.drawRect(106, 20, 108, 100, TFT_SKYBLUE);

  tft.drawRect(213, 20, 107, 100, TFT_SKYBLUE);

  tft.drawLine(0, 220, 320, 220, TFT_SKYBLUE);

  */

}

void loop()

{

  DateTime now = rtc.now();

  sprintf(t, ", %02d/%02d/%02d  and it's %02d:%02d:%02d", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());

  //tft.setFreeFont(FF21);

  //tft.drawString(t, 0, 1);

  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);

  Serial.println(t);

   delay(1000);

}

PlatformIO Serial Monitor is:

Saturday, 05/02/2022  and it's 15:19:45
Saturday, 05/02/2022  and it's 15:19:46
Saturday, 05/02/2022  and it's 15:19:47
Saturday, 05/02/2022  and it's 15:19:48
Saturday, 05/02/2022  and it's 15:19:49
Saturday, 05/02/2022  and it's 15:19:50
Saturday, 05/02/2022  and it's 15:19:51
Saturday, 05/02/2022  and it's 15:19:52
Saturday, 05/02/2022  and it's 15:19:53
Saturday, 05/02/2022  and it's 15:19:54
Saturday, 05/02/2022  and it's 15:19:55
Saturday, 05/02/2022  and it's 15:19:56
Saturday, 05/02/2022  and it's 15:19:57
Saturday, 05/02/2022  and it's 15:19:58
Saturday, 05/02/2022  and it's 15:19:59
Saturday, 05/02/2022  and it's 15:20:00
Saturday, 05/02/2022  and it's 15:20:01
Saturday, 05/02/2022  and it's 15:20:02
Saturday, 05/02/2022  and it's 15:20:03
Saturday, 05/02/2022  and it's 15:20:04
Saturday, 05/02/2022  and it's 15:20:05
Saturday, 05/02/2022  and it's 15:20:06
...
Saturday, 05/02/2022  and it's 15:23:21
Saturday, 05/02/2022  and it's 15:23:22
Saturday, 05/02/2022  and it's 15:23:23
...
Saturday, 05/02/2022  and it's 15:24:59
Saturday, 05/02/2022  and it's 15:25:00

Yes it’s working as intended as I pointed in my first Post
If I only use the TFT_eSPI sketch part, it works as indented to.
But not the two together in PlatformIO.

Hm, not sure what’s going on there. Please file an issue in Issues · platformio/platform-espressif32 · GitHub.

Ok tanks, I’m going to do that, but tank for your time.

I am new to all of the platformio stuff and first time on forum. So bare with me if I am out of place.
But I just finished a ESP32 Unleashed course and it uses:
Platformio, ESP32, TFT_eSPI, DS3231, BME280, EEprom and SD card.
TaskScheduler, ezTIme libs.
Being a neophyte I pulled out most of my hair learning how to do all this but in the end it works well.

What I see different is:
You use <RTDlib.h> I use <RtcDS3231.h>
I’m not including <SPI.h>

In platform.ini
you use RTClib I use RTC
We both us TFT_eSPI
I also have SPI in this file.

I hope this may help.
I am available if you need help getting it to work.

Hi Joe T.
Thanks for your time to reply.
LOL my project are going to use the same for Temp, Humidity and Lather going to implement CO2 (not EEprom and SD) to control all in a mushroom room.
I already did one for arduino, DS3231, BME280 and SSD1306. But SSD1306 have a tiny screen, hard to show all. I´m happy whit my work on that project.
So time to move to a bigger screen, but was not easy so far, I’m a little hold, English is not my best and I’m close to 0 knowledge in C++ Language. I’m trying to learn from examples, and applying them to my needs.

So I’m going to use <RtcDS3231.h> and removing <SPI.h>.

Strange, if I’m not mistaken, you only put on platform.ini the Library you use. So I think you can remove
SPI from it.

Tanks one more time from your tips.

Here is my .ini
just wanted to see if this is how you post code to forum

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 115200

[common]
lib_deps =
;use installed
  ${common.lib_deps}
  SPI
  Wire
  FS
  SD(esp32)
  Adafruit Unified Sensor
  Adafruit BME280 Library
  TFT_eSPI
  TaskScheduler
  Adafruit IO Arduino
  ezTime
  EEPROM
  RTC

Hi again, I did try using the <RtcDS3231.h> and removed the <SPI.h>m made de changes on platformio.ini, but still not working.
The TFT part work as expected but the DS3231 is not working, if I remove the TFT part and just let the DS3231 part, it works correctly.

RTC communications error = 2
01/01/2000 00:00:00Saturday
0.00C

I see you are using a NodeMCU-32S ESP32, mine is different, it’s the one sold on this Amazon Page:

I’m starting to think this module don’t work if I use IC2 + SPI bus same time.