DS3231 RTC not working together with ESP32

Hi,
maybe my problem is a trivial one but I'm still a newbie and, even after extensive googling, I was not able to find a solution.
Since i need to collect data on a SD card from a temp/hum sensor every ten minutes in an area where there is no available wifi i tried to use a DS3231RTC to timestamp the sensor readings while in the intervals the ESP goes to deepsleep and awakes at a fixed interval.
I was able to achieve this result by using an ESP 8266 but when I tried to use an ESP32 I found that the ESP was continuously rebooting even if the code was compiled without any problem.
So I tried to identify the problem by removing the SD card but it was still present and it disappeared ONLY when i removed the DS3231RTC.
Moreover, the serial monitor just displays garbage characters and even if I modify the baud rate I'm not able to obtain any usefui info on what's going on.
The code I'm using is the following


#include <SPI.h> //for the SD card 
#include <Wire.h>

#include <SD.h> // for the SD card
const int chipSelect = 15;

// Create a file to store the data
File myFile;

#include <RTClib.h> // for the RTC
RTC_DS3231 rtc; 

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_ADDR   0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 1  // Pin 8
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#include <SHT3x.h>
SHT3x Sensor; //This operates in Celsius

float temp;
float hum;

uint64_t uS_TO_S_FACTOR = 1000000; 
uint64_t TIME_TO_SLEEP = 600;


