Go Down

Topic: Strugling with RTC (Read 1 time) previous topic - next topic


I'm trying to build a controller for an automatic chicken coop door.  The door and the mechanics are no problem at all but I have no electronics nor programing experience.  As a result I have been stubbing my toe on setting up my RTC to work with the UNO R3 I have.  The pic below shows my set-up with the Arduino UNO on the bottom, the Arduino motor controller shield on top, and the Adafruit RTC plugged into the motor controller.  The black plastic box is an old Pelican box I had in my junk pile and works great to keep the electronics clean in a dusty chicken coop.

Using the instructions at the Adafruit learning site I have installed (I guess) the RTClib library and compiled/loaded the "TEST" sketch into my UNO.  The compiling and loading seemed to work OK but when I go to serial monitor I get a blank screen.

Since I get a blank screen I do not know if the sketch is loaded properly.  Since I get a blank screen I do not know if the clock time is set properly.  Can someone talk me through some troubleshooting steps?  Thanks.


Hi Playstationfarm.

(Been where you are.  Don't fret too much.)

Couple of things though:
The sketch would be handy to look at.

Zip it up and post it as an attachment.

Good luck.


Hi, I'm not the biggest expert, but here is something to start with:
Have you tested your Uno with any other code? Could be that the hardware isn't working right. Try loading this sketch first:
Code: [Select]

void setup()
Serial.begin( 9600 ); // Opens the serial line @ 9600 baud
delay( 10 );
Serial.println( "Hello, world" ); // prints out on the Serial monitor

void loop()

This should print "Hello World" on your serial monitor.

If this works, there is something weird about the RTC code you are trying to run. You should upload it, so we can take a look.
If the Hello World thing doesn't work, there is probably a big hardware problem.

This is just a starting point. Tell us what you find.


Mar 29, 2013, 12:16 am Last Edit: Mar 29, 2013, 12:18 am by PlayStationFarm Reason: 1
Sorry.  I should have put this in my first post.  It's the RTC test sketch provided by Adafruit on their website.

I have gone through many of the learning lessons from various places on the internet and I also have had good success with printing to screen with a couple of the lessons.  Also, I have written a sketch that runs my coop door open and closed just great.  I encountered lots of trouble trying to program lengthy pause times.  So I decided that an RTC was needed to overcome millis() limitations.

Here is the RTC test sketch provided by Adafruit . . .

Code: [Select]
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"


void setup () {

 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(now.month(), DEC);
   Serial.print(now.day(), DEC);
   Serial.print(' ');
   Serial.print(now.hour(), DEC);
   Serial.print(now.minute(), DEC);
   Serial.print(now.second(), DEC);

   Serial.print(" since 1970 = ");
   Serial.print("s = ");
   Serial.print(now.unixtime() / 86400L);

   // 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(future.month(), DEC);
   Serial.print(future.day(), DEC);
   Serial.print(' ');
   Serial.print(future.hour(), DEC);
   Serial.print(future.minute(), DEC);
   Serial.print(future.second(), DEC);


I may be having trouble with my addition of the RTClib library.  I remember reading somewhere in the gazillion pages of my studies that the Arduino IDE will automatically color the #include commands when the correct files are located in the library directory.  My IDE colors those commands blue and I'm pretty sure that's not good.  I did have some difficulty getting those RTClib.cpp and RTClib.h files in the library.  I might have something wrong there.  But when I compiled that sketch no error messages were given.  Hmmmmm.

Here is the sketch that I have tried to use with the lengthy pause intervals between run times for my door motor.  I have tested this same sketch with much shorter timing settings and it works very good.

Code: [Select]
// Drive motor CW 34 seconds, pause 16 hours, run motor CCW 34 seconds, pause 8 hours, repeat.  L.C. H. 2/24/2013

// Motor A definitions
int DIR_A = 12;
int SPEED_A = 3;
int BREAK_A = 9;

// Motor B definitions
int DIR_B = 13;
int SPEED_B = 11;
int BREAK_B = 8;

void setup()
 pinMode(SPEED_A, OUTPUT);
 pinMode(DIR_A, OUTPUT);
 pinMode(BREAK_A, OUTPUT);

 // release break
 digitalWrite(BREAK_A, LOW);

void loop()
 // run motor backward full speed.  (Max is 250)
 digitalWrite(DIR_A, HIGH);
 analogWrite(SPEED_A, 250);

 // run motor for 34 seconds to open

// stop motor
 analogWrite(SPEED_A, 0);

 // pause door open 16 hours (minus runtime)

// run motor forward full speed.  (Max is 250)
 digitalWrite(DIR_A, LOW);
 analogWrite(SPEED_A, 250);

 // run motor for 34 seconds to close

 // stop motor
 analogWrite(SPEED_A, 0);

 // pause door closed 8 hour (minus runtime)


Mar 29, 2013, 03:57 pm Last Edit: Mar 29, 2013, 03:59 pm by afremont Reason: 1
When you open the serial terminal window, you need to make sure to set the baud rate at the bottom of the window to match the sketch.  The sketch says 57600 baud, the window defaults to 9600 so that may be the problem.

If your sketches compile cleanly, then it must be finding the libraries ok.
Experience, it's what you get when you were expecting something else.



Just looking at your code 

#include <Wire.h>
#include "RTClib.h"

This is probably way of mark but I thought "lib.h" indicated to the compiler to include from the working directory and <lib.h> to include from the compilers environment settings,  any way it is quick to check try #include <RTClib.h>.  I had an issue with an include where I thought it could not be wrong as surely the compiler would complain, it was the include and the compiler did not complain.

All the best



It is not clear from your posts whether the Adafruit RTC test sketch works when you try it.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


It is not clear from your posts whether the Adafruit RTC test sketch works when you try it.

Thank you for your reply UKHeliBob.  I cannot determine if the RTC is accepting the program because I cannot get anything on the computer screen to indicate that the RTC has received the program.  The LEDs on the UNO blink to indicate that the program is loaded.  Is there another way to determine that the program is working?  I'm so new to all of this stuff I do not know of other possible ways to check.

afremont, yes I have set the baud rate correctly at 57600.  Thanks.

Ian, I will try your suggestion when I get home.  Thanks.


So the test sketch is not working
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Most of us have strugled with RTC. Here is some code that I use, after a day of despair.

It is for a 20x4 LCD as well as serial monitor. You can strip out the LCD stuff.

Code: [Select]
//Arduino 1.0+ Only

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

LiquidCrystal_I2C lcd(0x27,20,4); 

void setup(){
      lcd.backlight();  //Backlight ON if under program control
  // Print a message to the LCD.
lcd.print("Today it is");


void loop(){


byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );

void printDate(){

  // Reset the register pointer

  byte zero = 0x00;

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

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

  Serial.print(" ");

  lcd.print("    ");
     if ((second) < 10)

  lcd.print("         ");



Here is a snapshot of the IDE screen after compiling.  You can see the different colors in the two "include" statements.  I'm sure these colors indicate something but I cannot find out what it means.  Any suggestions?

I think if you change "RTClib.h" to <RTClib.h> you should get the colours the same as <Wire.h>. The programme should then compile the same and work the same. I have just realised that, in the code I attached, I have "Wire.h". It works OK.


If it cannot find the .h file,   it will complain,  and your compilation won't work.

The difference between "rtclib.h"  and <rtclib.h>  is obscure.  Often,  either will work.

The only time a problem will arise,  is if you have two different versions of the same file,
and one of them is no good.


I have tried the program with the "  " and also with the < >  around the RTClib and neither will allow my laptop screen to print the clock functions.  I have adjuster the baud rate to match the sketch with no luck.  I have read many posts on several different websites with no success.  Without the ability to see (on the screen) the results of the uploaded sketch I don't know if my RTC is programmed with the correct time or not.

I even went back to some of my old learning sketches to see if the print-to-screen commands work OK.  They do.  I even experimented with changing the baud rates and everything seems to work OK.

I'm completely puzzled with nowhere to turn.  We're getting ready for planting season and the farm is very busy these days.  I was hoping to have the Arduino controlled door opener issues solved before the spring rush.  We're working 12 to 16 hours a day.  Very frustrating.

Is there anyone out there who can help me?  I think if I can just get something to work I could continue on my own.


Go Up