Using a real time clock (RTC) on Arduino data logging shield

7.1k views Asked by At

I'm trying to set and read the time on an Adafruit data-logging shield for Arduino but the open source code on the Adafruit website doesn't seem to be working as it returns several errors such as 'RTC_DS1307' does not name a type, and saying several variables are outside the scope. The code is:

enter code here #include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
Serial.begin(57600);
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
// uncomment it & upload to set the time, date and start run the RTC!
//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);

}

Does anyone have any idea how to solve this? Thanks a lot!

5

There are 5 answers

0
praks411 On

I think you are not including proper headers of the libs.

Try changing

#include "RTCLib.h"

to

#include <RTCLib.h>

Make sure your have proper libs and headers for the shield.

0
Stefano On

You should use the import library feature available in the Arduino IDE.

From the Main Menu select

Sketch | Import Library | Add library 

and provide the path of the folder which holds the library files.

When you want to create a sketch which makes use of that library, just select

Sketch | Import library

and select the library name from the list. The correct header will be automatically included at the top of your sketch.

0
therealmitchconnors On

This might be several months too late, but #include "RTCLib.h" looks in the current project folder for RTCLib.h, while #include <RTCLib.h> looks in the libraries folder. If the code above is in a sketch (ie is not the example file in the library), changing your includes statement may fix your code.

0
techkilljoy On

I just had the exact same problem and solved it this way:

The sketch looks for the RTC library in:

C:\users\<userid>\Documents\Arduino\libraries

You must install the library yourself by (and close all instances of the Arduino IDE prior to doing this as it only detects libraries on startup):

  1. Downloading it from: https://github.com/adafruit/RTClib
  2. Extracting the zip file
  3. Renaming the inner of the two folders named "RTClib-master" to "RTClib" (note- this is the folder that contains the readme.txt file as well as other files)
  4. Moving this folder to the library directory mentioned above
  5. Restart IDE
0
jeffrey On

I had the same problem. It seems that when I extracted the zip file, it created the rtc file inside another file. So, whenever you call the library while running your sketch, it won't be able to find it since the header file is found inside another folder.

What you have to do is simply to make sure that you copy the RTClib folder (which contains the header files) directly in the Arduino library.

Do not copy the folder that contains the RTClib folder.

Sorry for my English I am not a native speaker.