Can't get my RTC to work

Hi all. Newbie here.

I have just bought a Mega board and an RTC DS1307 (Link here) and I’m having problems hooking it up.

I soldered wires to it to test it before I devise somewhere to mount it permanently. I seem to have followed the instructions on various sites but still get no result. I primarily went with the tutorial that was linked on the item… (Link here) and added the various libraries.

I ran the two example sketches with the following results:

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop() {
  tmElements_t tm;

  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

This just gives me this every time I press the reset button:

DS1307RTC Read Test
-------------------
DS1307RTC Read Test
-------------------
DS1307RTC Read Test
-------------------

And the set time code of:

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

This literally gives me a blank screen and no output. I think I have wired correctly and check the voltage and continuity on the pins and all are in tact.

As a beginner, I literally don’t know where to start troubleshooting this as I thought it would be relatively easy, I thought I would start with this and then move onto the other shields that I got.

Any advice welcome.
Thank you, Steve.

Stevelondon:
As a beginner, I literally don’t know where to start troubleshooting this as I thought it would be relatively easy, I thought I would start with this and then move onto the other shields that I got.

First thing in troubleshooting I2C devices would be:

Load an ‘I2C scanner’ sketch on your board and check if the I2C device can be found:

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// 
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

If there cannot be found any I2C device on the I2C bus, you first have to fix the hardware connections.

Are you sure you have the RTC battery installed correctly?

Thanks Jurs, I'll have a go at that in a mo.

Aarg that's how it came. I've taken the battery out and checked that it has power and it does.

so these are my results after leaving it to stand for about 5 minutes…

I2
I2C Scanner
Scanning…

I2C Scanner
Scanning…

It looks like your arduino is being reset for some reason. If you don't press the reset button, you shouldn't see message "I2C Scanner" more than once.

Make sure it's not your arduino that is faulty. Disconnect your RTC and try this code for example

void setup()
{
    Serial.begin( 9600 );
    Serial.println( "start" );
}

void loop()
{
    static uint16_t counter = 1;
    Serial.println( counter++ );
    delay( 1000 );
}

It should print "start" only once and then print a counter every seconds...

It only came up with that because I pressed reset on the board to see if it would work then. It didn't do to itself.

If the I2C scanner does not report the device address, something is wrong with the device, or the connections to it.

How long are those wires? I2C is very sensitive to the capacitance of long wires, where 'long' is anything over 10cm.

The wires are about 8 inches long. The boards continuity seems to be ok and it is receiving the 5v as required.

I did what you said and disconnected the RTC and run the counter script which worked fine.

Stevelondon:
so these are my results after leaving it to stand for about 5 minutes…

I2
I2C Scanner
Scanning…

I2C Scanner
Scanning…

That’s bad!

The output should read and repeat every five seconds:

Scanning...
I2C device found at address 0x50  !
I2C device found at address 0x68  !
done

The RTC Tiny board has two I2C devices: The DS1307 RTC clock and an I2C EEPROM memory.
If none of the devices is reported, you have a hardware problem and NOT a software problem.

Faulty connections, wrong voltage, defektive RTC Tiny or defective Arduino board.

What do you have you can use for hardware testing?
Do you have a multimeter to measure voltage?

If so, load the following “empty” sketch to your Arduino:

void setup() {
}

void loop() {
}

Then measure voltage on the 5V pin, SDA and SCL against ground
5V pin ?V
SDA ?V
SCL ?V

Then load this sketch on the Arduino:

#include <Wire.h>

void setup() {
  Wire.begin();
}

void loop() {
}

Then measure voltage again on the 5V pin, SDA and SCL against ground
5V pin ?V
SDA ?V
SCL ?V

Repeat this measurement: One time without RTC Tiny module connected to the Arduino and another time while the RTC Tiny module is connected.

Which voltages can you find?

An additional testing possibility would be if you have another I2C module, like another RTC (DS3231), a compass sensor, an acceleration and/or gyroscope sensor that uses I2C. Do you have that? Then connect and test that using the I2C scanner sketch.

Hi Jurs. That’s for your very detailed reply. I am currently on holiday but will try all these tests when I return on Friday. I don’t have any other i2c devices, only a TFT screen and relay module. But ill do the thither tests and come back to you.

Thanks again.

Hi Jurs.

Back from holiday and here are my results that confused the hell out of me.

Empty sketch with NO devices connected: 5V pin 4.91V SDA 4.86V SCL 4.86V

Now with the code uploaded with NO devices connected: 5V pin 4.91V SDA 4.87V SCL 4.87V

Empty sketch with DS1307 RTC clock connected: 5V pin 4.90V SDA 0V SCL 4.89V

Now with the code uploaded with DS1307 RTC clock connected: 5V pin 4.91V SDA 0V SCL 4.89V

What I don't understand is that when the clock is not connected, SDA gives an output voltage, but as soon as I plug the wire in that goes to the SDA on the clock, it stops giving a voltage. Is that supposed to happen?

The DS1307 module may be defective, and short circuiting the SDA pin. You need to try another I2C device to isolate the problem.

Did you connect the 4 wires to the RTC device correctly ?

If you followed advice for a UNO, the I2C signalling wires SCL and SDA are different pins from the Mega.

You also have to worry about pull-up resistors for the I2C signal wires.

michinyon: Did you connect the 4 wires to the RTC device correctly ?

If you followed advice for a UNO, the I2C signalling wires SCL and SDA are different pins from the Mega.

You also have to worry about pull-up resistors for the I2C signal wires.

The Mega 2560 duplicates the SCL and SDA signals at the same location as the UNO connector does. It also has 10k pullup resistors on them.

Stevelondon: Empty sketch with NO devices connected: 5V pin 4.91V SDA 4.86V SCL 4.86V

Now with the code uploaded with NO devices connected: 5V pin 4.91V SDA 4.87V SCL 4.87V

Empty sketch with DS1307 RTC clock connected: 5V pin 4.90V SDA 0V SCL 4.89V

Now with the code uploaded with DS1307 RTC clock connected: 5V pin 4.91V SDA 0V SCL 4.89V

It looks like something is wrong with your I2C hardware: After "Wire.begin()" has been executed, the SDA and SCL pins surely should be pulled against 5V, because the internal pull-up resistors in the Atmega are activated. If the I2C device would not react, SDA/SCL would stay at HIGH. If the I2C would react as expected, SDA/SCL would be HIGH/5V after initialization of the Wire library. So if you find SDA at LOW/0V, something wents completely wrong.

I don't know what is going wrong, but at first I'd try: Take out the buffer battery for 10 minutes, then put it back and restart.

If that doesn't work, try another I2C device with your Arduino board.

Perhaps not one of those "Tiny RTC" modules of poor quality, which are dead on arrival in several cases. Perhaps try another DS3231 RTC module. DS3231 RTC modules have become really cheap at some eBay sellers from China lately, they also work with 5V voltage and I2C, the clock is much more accurate than DS1307.

Ok. So i have spoke to the man who sold me the RTC and he is sending me a new one so should have that soon.

I have also bought a new original Arduino Mega 2560 today to test the differences. Exactly the same results in that the SDA pin goes LOW once connected to the RTC. I also noticed that the SCL also drops to 3.3 after the SDA is connected too. Not sure why but at least the Mega seems constant between the two and should narrow down to the RTC which I can test when it arrives.

Stevelondon: I also noticed that the SCL also drops to 3.3 after the SDA is connected too.

Please check if you connected the RTC module to VCC=5V and NOT to VCC=3.3V! The DS1307 is a 5V device that needs VCC=5V voltage for I2C to work correctly!

You don't have any other I2C devices connected to the I2C bus while testing the RTC, do you?

BTW, can you have a look on the battery in your RTC module: Is it a 'LIR2032' (rechargabe 3.6V) cell or is it a 'CR2032' (non rechargable)? And which voltage can you measure between the modules GND and BAT pins?

Excellent news (little things please little minds eh) the new RTC arrived and works perfectly. :-) Thanks for all your help.