Strange output from DS3231 RTC

Problem

I have an Arduino Uno, along with an DS3231 RTC and an SD card reader. I'm setting it up as part of a battery charge/discharge test circuit, with the RTC being used as logging information.

Running the DS3231 example code by itself works fine. However, when I implemented the example code into my battery testing program, I get strange outputs.

For instance, with the example code, I get this output on the serial monitor:

201 1 1 1 0 0 0 24h T=28.00 O-
Alarm 1: 0 Date 0 0 0
Alarm 2: 0 Date 0 0

1/1/1day of the week :1
** 0:0:1**
201 1 1 1 0 0 1 24h T=28.00 O-
Alarm 1: 0 Date 0 0 0
Alarm 2: 0 Date 0 0

** 1/1/1day of the week :1**
** 0:0:2**
201 1 1 1 0 0 2 24h T=28.00 O-
Alarm 1: 0 Date 0 0 0
Alarm 2: 0 Date 0 0

etc...

As intended. However, with the logging code, I get this output:

Initializing SD card...card initialized.
165/85/165 day of the week :165
** 25:165:165**

2.00

165/85/165 day of the week :165
** 25:165:165**

2.00

165/85/165 day of the week :165
** 25:165:165**

1.99

etc...

Note that the '1.99' and '2.00' values are the voltage read through A0. This is essentially an emulation of the battery's voltage; when it reaches certain voltages it lights up either a red or green LED. The voltage is modified using a variable resistor.

Also, note that the format is different; this is the shortened version of the time information. The same wrong numbers are present in the original expanded form.

Code
I am using this library for the DS3231.

I have included the code for the testing program and the battery program.

Already completed tests:

I have already checked to see that the DS3231 is wired correctly and it is. I've also checked to see if initializing the wire used for the voltage read causes the issue by initializing it in the original test program; this is not the case.
Moving the time code around also does nothing.

I will post the wiring if it is desired.
Finally, "setDS3231time(0,0,0,1,1,1,1);" is a holdover from a custom-made library. This is just a simple instruction that sets the DS3231 to 0 time every time it is run.

If someone could please help me with this, it would be greatly appreciated!

SuperTest.zip (2.65 KB)

BatteryProgramSuperNewClock.zip (3.6 KB)

Maybe the below section in BatteryProgramSuperNewClock causes your problem

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  //Time Start
  setDS3231time(0,0,0,1,1,1,1);
  //Time End

If there is no card, you do not initialize the DS3231.

But your loop() will still be executed; the return in the if block does not prevent that. If you don't want the loop to be executed, you should replace the return with e.g.

  for (;;);

You should actually see 'Card failed, or not present' in the serial monitor; as you don't mention that, I'm not sure that it is the cause of your problem. But I think it's worth considering.

Thanks for the suggestion, I'll try that as soon as I can.

165/85/165 day of the week :165

This is caused by data read errors. Do you have external pullup resistors on the I2C pins?

sterretje:
Maybe the below section in BatteryProgramSuperNewClock causes your problem

  // see if the card is present and can be initialized:

if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
   // don't do anything more:
   return;
 }
 Serial.println("card initialized.");
 //Time Start
 setDS3231time(0,0,0,1,1,1,1);
 //Time End





If there is no card, you do not initialize the DS3231.

But your loop() will still be executed; the return in the if block does not prevent that. If you don't want the loop to be executed, you should replace the return with e.g.


for (;;);




You should actually see 'Card failed, or not present' in the serial monitor; as you don't mention that, I'm not sure that it is the cause of your problem. But I think it's worth considering.

Unfortunately this did not work. Thanks for the suggestion though.

jremington:
This is caused by data read errors. Do you have external pullup resistors on the I2C pins?

I'm still a newbie, I'm not sure I understand.

I do have a variable resistor used to emulate the battery's voltage, and I have 3 other resistors used to keep the LEDs at the right voltage (as I understand it.) Are you referring to the 3 resistors or the variable resistor?

I'll include an image of the circuitry so you can see what I mean:

Thanks for the help!

This is caused by data read errors. Do you have external pullup resistors on the I2C pins?

I'm still a newbie, I'm not sure I understand.

Clearly you don't understand, and Google could undoubtedly help with that.

You need 4.7K resistors from Vcc to A4 and A5 (the I2C pins). If those resistors are not on the RTC module, you have to add them externally.