Esp32 with DS3231 RTC Loosing time when powered Externally

Closed. This question is not about programming or software development. It is not currently accepting answers.


This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. You can edit the question so it's on-topic or see if it can be answered on another Stack Exchange site, but be sure to read the on-topic page for a site before posting there.

Closed 3 days ago.

Edit questionDelete question

I am working on a project where I need to interface RTC Ds3231 with the esp32 and then display the time on 4 digit 7 segment.

Now, this seems a pretty much straight forward and easy task and everything works fine until the esp32 is powered using an external power supply say mobile charger of 5v2amp.

When esp32 is powered externally it starts loosing time. The time loss always varies but for a general concept 5-10 minutes over 1 hour are lost.

programming of the Esp32 is As Follows

#include "RTClib.h"

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


//For 7 Segment 
int A = 14;
int B = 13 ;
int C = 27;
int D = 26;
int E = 25;
int F = 33;
int G =12;
int D1 = 4;   //DIGIT 1 ON PIN 1
int D2 = 5;
int D3 = 18;
int D4 = 19;
long starttimeforserial = 0;
boolean ftime = true;
long starttime = 0;

int h1;
int h2;
int m1;
int m2;


void setup() 
{
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(C,OUTPUT);
  pinMode(D,OUTPUT);
  pinMode(E,OUTPUT);
  pinMode(F,OUTPUT);
  pinMode(G,OUTPUT);
  pinMode(D1,OUTPUT);
  pinMode(D2,OUTPUT);
  pinMode(D3,OUTPUT);
  pinMode(D4,OUTPUT);
Serial.begin(115200);
 
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
 

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

Serial.print("  Clock ");

delay(3000);
}
 
void loop()
{
DateTime now = rtc.now();


Serial.print("Second: ");
Serial.println(now.second(), DEC);
 

 
Serial.print("Minutes; ");
Serial.println(now.minute(), DEC);
 
Serial.print("Hour: ");
Serial.println(now.hour(), DEC);
 
Serial.print("Days");
Serial.println(now.day(), DEC);
  

Serial.println(now.year(), DEC);
 
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
 
Serial.println("");

printLocalTime();



}

void Segment_7(int a, int b,int c, int d)
{
  int x[4];
  x[0] =a; x[1]=b; x[2] =c; x[3] =d;
  
  for(int i=0;i<=3;i++)
  {
    
    if(i==0){digitalWrite(D1, LOW);}
    else if(i==1){digitalWrite(D2, LOW);}
    else if(i==2){digitalWrite(D3, LOW);}
    else if(i==3){digitalWrite(D4, LOW);}
    
   switch(x[i]){

    
    //0
    case 0:
    digitalWrite(A,HIGH);
    digitalWrite(B,HIGH);
    digitalWrite(C,HIGH);
    digitalWrite(D,HIGH);
    digitalWrite(E,HIGH);
    digitalWrite(F,HIGH);
    break;
    //1
    case 1:
    digitalWrite(B,HIGH);
    digitalWrite(C,HIGH);
    break;
    case 2:
    digitalWrite(A,HIGH);
    digitalWrite(B,HIGH);
    digitalWrite(G,HIGH);
    digitalWrite(E,HIGH);
    digitalWrite(D,HIGH);
    break;
    case 3:
     digitalWrite(A,HIGH);
    digitalWrite(B,HIGH);
    digitalWrite(C,HIGH);
    digitalWrite(D,HIGH);
    digitalWrite(G,HIGH);
    break;
    case 4:
    digitalWrite(F,HIGH);
    digitalWrite(G,HIGH);
    digitalWrite(B,HIGH);
    digitalWrite(C,HIGH);
    break;
    case 5:
    digitalWrite(A,HIGH);
    digitalWrite(F,HIGH);
    digitalWrite(G,HIGH);
    digitalWrite(C,HIGH);
    digitalWrite(D,HIGH);
    break;
    case 6:
     digitalWrite(A,HIGH);
    digitalWrite(F,HIGH);
    digitalWrite(E,HIGH);
    digitalWrite(G,HIGH);
    digitalWrite(D,HIGH);
    digitalWrite(C,HIGH);
    break;
    case 7:
    digitalWrite(A,HIGH);
    digitalWrite(B,HIGH);
    digitalWrite(C,HIGH);
    break;
    case 8:
    digitalWrite(A,HIGH);
    digitalWrite(B,HIGH);
    digitalWrite(C,HIGH);
    digitalWrite(D,HIGH);
    digitalWrite(E,HIGH);
    digitalWrite(F,HIGH);
    digitalWrite(G,HIGH);
    break;
    case 9:
    digitalWrite(A,HIGH);
    digitalWrite(F,HIGH);
    digitalWrite(G,HIGH);
    digitalWrite(B,HIGH);
    digitalWrite(C,HIGH);
    digitalWrite(D,HIGH);
    break;
    
  }
  delayMicroseconds(700);
  digitalWrite(D1, HIGH);
  digitalWrite(D2, HIGH);
  digitalWrite(D3, HIGH);
  digitalWrite(D4, HIGH);
  digitalWrite(A,LOW);
  digitalWrite(B,LOW);
  digitalWrite(C,LOW);
  digitalWrite(D,LOW);
  digitalWrite(E,LOW);
  digitalWrite(F,LOW);
  digitalWrite(G,LOW);
 
  
  }


  
}
void printLocalTime()
{
  
DateTime now = rtc.now();
if((millis()-starttimeforserial)>1000)
{
 starttimeforserial = millis();


Serial.print("RTC DateTime: ");
 
  
  //delay(1000);
  
  if(millis()-starttime>200)
  {
    starttime =millis();
   if(now.hour()>9)
   {
    String temp = String(now.hour());
    char temp1 = temp.charAt(0);
    char temp2 = temp.charAt(1);
    h1 = temp1- '0';
    h2 = temp2 - '0';
    Serial.printf("h1 & h2 are => %d,  %d \n", h1, h2);
   }
   else
   {
    String temp = String(now.hour());
    char temp1 = temp.charAt(0);
    char temp2 = temp.charAt(1);
    h1 = 0;
    h2 = temp1 - '0';
   }

   if((now.minute())>9)
   {
    String temp = String(now.minute());
    char temp1 = temp.charAt(0);
    char temp2 = temp.charAt(1);
    m1= temp1 - '0';
    m2 = temp2 - '0';

    Serial.printf("m1 & m2 are => %d,  %d \n", m1, m2);
   }
   else
   {
    String temp = String(now.minute());
    char temp1 = temp.charAt(0);
    char temp2 = temp.charAt(1);
    m1=  0;
    m2 = temp1 - '0';
    Serial.printf("m1 & m2 are => %d,  %d \n", m1, m2);
   }
   
  }
    
  


}
  
  Segment_7(h1,h2,m1,m2);
}

Please post a schematic of your project

Can be a problem. The ESP could be drawing to little current from a phone battery charger to where the phone battery charger, which thinks the ESP is a battery, shuts the charge supply. Such a move may cause the ESP brownout detection to trigger. During the brownout the ESP32 may forget things or do the math wrong. The ESP may even be resetting.

I'd start with trying a different power supply other than a battery phone charger.

Where do you set the time to your RTC?

Like UKHeliBob say a good schematic of the whole setup.

The RTC needs a 3V battery like CR2032 or CR1220.

Thank You very much for your response following is the circuit schematic of esp32 attached to the rtc module and 4-digit 7 segments.


Please note that I have tried powering the esp32using vin and ground pin and using the USB connector result still says the same.
Once again than you very much for your response.

Thank you very much for your response. I will try it using different power supplies and then update the results accordingly.
Also, I will measure the results showing how much current is drawn from the mobile charger and compare it to the current getting drawn from the laptop power source and upload them as well.

Thank you very much for your response, I just shared the schematic.
Also, the RTC is powered by the cr2032 battery.

Is the RTC a 3V3 or a 5V module?

