So I'm new to arduino and programming, I tried to get a DS1307 working on my arduino for turning on relays based on the current time ,and for data logging purposes. But I can't get the RTC to set the correct time. It's a Tiny RTC DS1307 from ebay, I have it installed on an arduiono mega2560. The vcc on the rtc is connected to 5v, gnd to gnd, sda to pin 20 (sda pin on mega), and sdl to pin 21 (sdl pin on mega). When I run the TimeRTC example from the Time library:
/*
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
*
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
void setup() {
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop()
{
digitalClockDisplay();
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
The serial port shows:
RTC has set the system time
17:18:09 17 4 2037
17:18:10 17 4 2037
17:18:11 17 4 2037
17:18:12 17 4 2037
17:18:13 17 4 2037
17:18:14 17 4 2037
17:18:15 17 4 2037
17:18:16 17 4 2037
17:18:17 17 4 2037
17:18:18 17 4 2037
Which is all fine and dandy except it's not 5:18PM April 17, 2037. If I try to set the time over the serial port with a unix stamp, like T1377641703 for 5:15PM 8/27/2013, nothing happens and the arduino continues printing out times from 24 years in the future.
If I try running the DS1307 example from RTClib:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup () {
Serial.begin(57600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
it prints out this:
RTC is NOT running!
2165/165/165 165:165:85
since midnight 1/1/1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since midnight 1/1/1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since midnight 1/1/1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
2165/165/165 165:165:85
since midnight 1/1/1970 = 2028820689s = 23481d
now + 7d + 30s: 2034/4/23 17:18:39
I've looked around on the web trying to find out how to fix such a basic problem, but haven't found a solution. What am I overlooking? Maybe my RTC is broke?