uRTCLib icon indicating copy to clipboard operation
uRTCLib copied to clipboard

Sketch stuck at rtc.enableBattery()

Open zekageri opened this issue 2 years ago • 4 comments

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.

zekageri avatar Oct 03 '22 18:10 zekageri

Does it print any of:

[RTC] - Battery activated.

or

[RTC] - Battery activation failed.

Naguissa avatar Oct 03 '22 18:10 Naguissa

No

zekageri avatar Oct 03 '22 18:10 zekageri

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 avatar Oct 03 '22 18:10 zekageri

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.

zekageri avatar Oct 03 '22 18:10 zekageri

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

arduinomnomnom avatar Feb 25 '23 04:02 arduinomnomnom

I have just switched to an other lib.

zekageri avatar Feb 25 '23 06:02 zekageri

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

Naguissa avatar Feb 25 '23 12:02 Naguissa

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 avatar Mar 27 '23 12:03 spaelectronics

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 avatar Mar 27 '23 12:03 spaelectronics

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?

spaelectronics avatar Mar 27 '23 13:03 spaelectronics

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?

Naguissa avatar Mar 27 '23 13:03 Naguissa

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.

spaelectronics avatar Mar 27 '23 13:03 spaelectronics

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

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

Naguissa avatar Mar 27 '23 13:03 Naguissa

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

Naguissa avatar Mar 27 '23 13:03 Naguissa

I'm using DS3231

spaelectronics avatar Mar 27 '23 14:03 spaelectronics