loading

Why doesnt DS1307 work on my arduino?

I am trying hard to get a DS1307 clock chip to work with my arduino... and i've gotten nothing thus far...

My setup: (for DS1307),
VCC connected to +5v on arduino, 
Clock pins connected to a 32.768khz crystal
Battery pin connected to arduino 3.3v output,
SDA and SCL pins pulled up with 1.8k resistors to +5v and then to respective A4 and A5 on arduino
Arduino is connected to my macintosh via USB. The atmega 328 on it is plugged into an UNO board but the chip is running the Duemilanove bootloader.

All I get when running Adafruits RTClib sketch is this:

2165/165/165 165:165:85
since midnight 1/1/1970 = 1402703185s = 16234d
now + 7d + 30s: 2014/6/20 23:46:55


complete junk readings, and the same no matter what the heck I try. Any ideas guys? I must admit I got my chip from a low cost thailand company (taydaelectronics), but I've tried 2 clock chips and 3 crystals and get the same results....???

sort by: active | newest | oldest
Have you set it ?

Hello, it has been a year since the question was asked but I recently encountered the same problem so I post the solution for anyone struggling with this as I have.

The problem in my case was insufficient voltage. I have arduino clone and when powered from the usb it puts out only +-3.6V on the 5V pin.

It all started to work perfectly when i powered the arduino from 12V PSU along the USB.

Hope it helps.

no_ext_psu.jpgext_psu.jpg

Thank you mate you saved my day.. my team was pondering over this issue since yeste evening.. . I connected a external power supply and .. BINGO !! Thanks anyways

PatrikH kuk1n1 year ago

Thanks for the hint! I accidently set my USB-adapter to 3V3 for a 5V Arduino and got this strange results. Your note set me on track and my DS1307 is now working as it should.

kuk1n PatrikH1 year ago

Glad to hear it helped someone :)

astroboy907 (author)  steveastrouk4 years ago
I *try* to set it (using the RTC.adjust(DateTime(__DATE__, __TIME__)); in RTClib) but I don't know if it is doing anything I have tried a few other libraries and get skewed time results any time I try to set it...
First of all, I'd check whether your clock is running, using an oscilloscope. Check that SDA and SCL ARE active too.
astroboy907 (author)  steveastrouk4 years ago
Don't have an o-scope. Best I can do is with another board or chip... haven't had the need for the scope yet (why I don't have one)
If you had one, you could have saved 2 chips and three crystals.....
astroboy907 (author)  steveastrouk4 years ago
Turns out my arduino A5 pin broke off (from plutiple insertions). Will try again in a couple days (outta town) with a new/fixed chip
astroboy907 (author)  steveastrouk4 years ago
I doubt I blew the chips (I did mess with them a lot, but didnt get magic blue smoke)..

Anyways, if I cannot figure this out I can always use the arduino watchdog timer...

Is it possible to blow the DS1307 when using a 9V battery connected to the barrel jack of an Arduino UNO? I assumed that the jack is regulated to prevent frying the board and attached components.

I had written and finished debugging my code which required a timestamp for both when the log file is created and when the sensor is read. When attached to my computer using the USB cable the code executed just dandy and was even more on time than my wrist watch. However, when I unplugged it from my computer and attached the battery to the board, through the barrel jack, both the Memsic 2125 and DS1307 stopped working and I received the 165 error from the RTC unit and the RTC is not running when I use the !rtc.isrunning() library feature.

I checked all the joints and the battery terminal on the DS1307 breakout board every appears to be fine, I tried every software solution to reset the clock to no avail. I am waiting on a spare DS1307 chip in the mail but I did not see any blue smoke arise from it when I plugged in the 9V battery for the standalone mode test. I am thinking that somehow I shorted both the DS1307 and Memsic 2125 sensor but I do not understand how and why it happened.

diy_bloke2 years ago

The '165'error may have several causes:
Broken chip
No pull ups on the sda and scl (shouldn’t generally be necessary if you use a module)
Voltage too low
problem in your sketch (did you remember to declare 'Wire.begin')
Wrong I2C port as described below
generally it pays to see if the RTC is actually running. the adafruit lib allows the following statement to check that:

rtc.begin();
if (! rtc.isrunning()) {
Serial.println(F("RTC is NOT running!"));
}

dconsonni2 years ago

Check the datasheet, different versions of arduino uses different ports for SDA and SCL.

Smashim4 years ago
using arduino 1.03 this is the only sketch I was able to get working so I think this will do it for you..............credit goes to Maurice Ribble with some added comments by me to use my geeetech DS1307 module to work (basically same as your setup)

//
// Maurice Ribble
// 4-17-2008
// http://www.glacialwanderer.com/hobbyrobotics

// This code tests the DS1307 Real Time clock on the Arduino board.
// The ds1307 works in binary coded decimal or BCD. You can look up
// bcd in google if you aren't familior with it. There can output
// a square wave, but I don't expose that in this code. See the
// ds1307 for it's full capabilities.
// GEETECH SD1307 RTC MODULE NOTES:
// -----SWITCH MUST BE IN "RS" POSITION
// -----BACKUP BATTERY IS CR 1225 TYPE (3V LITIUM) WITH "+" SIDE ON TOP

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

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

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

// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/*void stopDs1307()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(0x80);
Wire.endTransmission();
}*/

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}


void setup()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
Serial.begin(9600);

// Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.
second = 10;
minute = 9;
hour = 9;
dayOfWeek = 5;
dayOfMonth = 25;
month = 1;
year = 13;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day_of_week:");
Serial.println(dayOfWeek, DEC);

delay(1000);
}

Thanks Smashin! This is working with mine after the actual Time library would say otherwise. I was able to set it, then after omitting the set code, ...able to retrieve the correct date every time (especially when powering down but with a bat.)

Smashim4 years ago
this is the section of code where you enter in the date/time info:

// Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.
second = 10;
minute = 9;
hour = 9;
dayOfWeek = 5;
dayOfMonth = 25;
month = 1;
year = 13;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}