RTClib not doing it's job?

So I have a simple setup on my arduinoUNO where I would like to display the time on an LCD using an RTC. I have it all hooked up correctly and tested the two modules individually to ensure they work.

Now the problem is when I write: lcd.print(now.second()); or minute,hour, anything like that, the program gives me the error message saying “now” was not declared in this scope.

Here’s the code:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
   #define Serial SerialUSB

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
  lcd.begin(16, 2);

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);

  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(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

void loop () {
    DateTime now = rtc.now();
    Serial.print(now.year(), DEC);
    Serial.print(now.month(), DEC);
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(now.minute(), DEC);
    Serial.print(now.second(), DEC);
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);

I’ve simply copied most of the code from the tutorial adafruit and installed the RTClib which is being used here. But for some reason it tells me “now” is not declared when it works for serial.print easily


ds1307.ino (1.86 KB)

@groundFungus - that's not the issue, you get it with the #include "RTClib.h" most likely since OP said it works with Serial. the issue is just code sequence...

  lcd.print(now.second()); // YOU TRY TO USE 'now' HERE
  DateTime now = rtc.now(); // BUT YOU DECLARE AND DEFINE IT LATER... CAN'T WORK...

just exchange the lines of code, first define the variable, then use it

Ohhhh I had no idea the location in the section mattered, that helps a lot. Thank you very much!!

@groundFungus - not cool to remove a comment - then mine looks weird...

Ohhhh I had no idea the location in the section mattered, that helps a lot. Thank you very much!!

yep it does :slight_smile:

that's why you'll usually see variables defined at the beginning of a function and then used later (or for global variable at the start of a file). Some tricks might happen sometimes in the background during the compilation process to help solve some cases, but better not rely on that.

--> remember 1st define your variables, 2nd use them :slight_smile:

I apologize. I removed it when I realized my post was stupid, irrelevant and possibly confusing after seeing yours. I checked the RTClib and saw that the DateTime class was included in the RTClib.

fine - usually better to just edit and cross the text or just let it go.

no worries