Pages: [1]   Go Down
Author Topic: Question regarding RTC libraries  (Read 1272 times)
0 Members and 1 Guest are viewing this topic.
Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First, I'd like to say I'm quite new to working with electronics and programming; in fact, my specialization in university and graduate school was microbiology, and not anything to do with computers/electronics.

That being said, I have good computer knowledge (building/Windows OS/networking), but have very little actual programming knowledge (did a little bit of Pascal back in the day...)

Needless to say, the Arduino IDE (from what I understand, an extension of C++) is quite intimidating, but I've managed to work my way through some of the more basic examples, and am working on trying to get a more complicated project.

Right now, I'm trying to get a DS1307 breakout board working, but for the life of me, can't seem to get it to work. Hardware wise, I have it connected to an Arduino Mega 2560.

The Vcc is connected to the 5V pin, the ground to ground, SDA to SDA and SCL to SCL. I have tried with and without pull up resistors (connected between 5V-SDA and 5V-SCL).

Using the Adafruit library (https://github.com/adafruit/RTClib) and the following code, I can get the RTC to work just fine.

Code:
  // 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(9600);
    Wire.begin();
    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 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);
}

However, when I try to use Henning Karlsen's RTC library (http://www.henningkarlsen.com/electronics/library.php?id=34) and the following code:

Code:
   // DS1307_Serial_Easy (C)2010 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// A quick demo of how to use my DS1307-library to
// quickly send time and date information over a serial link
//


#include <DS1307.h>

// Init the DS1307
DS1307 rtc(20, 21);

void setup()
{
  // Set the clock to run-mode
  rtc.halt(false);
 
  // Setup Serial connection
  Serial.begin(9600);

  // The following lines can be commented out to use the values already stored in the DS1307
  //rtc.setDOW(SUNDAY);        // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(3, 10, 2010);   // Set the date to October 3th, 2010
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
 
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
 
  // Wait one second before repeating :)
  delay (1000);
}

All I get is

xxxxxxxxx 85.85.2165 -- 27:85:85

Anyone have any clue about this?

One might wonder why not just use the Adafruit library and coding, and not Henning Karlsen's, but that is because the other project I am working on is based around his libraries and recoding it would be too big of a hurdle for me right now.
« Last Edit: January 03, 2013, 03:15:03 am by Darkblade48 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25860
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// I assume you know how to connect the DS1307.
// DS1307:  SDA pin   -> Arduino Digital 4
//          SCL pin   -> Arduino Digital 5
I assume you know that the I2C pins are analogue pins 4 & 5?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// I assume you know how to connect the DS1307.
// DS1307:  SDA pin   -> Arduino Digital 4
//          SCL pin   -> Arduino Digital 5
I assume you know that the I2C pins are analogue pins 4 & 5?
On the Arduino Mega2560 board, I thought the I2C pins were the SDA and SCL pins (pins 20 and 21, respectively)?

I will give it a try plugging it into the analog pins 4 and 5....
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25860
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry - missed the point that you were using a Mega.
Maybe lose the comment?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry - missed the point that you were using a Mega.
Maybe lose the comment?
I've removed the confusing comment that was in the coding; it's strange how some other libraries (Adafruit, Matt Joyce's old library that compiles in pre-1.0 Arduino IDE) work seemingly without problem.

The only difference that I can discern is that the other libraries use Wire.h whereas Karlsen decides to forgo using Wire.h.
Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm, I guess I'll have to rewrite my code if nobody else has any ideas.

Edit: I managed to figure out how to adjust the time using the Adafruit library, but now in trying to rewrite the coding, I am getting another error:

Code:
RTClib\RTClib.cpp.o: In function `date2days':
C:\Arduino-1.0.3\libraries\RTClib/RTClib.cpp:26: multiple definition of `i'
Project_1.cpp.o:(.bss.i+0x0): first defined here
c:/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
c:/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Warning: size of symbol `i' changed from 1 in Project_1.cpp.o to 2 in RTClib\RTClib.cpp.o

 smiley-eek-blue

I haven't the faintest clue how to fix this.

I am wondering if it might be easier just to figure out what is going on with the Karlsen library...
« Last Edit: January 04, 2013, 02:27:17 am by Darkblade48 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...All I get is

xxxxxxxxx 85.85.2165 -- 27:85:85

Anyone have any clue about this?
...
I've figure out this:
without TFT it is working fine
when TFT is on the place - it give this result :xxxxxxxxx 85.85.2165 -- 27:85:85
Did you find a solution?
Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was also using a TFT Touch screen when I encountered that problem.

I tried testing various values of pull up resistors, and it turns out that it required 4.7k Ohm (I was using a higher value before, and it did not seem to like it).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

does it work now?
Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It works just fine.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

with TFT and 4.7k Ohm resistors?
Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, I am using the TFT Touchscreen LCD with 4.7k Ohm resistors and using the sample sketch that Henning Karlsen wrote with no problems.

I did switch from a DS1307 to a DS3231, but using the exact same code still works (all I did was change one component).
Logged

Pages: [1]   Go Up
Jump to: