RTC 1307 "rtc not running"

With the example sketch: ds1307 (RTCLib) "installed" in the Arduino Mega, after i send one time with this line uncommented:

rtc.adjust(DateTime(2019, 5, 13, 22, 35, 0));

and comment this line sending from the second time, when i reset the arduino (pressing the red button on it) the time seens to work fine. BUT when i disconnect the USB cable, the date/time resets and i receive the message "rtc not running".

This is the module i'm using from a random store: https://www.lojaarduino.com.br/placas/brick-sensors/modulo-de-tempo-real-rtc-ds1307-para-arduino-mega-uno-1035/

Do you have a battery installed in the socket on the bottom side of the board? What voltage does that battery have?

pylon:
Do you have a battery installed in the socket on the bottom side of the board? What voltage does that battery have?

Yes. Its in 3.06v

Post the complete sketch you're using and a wiring diagram of your setup!

pylon:
Post the complete sketch you're using and a wiring diagram of your setup!

I'm using the Example Sketch from the RTCLib. It uses I2c communication.

https://pastebin.com/9kepbL0U

You have very strange symptoms, and it appears that the Clock Halt bit (bit 7 resgister 0) is not being cleared when the time is set.

Please run this code, and report back what you read for Register 0x00

// DS1307 I2C Read and Write Timekeeper Registers 0x00-0X07

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
#define startRegister 0x00
#define endRegister 0x07

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.print("Register");
  Serial.print("\t");
  Serial.println("Bit Values");
  Serial.println();

  //writeNVRAM(0x00, B00000000);//enable oscillator and set seconds to 0
  //writeNVRAM(0x00, B10000000);//disable oscillator set CH bit

  for (int a = startRegister; a <= endRegister; a++)
  {
    byte b = readNVRAM(a);
    Serial.print("0X");
    if (a < 16)
      Serial.print("0");
    Serial.print(a, HEX);
    Serial.print("\t");
    Serial.print("\t");


    for (int i = 7; i >= 0; i-- )
    {
      Serial.print((b >> i) & 0X01);//shift and select first bit
    }
    Serial.println();
  }
}

void writeNVRAM(byte location, byte data)
// writes data to DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.write(data);
  Wire.endTransmission();
}

byte readNVRAM(byte location)//// reads data from DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
  return Wire.read();
}

void loop() {
}

cattledog:
You have very strange symptoms, and it appears that the Clock Halt bit (bit 7 resgister 0) is not being cleared when the time is set.

Please run this code, and report back what you read for Register 0x00

// DS1307 I2C Read and Write Timekeeper Registers 0x00-0X07

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
#define startRegister 0x00
#define endRegister 0x07

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.print("Register");
  Serial.print("\t");
  Serial.println("Bit Values");
  Serial.println();

//writeNVRAM(0x00, B00000000);//enable oscillator and set seconds to 0
  //writeNVRAM(0x00, B10000000);//disable oscillator set CH bit

for (int a = startRegister; a <= endRegister; a++)
  {
    byte b = readNVRAM(a);
    Serial.print("0X");
    if (a < 16)
      Serial.print("0");
    Serial.print(a, HEX);
    Serial.print("\t");
    Serial.print("\t");

for (int i = 7; i >= 0; i-- )
    {
      Serial.print((b >> i) & 0X01);//shift and select first bit
    }
    Serial.println();
  }
}

void writeNVRAM(byte location, byte data)
// writes data to DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.write(data);
  Wire.endTransmission();
}

byte readNVRAM(byte location)//// reads data from DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
  return Wire.read();
}

void loop() {
}

Register Bit Values

0X00 10000000
0X01 00000000
0X02 00000000
0X03 00000001
0X04 00000001
0X05 00000001
0X06 00000000
0X07 10110011

0X00      10000000

Yes, this confirms that the clock halt bit is set. Normally, setting the time will clear bit 7 of register 0x00.

Try the code I sent with this line uncommented so it runs, and see if the clock halt bit clears.

writeNVRAM(0x00, B00000000);//enable oscillator and set seconds to 0

If the bit does not clear, I believe the module is defective.

the date/time resets and i receive the message "rtc not running".

What does the date/time reset to?

cattledog:

0X00      10000000

Yes, this confirms that the clock halt bit is set. Normally, setting the time will clear bit 7 of register 0x00.

Try the code I sent with this line uncommented so it runs, and see if the clock halt bit clears.

writeNVRAM(0x00, B00000000);//enable oscillator and set seconds to 0

If the bit does not clear, I believe the module is defective.

Register Bit Values

0X00 00000000
0X01 00011001
0X02 01010000
0X03 00000010
0X04 00100101
0X05 00010011
0X06 10011001
0X07 00000000

cattledog:
What does the date/time reset to?

Disconnect from USB makes lost the data/time.

0X00 00000000

OK, the bit did clear.

Now,remove the usb. Reconnect it, and run the register reading with the ch bit clearing line commented out.

//writeNVRAM(0x00, B00000000);//enable oscillator and set seconds to 0

What is in the registers now?

Register Bit Values

0X00 00001000
0X01 00000000
0X02 00000000
0X03 00000001
0X04 00000001
0X05 00000001
0X06 00000000
0X07 10010011

The more I experiment with my DS1307 I am convinced that your experience looks like there is no battery. You have measured voltage on the battery you have installed, but the battery holder may be defective or the module board. Can you verify that the battery voltage is seen at the Vbat pin of the chip. You will need to look at a data sheet for the device.
https://datasheets.maximintegrated.com/en/ds/DS1307.pdf

cattledog:
The more I experiment with my DS1307 I am convinced that your experience looks like there is no battery. You have measured voltage on the battery you have installed, but the battery holder may be defective or the module board. Can you verify that the battery voltage is seen at the Vbat pin of the chip. You will need to look at a data sheet for the device.
https://datasheets.maximintegrated.com/en/ds/DS1307.pdf

Battery is ok, i measured with multimeter. But i'm making some tests here and get this point: I removed the "BAT" wire (connected from RTC pin bat to AnalogIn) and now even removing the USB, the date/time is not lost anymore.
Maybe the bat pin is problematic ?

EDIT----
Yeah, i've made so many tests here, and when i connect some to analogPin to know what is the batt voltage, the data/time is lost.

I removed the "BAT" wire (connected from RTC pin bat to AnalogIn) and now even removing the USB, the date/time is not lost anymore.
Maybe the bat pin is problematic ?

Use a high value resistor between +battery and A0.
Something like 1Megohm.
Add a 100n cap from A0 to ground for stable readings.

It's very unusual for the Vbat pin to be connected to anything. Many modules don't even have this output broken out.

Post the complete sketch you're using and a wiring diagram of your setup!

I did not realize that connection between Vbat and A0. The wiring diagram asked for may have helped us move along quicker.