void setup() {

  //initializing Serial monitor
  Serial.begin(115200);
    while(!Serial);
  delay(1000);
    
 Sensor.Begin();

 Sensor.UpdateData();
  
   // setup for the RTC
  while(!Serial); 
  delay(1000);
 if(! rtc.begin()) {
      Serial.println("Couldn't find RTC");
      while (1);
    }
    else {
      // following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }
  
  temp = Sensor.GetTemperature();
  hum = Sensor.GetRelHumidity();

  // setup for the SD card
  Serial.print("Init SD");

  if(!SD.begin(chipSelect)) {
    Serial.println("Failed");
    return;
  }
    
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

 
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();  //Pulisce il buffer da inviare al display
  display.setTextSize(1);  
  display.setTextColor(WHITE);
  display.ssd1306_command(SSD1306_DISPLAYON); 

  loggingTime();
  loggingTemperature();
  displayReadings();
  SD_writing();

  delay(5000);
   display.ssd1306_command(SSD1306_DISPLAYOFF);

   Serial.println("Sensor data logged successfully! Going to sleep");
 esp_deep_sleep_start();
    
}

void loggingTime() {
  DateTime now = rtc.now();
char dateString[11];
  sprintf(dateString, "%04i-%02i-%02i  ", now.year(), now.month(), (now.day()));
  
char timeString[9];
  sprintf(timeString, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
}

 void loggingTemperature() {

Serial.print("Temperature_SHT31 = ");
  Serial.print(Sensor.GetTemperature());
  Serial.println(" *C");
 Serial.print("Humidity_SHT31 = ");
  Serial.print(Sensor.GetRelHumidity());
  Serial.println(" %");
    }
 
void SD_writing() {
      DateTime now = rtc.now();
char dateString[11];
  sprintf(dateString, "%04i-%02i-%02i  ", now.year(), now.month(), (now.day()));
  
char timeString[9];
  sprintf(timeString, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
 myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(timeString);
    myFile.print(",");

    myFile.print(temp);
    myFile.print(",");

   myFile.print(hum);
    myFile.println(","); 

  myFile.close();
} 

}
void displayReadings() {
   DateTime now = rtc.now();
char dateString[11];
  sprintf(dateString, "%04i-%02i-%02i  ", now.year(), now.month(), (now.day()));
  
char timeString[9];
  sprintf(timeString, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());
  
display.clearDisplay();
display.setCursor(0,0); 
//display.println(dateString);
display.setCursor(0,24); 
display.print("T_SHT31   ");
display.print(Sensor.GetTemperature());
display.println(" C"); 
display.print("Hum_SHT31  ");
display.print(Sensor.GetRelHumidity());
display.println(" %");
display.setCursor(0,48);
display.print("going to sleep for 1 min");
display.display(); //Invia il buffer da visualizzare al display
}

void loop() {
}
  



Thx in advance for your help

Hi,
I realized that there is a mistake in the code I sent you the line
//display.println(dateString);
appears to be commented but in the code IT is UNCOMMENTED and supposed to work.
Sorry for the confusion.
Bruno

Post an annotated schematic showing exactly how you have wired it. Links to technical information on the hardware devices will also help.

1 Like

With the space at the end, I think you are writing past the end of the dateString character array. The timeString code indicates that you know to make room for the null terminator.

Hi,
really thx for your help.
I hadn't realized the mistake I'd made with the dateString array.
However, even if I comment the line containing the mistake and I just leave the line where there is timeString the ESP32 goes on rebooting.
I 'm enclosing what serial monitor displays when the code is running in the hope it can help to clarify the issue
I'm really perplexed.

Hi,
since I'm not able to use a software to draw electronic circuit I depicte dthe circuit by using PPT instruments.
Hope it can works.
For what attains the technical details of the hardware devices there are

  1. an ESP32 clone that till now has been working with different codes without any problem ( except those caused by my coding/wiring mistakes)
  2. a DS3231 connected through I2C
  3. a SHT3X sencor connected through I2C
  4. a 0.96" OLED 1306SSD display connected through I2C
  5. a MH-SD card module connected through SPI (MOSI to 23, MISO to 19, SCK to 18, CS to 15)
    The same hardware works fine when I use an ESP 8266; the problem seems related to the code managing the DS3231 but, since I am a newbie, I really don't know how to solve this problem.
    One more info/question : why, if I check the I2c addresses with I2C scanner I find four devices - at 0x3C 0x44 0x57 and 0x68 addresses - when I've just connected three devices ( the sensor, the RTC and the OLED) ?
    Is the fourth address the SD card module ?

    but how is it possible if it's connected through SPI and not I2C ?
    May be is a silly question ...

Look for KiCad, it is a great CAD package and it is free. It will run on most platforms. You state you connected the DS3231, that responds to only the selected I2C address. In your picture you show the RTC module, that also has onboard a EEPROM chip hence the second address.

Do not worry you and thousands of others have come to the same conclusion. Yes I learned it the same way.

Can you successfully run a stand alone library example program for the RTC?

1 Like

Hi,
thx for your suggestion about KiCad that I'll try for sure and thx for your clear explanation of the mysterious "fourth" I2C address.
By the way, I changed ESP32 and the continuous rebooting just disappeared; the display shows at the correct time the data from sensor AND the time before going to sleep.
Now i'm working to understand why the SD library I used without problems with ESP8266 doesn't seem to work with the same code when applied to ESP32 because the SDcard remains empty of data: I think that again the problem is in my coding.
Have a nice evening
Bruno

This is what a ESP-32 or ESP8266’s reset output looks like.
Were you pressing the reset button multiple times?

Hi,
I apparently solved the problem by changing the ESP32 with another identical.
Now I'm working to make SD card work with ESP32 because the code that worked with ESP8266 seems not to work with ESP32 ( SD card remains empty even if the data are correctly displayed at the fixed intervals).
Thx again for your help and the time you spent for me.
Have a nice evening
Bruno

Hi,
No I didn't touch the reset button but, as I said, changing the "strange" ESP32 with another apparently identical the problem just disappeared.
Best
Bruno

1 Like

Sounds like a hardware problem.
Faulty board?

Hint: Not all libraries work with all processors in the Arduino IDE. Several have completely different instruction sets and hardware. Many people over the years have put in a lot of effort and time and it shows. It is amazing on how many different processors the IDE will support and from the user's the instructions remain the same (Big advantage of a high level language such as C++ and its implementation. The simple fact millions of people from all around the globe are using the tools and they are working is a big checkered flag for all those that contributed to the Arduino system. And yes the bugs are eventually fixed.

1 Like

Hi,
I verified again all my four ESP32 with the same hardware and, after a careful debugging of the code, finally they work fine.
However your hypothesis was the first one I thought of.
Thx and have a nice day
Bruno

Hi,
sorry for the very lomg delay in aswering your post but a "simple" programmed hospital check turned out to be more complicated than foreseen and in the last days I feared to be in trouble greater than with my microprocessor problems... However, now it's fixed
I couldn't agree any more with your comment on the libraries and how they solve a lot of problems for a newbie as I am and indeed, even in this case, the problem that caused a malfunction was in my code ( as it's usual) and not in library.
Thx again
Best
Bruno

1 Like

Hopefully you got two things out of this, you had fun and learned a lot. Thanks for the feedback.

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