[SOLVED] Cannot set time (Arduino MEGA 2560 + TinyRTC v1.1 by Waiman)

Good day!

I was hoping you could help me figure out what’s wrong with my setup :cold_sweat:

I’m using Arduino MEGA 2560 and TinyRTC v1.1 by Waiman. I was going to use it with tft lcd with sd card slot for data logging. The tft lcd code is now working and I’m currently testing the rtc.

I connected the RTC Module directly to the Arduino,

Pins 20 and 21 are SDA and SCL, and I used pins 18 and 19 as powersource by declaring them as output and configuring the pins to output low and high.

I used the code in the DS1307 Lib>DS1307 like the example in the dfrobot site but when I open the COM port it doesn’t display anything.

#include <Wire.h>
#include <DS1307.h>

int rtc[7];
byte rr[7];
int ledPin =  13;
void setup()
{
  //DDRC|=_BV(2) |_BV(3);  // POWER:Vcc Gnd
  //PORTC |=_BV(3);  // VCC PINC3
  
    pinMode(18, OUTPUT);
    pinMode(19, OUTPUT);
    
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
  RTC.get(rtc,true);
  if(rtc[6]<12){
    RTC.stop();
    RTC.set(DS1307_SEC,1);
    RTC.set(DS1307_MIN,27);
    RTC.set(DS1307_HR,01);
    RTC.set(DS1307_DOW,7);
    RTC.set(DS1307_DATE,12);
    RTC.set(DS1307_MTH,2);
    RTC.set(DS1307_YR,12);
    RTC.start();
  }
  //RTC.SetOutput(LOW);
  //RTC.SetOutput(HIGH);
  //RTC.SetOutput(DS1307_SQW1HZ);
  //RTC.SetOutput(DS1307_SQW4KHZ);
  //RTC.SetOutput(DS1307_SQW8KHZ);
  RTC.SetOutput(DS1307_SQW32KHZ);
}

void loop()
{  
    digitalWrite(18, LOW);
    digitalWrite(19, HIGH);
    
  int i;
  RTC.get(rtc,true);

  for(i=0; i<7; i++)
  {
    Serial.print(rtc[i]);
    Serial.print(" ");
  }
  Serial.println();
 digitalWrite(ledPin, HIGH); 
 delay(500);
 digitalWrite(ledPin, LOW);
 delay(500);
 if (Serial.available() > 6) {
     for(i=0;i<7;i++){
       rr[i]=BCD2DEC(Serial.read());
     }
     Serial.println("SET TIME:");
       RTC.stop();
    RTC.set(DS1307_SEC,rr[6]);
    RTC.set(DS1307_MIN,rr[5]);
    RTC.set(DS1307_HR,rr[4]);
    RTC.set(DS1307_DOW,rr[3]);
    RTC.set(DS1307_DATE,rr[2]);
    RTC.set(DS1307_MTH,rr[1]);
    RTC.set(DS1307_YR,rr[0]);
    RTC.start();
 }
}
char BCD2DEC(char var){
  if (var>9){
     var=(var>>4)*10+(var&0x0f);
  }
  return var;
}

I also tried RTClib.h of adafruit and Jeelabs but it’s still not working.
I checked pins 18 and 19; they output 5V.

I don’t know what’s wrong :’(

Did I programmed the mega wrong? Did I downloaded the wrong library?

If this is not a programming question, please let me know :slightly_frowning_face:

I would unplug the device from the mega and try connecting the Vcc and Gnd inputs to the 5V and gnd pin on the other side of the Mega.

What you did, should work, but obviously it doesn't work for you.

If it works the other way, and not your way, then there is something wrong with your strategy for powering the device.

Th other obvious possibilities are, your device is broken, or your program is wrong.

The other thing I would do, is try a simpler sketch which will determine whether the device is communicating with you at all.

Make a copy of the sketch and then eliminate everything except getting the time from it and printing the result of that.

Can you detect the presence of the RTC using the I2C scanner?

http://playground.arduino.cc/Main/I2cScanner

I've checked it using the I2C scanner last friday and it reads 0x68 as device address so I did not think it was a connection problem.

Today, I tried plugging the power pins [of the RTC] directly to 5V and GND of the mega and loading a simpler program [from bildr.com]--doesn't involve downloading DS1307 lib. It worked! I guess I was hoping this connection with uno is the same with mega. Also, I was just too lazy to cut some wires. Sorry about that.

//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527


void setup(){
  Wire.begin();
  Serial.begin(9600);
  //setDateTime(); //MUST CONFIGURE IN FUNCTION
  
  pinMode(18, OUTPUT);
  pinMode(19, OUTPUT);
  
  digitalWrite(18, LOW);
  digitalWrite(19, HIGH);
}

void loop(){
  printDate();
  delay(1000);
}

void setDateTime(){

  byte second =      0; //0-59
  byte minute =      16; //0-59
  byte hour =        11; //0-23
  byte weekDay =     3; //1-7
  byte monthDay =    12; //1-31
  byte month =       15; //1-12
  byte year  =       14; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start 

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}

So why did my setup fail? -->setting pins 18 and 19 as power output

"pull-up resistors" Ugh! Just realized it this morning

Sorry again guys and thank you for the help!