Strugling with RTC

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:

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.

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 . . .

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

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.

// 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
  delay(34000);
 
 // stop motor 
  analogWrite(SPEED_A, 0);

  // pause door open 16 hours (minus runtime)
  delay(57566000);

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

  // run motor for 34 seconds to close
  delay(34000);
 
  // stop motor 
  analogWrite(SPEED_A, 0);

  // pause door closed 8 hour (minus runtime)
  delay(10000);
  
}

Anybody out there?

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.

Hi,

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

Ian

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

UKHeliBob:
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

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.

//Arduino 1.0+ Only

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

LiquidCrystal_I2C lcd(0x27,20,4);  

void setup(){
  Wire.begin();
  Serial.begin(9600);
  lcd.init();  
    delay(1000);
 lcd.clear();
      lcd.backlight();  //Backlight ON if under program control
      lcd.setCursor(0,0); 
  // Print a message to the LCD.
 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(10,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(10,2);
  
  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);
  lcd.print("    ");
  
  lcd.setCursor(0,3);
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
     if ((second) < 10)
  {
    lcd.print("0");
  };
   lcd.print(second);

  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.

Thanks.