the clock ds1307 isn't working,help

I have connect it to my ARDUINO :
SDA----->Analog in(A5)
SCL------>Analog in(A4)
5V->5V
Gnd->GND

have try to use the set time code

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
int RedLED=9; //digital ourput LED
int GreenLED=10;//digital ourput LED
void setup(){
  Wire.begin();
  Serial.begin(9600);
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
  //  

  printDate();
  Serial.print("Analog 2 is - ");
  Serial.println(analogRead(A2));
  Serial.print("Analog 3 is - ");
  Serial.println(analogRead(A3));
  Serial.print("Analog 4 is - ");
  Serial.println(analogRead(A4));
  Serial.print("Analog 5 is - ");
  Serial.println(analogRead(A5));
digitalWrite(RedLED,HIGH);
digitalWrite(GreenLED,LOW);
  delay(3000);
 digitalWrite(RedLED,LOW);
digitalWrite(GreenLED,HIGH);
  delay(1500);
}

void setDateTime(){

  byte second =      00; //0-59
  byte minute =      50; //0-59
  byte hour =        8; //0-23
  byte weekDay =     3; //1-7
  byte monthDay =    1; //1-31
  byte month =       10; //1-12
  byte year  =       13; //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(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}

but still I can't get any time just
165/165/165 165/165

why is it?

Thanks ,

Hi,

Looking at this tutorial about the DS1307 on Adafruit Overview | DS1307 Real Time Clock Breakout Board Kit | Adafruit Learning System
Your code it not talking to the chip correctly. It seems you are close. Have a read though it and you should be able to make the changes needed in your code.

I2C requires pullup resistors on SCL/SDA lines, do you have those? 4.7K works great.

DS1307 requires MSB of register 0 to be 1 for the clock to run. Is the code perhaps clearing that bit when the time is set?

"Bit 7 of Register 0 is the clock halt (CH) bit. When this bit is set to 1, the oscillator is disabled. When cleared to 0, the oscillator is enabled. On first application of power to the device the time and date registers are typically reset to 01/01/00 01 00:00:00 (MM/DD/YY DOW HH:MM:SS). The CH bit in the seconds register will be set to a 1. The clock can be halted whenever the timekeeping functions are not required, which minimizes current (IBATDR)."

http://datasheets.maximintegrated.com/en/ds/DS1307.pdf

Are you using an Arduino ready DS1307 RTC module, or are you cobbling something up yourself with a bare chip?

Your code looks essentially the same as mine but appears to be cluttered with irrelevant junk. Try this

//Arduino 1.0+ Only
//Arduino 1.0+ Only
// pre-set the time in the void then use reset button to set it!


#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
printDate();
Serial.println("loopstart");
}

void loop(){

  printDate();
  
  delay(1000);
}

void setDateTime(){

  byte second =      30; //0-59
  byte minute =      24; //0-59
  byte hour =        0; //0-23
  byte weekDay =     5; //1-7
  byte monthDay =    19; //1-31
  byte month =       4; //1-12
  byte year  =       13; //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);
}

still not good
this is what I'm getting :

165/165/165 45:165:165
165/165/165 45:165:165
165/165/165 45:165:165
165/165/165 45:165:165
165/165/165 45:165:165
165/165/165 45:165:165
165/165/165 45:165:165

is it possible that my analog pin are not working good?

and yes I'm using a model of the DS1307 and connection like this :
http://bildr.org/2011/03/ds1307-arduino/

I'm with Bob. You need those pullup resistors on sca & scl.

I don't understand how that tutorial works without them. The Sparkfun breakout board doesn't have them fitted and the sketch does not set internal pullups either. Does wire.begin() set internal pullups?

Internal pullups are too weak for I2C anyway. 4k7 pullups on both lines.

No, I wouldn't rely on internal pullups for i2c either. Just curious how that tutorial ever worked. The only thing I could think of was internal pullups...

well this is embarrassing........ :blush: :blush: :blush: :blush:

the wire that connect from SDA to A4 wasn't good.. replace it and now it's just fine.
now I have another question about the code:

this is the code I'm using in order to see the time:

#include "Wire.h"
#include <SPI.h>
#define DS1307_ADDRESS 0x68

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


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

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);

  byte zero = 0x00;
  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(monthDay);
  Serial.print("/");
  Serial.print(month);
  
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}

this is what I get:

8/10/13 14:5:26
8/10/13 14:5:27
8/10/13 14:5:28
8/10/13 14:5:29

my question is what I need to change in order to get 2 digits number in minutes+seconds?
like so: 8/10/13 14:05:06

and also what need to add in order so see the day?
for example
1 would be Sunday
6 would be Friday
ext....

Thank ,

lcd.print(":");
    if(second <10)
  {
    lcd.print("0");
  }
  lcd.print(second);
 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());


  lcd.setCursor(12, 0);
    switch (weekDay)                      // Friendly printout the weekday
  {
    case 1:
      lcd.print("MON  ");
      Serial.print("MON  ");
      break;
    case 2:
      lcd.print("TUE  ");
      Serial.print("TUE  ");
      break;
    case 3:
      lcd.print("WED  ");
      Serial.print("WED  ");
      break;
    case 4:
      lcd.print("THU  ");
      Serial.print("THU  ");
      break;
    case 5:
      lcd.print("FRI  ");
      Serial.print("FRI  ");
      break;
    case 6:
      lcd.print("SAT  ");
      Serial.print("SAT  ");
      break;
    case 7:
      lcd.print("SUN  ");
       Serial.print("SUN  ");
      break;
  }

I have read more about the "switch\case" function and understand more

Thank you very much!

Hi David, as an alternative to the switch/case...

char dow[7][6] = { "MON  ", "TUE  ", "WED  ", "THU  ", "FRI  ", "SAT  ", "SUN  " };

  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());


  lcd.setCursor(12, 0);
  lcd.print(dow[weekDay]);
  Serial.print(dow[weekDay]);

  }