@Idahowalker As per the datasheet, the preferred voltage for its operation is 3.3 volts. However, the range varies from 2.3V to 5.5V. Following is the screenshot from the datasheet.

Dang. I don't see why there is an issue. Sorry I could not help.

I can't see a problem, but I would try disconnecting the 7 segment display and trying a sketch that just prints the time to the Serial monitor as it comes from the RTC with no formatting

Don't bother with any formatting, only print the time when the value of now.second() changes and remove the code associated with the 7 segment display

In other words, start with the basics

In your application

But what do this?

    // If the Arduino is NOT connected to a PC, then comment out the line below
    // Set the Date & Time to the time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

Where do you set the time to the right value?

@Idahowalker Don't be sorry mate. Your time and effort meant a lot to me. :innocent:

As stated above this code is commented when ever I want to connect it externally.
Generally this code is used for getting pc time and setting it in the RTC

Once I connect to the external power supply I don't have access to the serial on pc.
How ever I have done testing So.
And after 1 day when I connected the esp back to my PC. Serial monitor showed time 2 hours ahead. (still the same)

Wich timezone? It seems the RTC has the UTC time.

time_t time_provider();

time_t time_provider() {
  return rtc.now().unixtime();
}

In (RTC)setup

  setSyncProvider(time_provider);  //sets Time Library to RTC time
  setSyncInterval(300);            //sync Time Library to RTC every 300 seconds of 5 minuten
  DateTime.setTimeZone("CET-1CEST,M3.5.0,M10.5.0/3");// for MET Brussels, Paris, Berlin

@buckfast_beekeeper Thank you very much for the code and I will surely check with the update.
But the only confusion that still remains is. Why the RTC don't loose the time when it is connected with the laptop. Even if the cable is just a power cable (No Rx, Tx), but looses time when connected to almost any power supply.
If you want we can also meet virtually & discuss this problem a bit in dept.
As I already have 4-5 years of experience with IoT & Embedded systems I therefore consider this problem to be related with something more complex. However I do think I am missing important.
Your help is and will be highly appreciated.

I guess everyone is grasping at straws for a situation that seems impossible. Here are my straws:

The ZS-042 DS3231 module you are using has a "charging" circuit in case you want to use a rechargeable coin cell. But of course a CR2032 is a primary battery, and doesn't need to be recharged. So, you want to desolder either the diode or the resistor to disable the charging circuit.

Is the CR2032 still near 3V?

Can you check the voltage on the module's Vcc pin while you are running from the PC, and when running on some other supply, to see if there is any difference?

Just to clarify - when powering from some other supply, are you connecting through the USB connector - the same connector used with the PC, and supplying the same voltage? And you are not changing the firmware?

Unknown unknown suspected.

@ShermanP Thank you very much for taking your time and commenting on your valuable response.
You are absolutely right and it really is a situation that seems to be almost impossible but sadly it is. and it's been more than a year since I am looking for a solution.
Alright, So I just de-soldered the resistor on my zs-042 module:
And Following are the measurements of the voltage.

With No other Power.(Only RTC Battery connected CR2032 in the zs-042 module)
Battery terminal voltage: 3.32 volts

Powered On: Externally
The voltage on Esp32 Vin: 4.9 volts
The voltage on Esp32 3.3v pin: 3.30 volts
The Battery terminal voltage: 3.33 volts

Powered On: With a laptop
The voltage of Esp32 Vin: 4.46 volts
The voltage on Esp32 3.3v pin: 3.30 volts
The Battery terminal voltage: 3.32 volts

as far as the firmware and connector is concerned I am normally using the onboard esp32 USB connector in both cases and only one line is changed, commented out when connected to an external power supply, and un-commented when connected to a laptop(Just to set the time again.)

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

Further to clarify I have tested that line commented out on PC and the RTC won't lose time. sadly not the case with the power supply.

Although right now after de-soldering the resistor I have put the circuit to test and in 1-2 hours I will update you on whether the RTC is still losing time or not.
Once again thank you for your time and effort.