Naguissa / uRTCLib

Really tiny library to basic RTC functionality on Arduino. DS1307, DS3231 and DS3232 RTCs are supported.
https://www.foroelectro.net/librerias-arduino-ide-f29/rtclib-arduino-libreria-simple-y-eficaz-para-rtc-y-t95.html
GNU Lesser General Public License v3.0
88 stars 24 forks source link

Sketch stuck at rtc.enableBattery() #27

Closed zekageri closed 8 months ago

zekageri commented 2 years ago

I'm using an ESP32-wrover-E ( 16mb flash 8mb psram )

After initializing the i2c and setting the address my sketch got stuck at rtc.enableBattery() and it never returns from that.

RTC is ds3231, using PlatformIO to code and latest from everything.

#include "Arduino.h"
#include "uRTCLib.h"

uRTCLib rtc;

#define DS3231_ADDR (0xD0 >> 1)
int rtcSDA_Pin  = 32;
int rtcSCL_Pin  = 33;

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

    Serial.println("0");
    if( !URTCLIB_WIRE.begin(rtcSDA_Pin, rtcSCL_Pin) ){
        Serial.println("WIRE BEGIN FAILED");
    }
    Serial.println("1");
    rtc.set_rtc_address(DS3231_ADDR);
    Serial.println("2");
    rtc.set_model(URTCLIB_MODEL_DS3231);
    Serial.println("3");
    if (rtc.enableBattery()) {
        Serial.printf("[RTC] - Battery activated.\n");
    }else {
        Serial.printf("[RTC] - Battery activation failed.\n");
    }
    Serial.println("4");
}

void loop(){
}

4 is never printed on the Serial.

Naguissa commented 2 years ago

Does it print any of:

[RTC] - Battery activated.

or

[RTC] - Battery activation failed.

zekageri commented 2 years ago

No

zekageri commented 2 years ago

If i comment out rtc.enableBattery() it goes to the next line which is

isRTC_PowerLost = rtc.lostPower();
if ( isRTC_PowerLost ) {
    Serial.print("[RTC] - Power went down.\n");
    rtc.lostPowerClear();
} else {
    Serial.print("[RTC] - Power was ok.\n");
}

which prints [RTC] - Power was ok.

After i set a time with a wrapper func rtc_setTime(50,35,20,1,3,10,21);

/*
*   Set the RTC time.
*/
void TimeSystem::rtc_setTime(byte sec,byte min, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year){
    rtc.set(sec, min, hour, dayOfWeek, dayOfMonth, month, year);
}

And reading wrong value.

void TimeSystem::rtc_getTime(){
    rtc.refresh();
    year        = rtc.year();
    month       = rtc.month();
    dayOfMonth  = rtc.day();
    hour        = rtc.hour();
    min         = rtc.minute();
    sec         = rtc.second();
    dayOfWeek   = rtc.dayOfWeek();
    if( isDebugOn ){
        Serial.print("[RTC] - Time: %d %d/%d %d:%d:%d",year, month, dayOfMonth, hour, min, sec);
    }
}

Which prints: [RTC] - Time: 165 25/165 45:85:857

zekageri commented 2 years ago

I2C scanner sees my ds3231

[RTC] - Scanning I2C bus...
Found address: 104 (0x68)
Found 1 device(s).

But with 0x68 it does not pass trought this function.

arduinomnomnom commented 1 year ago

Hi, I've just encountered this issue with a sketch that was working before. The ESP32 Dev board hangs at the enableBattery() line. Commenting it out and the sketch works.

Clue: I'm using the Arduino IDE 2.0.3 and had just changed from board ESP32 v1.0.6 to v.2.0.7

zekageri commented 1 year ago

I have just switched to an other lib.

Naguissa commented 1 year ago

I've to check it, maybe it's a timing issue, and would be good to add a I2C failure exit.

spaelectronics commented 1 year ago

I am also having a problem enabling the battery on my ESP32-WROOM-32.

URTCLIB_WIRE.begin();
  myRTC.set_model(URTCLIB_MODEL_DS3231);

  if (myRTC.enableBattery()) {
    Serial.println("Battery activated correctly.");
  } else {
    Serial.println("ERROR activating battery.");
  }

I always get "ERROR activating battery." on the Serial Monitor.

But when I change code to disableBattery(), it works perfectly!

INFORMATION:

I'm using Arduino IDE 2.0.4, with version 1.0.6 ESP32 board set from https://github.com/espressif/arduino-esp32

spaelectronics commented 1 year ago

I am also having a problem enabling the battery on my ESP32-WROOM-32.

URTCLIB_WIRE.begin();
  myRTC.set_model(URTCLIB_MODEL_DS3231);

  if (myRTC.enableBattery()) {
    Serial.println("Battery activated correctly.");
  } else {
    Serial.println("ERROR activating battery.");
  }

I always get "ERROR activating battery." on the Serial Monitor.

But when I change code to disableBattery(), it works perfectly!

INFORMATION:

I'm using Arduino IDE 2.0.4, with version 1.0.6 ESP32 board set from https://github.com/espressif/arduino-esp32

spaelectronics commented 1 year ago

As a matter of fact, rtc.lostPower() always returns false, even after a complete power failure (remove battery and remove vcc, and shorting battery pins to be sure)

The time is completely reset, but the lostPower() function still returns false.

I think this may be related to the same problem causing the enableBattery() function to fail. Possibly an ESP32 compatibility issue?

Naguissa commented 1 year ago

I have to check, but I suspect about ESP32 I2C being too fast or having any kind of read problem.

About lostPower, did you call refresh before it? Refresh is what updates all RTC info, including this piece of it.

Also, which RTC model are you using?

spaelectronics commented 1 year ago

Okay, so I put rtc.refresh() before both the lostPower AND enableBattery calls, and now this is what I get:

enableBattery is now returning true (the success message) lostPower is now returning true, however, even after I reboot the ESP32 (without disconnecting power), it continues to return true.

There is something very weird going on here.

Naguissa commented 1 year ago

For sure! enableBattery should not be affected by refresh...

I have to recheck the library with my ESP32s and last arduino version....

Naguissa commented 1 year ago

Oh, about RTC model, are you using the DS3231 or DS3232?

spaelectronics commented 1 year ago

I'm using DS3231