Go Down

Topic: DS1307 error (Read 465 times) previous topic - next topic

mosely

I am pretty new to arduino but have taken on the task of trying to make a controller for my dart frog terrarium. I purchased a ds1307 to keep time but I cannot get it to work properly. I am using the RTClib from adafruit and trying to use the example code. When I upload the code the date and time reads 2100/1/17 0:1:33 and stays at that repeating the serial post but the time does not change. I moved the line of code to reset the clock every time it uploads and now it will display the correct time on the first serial entry but then it goes back to displaying 2100/1/17 0:1:33. When I press reset the first serial entry will be the time that I uploaded last before going back to 2100/1/17 0:1:33. I am lost on what to do and hope that someone here can help me out.

jremington

Please read the forum guidelines and post your code, using the "#" button.
"It seems to run on some form of electricity"

Nick_Pyner

Try this to set the time.

Code: [Select]
//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);
}


and this to read the time ----------------------------------------------------------

Code: [Select]
//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include <PCD8544.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68
static PCD8544 lcd;

void setup(){
  Wire.begin();
  Serial.begin(9600);
  lcd.begin(84, 48);
 
  // Print a message to the LCD.
    lcd.setCursor(0,0);
  lcd.print("Today it is");
}

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


  lcd.setCursor(21,1);
    switch (weekDay)               // Friendly printout the weekday
  {
    case 1:
      lcd.print("MONDAY  ");
      Serial.print("MON  ");
      break;
    case 2:
      lcd.print("TUESDAY  ");
      Serial.print("TUE  ");
      break;
    case 3:
      lcd.print("WEDNESDAY  ");
      Serial.print("WED  ");
      break;
    case 4:
      lcd.print("THURSDAY  ");
      Serial.print("THU  ");
      break;
    case 5:
      lcd.print("FRIDAY  ");
      Serial.print("FRI  ");
      break;
    case 6:
      lcd.print("SATURDAY  ");
      Serial.print("SAT  ");
      break;
    case 7:
      lcd.print("SUNDAY  ");
       Serial.print("SUN  ");
      break;
  }

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

   lcd.setCursor(0,4);
  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);
 
  lcd.setCursor(0,5);
 
   if( second==0)
  {
   lcd.print("         ");
   lcd.setCursor(0,5);
   }
 
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
  lcd.print(second); 
}


Strip out the display stuff you don't need.

mosely

Here is the code that I was using, as I stated it is basically the example code for the ds1307 in RTClib.
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

Code: [Select]
#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();
  rtc.adjust(DateTime(__DATE__, __TIME__));

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
  }
}

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


Nick, I tried your code and had similar results as the example code. The time would reset and then the all following serial entries change to a different time and stay the same. The difference is that the first serial entry is not set to the same date as my computer. Here are the first few serial entries using your code:

4/19/13     0:24:30
loopstart
4/19/13     6:24:30
14/26/99     0:0:63
14/10/99     0:0:63
14/10/99     0:0:63
14/10/99     0:0:63
14/10/99     0:0:63
14/10/99     0:0:63
14/10/99     0:0:63
14/10/99     0:0:63
14/10/99     0:0:63

Nick_Pyner

I should have been a bit clearer, but I'm not sure this is the problem anyway.

The setting code has to be edited for the time you want to start the clock at, say a couple of minutes into the future.
You then press reset just before, and release it at, the set time. Use once. If you want to do it again, you have to edit again.

Having said that, if you have any display at all, the programme is working.

Your problem could be power. I only say that because it often is. The relationship between the DS1307, its battery, and the Arduino 5v supply seems to defy all logic. The battery could be suss and/or, if you are getting power from the USB cable. you may also find you can cure the problem by using a 9v wall wart.


mosely

I don't have a 9v AC adapter at the moment but I just tried it with a 9v battery attached and had the same results. I don't have a spare LCD screen so the board was still attached to the computer. Could it be that the DS1307 is just broken or do you think that there is some way to fix this?

JimboZA

I've had great success with a couple of libraries from Henning Karlsen. I don't have a 1307, so I can't vouch for his 1307 library, but why not give it a try?
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Nick_Pyner


I don't have a 9v AC adapter at the moment but I just tried it with a 9v battery attached and had the same results. I don't have a spare LCD screen so the board was still attached to the computer. Could it be that the DS1307 is just broken or do you think that there is some way to fix this?


I think you have fallen into  a trap there, sometimes known as the Radio Shack trap. This could indeed be a power problem but a 9v battery is not a solution because it doesn't have any. It's all volts and no amps, so to speak. There is nothing to suggest anything is broken, and I would be disinclined to conclude  that until all other possibilities are properly addressed. Don't worry about the LCD. You don't need it, and it, or at least its backlight, could be more of a hinderance than a help.



Go Up