Simple RTC Timer Returning 0's for despite Read Test Example returning correct date/time

I'm working with this code to automate a small door on a timer using a DS1307 RTC; I have set the RTC time using the SetTime Example within the DS1307 RTC Examples and it returns the correct time when checked with ReadTest Example. However, when I load my program onto the Arduino, any Serial.print data returns 00:00:00 1/1/1970 as though the rtc is not responsive. I'm a super green beginner and borrowed this code from somewhere else and modified it to work with my RTC, but is there something obvious I'm missing? The functions for the Button Open and Close work fine, only the timer sections wont work.

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

#define DS1307_CRTL_ID 0x68
RTC_DS1307 rtc;

/*
 * GND - GND
 * VCC - 5v
 * SDA - SDA
 * SCL - SCL
 */

#define RELAY1  2
#define RELAY2  3

tmElements_t tm;
int dOpen = 1;
int dClose = 0;

#define BUTTONOp        8  // Button
#define BUTTONCl        9  // Button

void setup()
{
  // Initialize the rtc object
rtc.begin();
Serial.begin(9600);

  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  digitalWrite(RELAY1,HIGH);
  digitalWrite(RELAY2,HIGH);

  pinMode(BUTTONOp, INPUT);  digitalWrite(BUTTONOp, HIGH);
  pinMode(BUTTONCl, INPUT);  digitalWrite(BUTTONCl, HIGH);

}

void loop()
{
  
  if (digitalRead(BUTTONOp) == 0)
  {
      //Open Door button
      digitalWrite(RELAY1,LOW);
      digitalWrite(RELAY2,HIGH);
      Serial.println("Button Open");
  }
  else if (digitalRead(BUTTONCl) == 0)
  {
      //Close Door button
      digitalWrite(RELAY1,HIGH);
      digitalWrite(RELAY2,LOW);
      Serial.println("Button Close");
  }
  else if (tm.Hour == 7 && (tm.Minute == 00 || tm.Minute == 1))
  {
      //Open Door timer
      digitalWrite(RELAY1,LOW);
      digitalWrite(RELAY2,HIGH);
      Serial.println("Door Open");
      
  }
  else if (tm.Hour == 19 && (tm.Minute == 00 || tm.Minute == 1))
  {
      //Close door timer
      digitalWrite(RELAY1,HIGH);
      digitalWrite(RELAY2,LOW);
      Serial.println("Door Close");
  }
  else
  {
    //Stop
    digitalWrite(RELAY1,HIGH);
    digitalWrite(RELAY2,HIGH);

  }
}

Hi, welcome to the forum.

I'm having mixed feelings about the sketch. I mean, the sketch seems to be a mix between the Adafruit RTClib and the TimeLib DS1307RTC library with ReadTest.ino example.

Which RTC library do you want to use ?

  1. Adafruit RTClib : https://github.com/adafruit/RTClib
  2. TimeLib : https://www.pjrc.com/teensy/td_libs_Time.html

These two are the most common RTC libraries for a long time, but there are other options.
Some newer boards have a RTC build in the processor, and there are official Arduino time libraries for those.

When you show a sketch on this forum, please use code tags. Put the sketch between lines with three backslash-single-quotes or use the </> button.

```
Your sketch
```

You could edit your first post and fix that.

Don't you have to call a function to fetch the time from the RTC chip before you have the values to use? Look at the examples for the RTC library you are using.

Alright, new code below; NOW I get proper timing when the arduino is plugged in via USB, but once unplugged and running on independent 5V power I have the same issue as before. I can't use the buttons now though unless plugged in, but if the relay is switched on (and its indicator light is on) when unplugged it will remain on until plugged back in outside of the conditions of the else if statement. Battery voltage is above 5V

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

#define DS1307_CRTL_ID 0x68
RTC_DS1307 rtc;

/*
 * GND - GND
 * VCC - 5v
 * SDA - SDA
 * SCL - SCL
 */

#define RELAY1  2
#define RELAY2  3

tmElements_t tm;
int dOpen = 1;
int dClose = 0;

#define BUTTONOp        8  // Button
#define BUTTONCl        9  // Button

void setup()
{
//Serial.begin(9600);

  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  digitalWrite(RELAY1,HIGH);
  digitalWrite(RELAY2,HIGH);

  pinMode(BUTTONOp, INPUT);  digitalWrite(BUTTONOp, HIGH);
  pinMode(BUTTONCl, INPUT);  digitalWrite(BUTTONCl, HIGH);

}

void loop()
{
  if(RTC.read(tm)){
    
  if (digitalRead(BUTTONOp) == 0)
  {
      //Open Door button
      digitalWrite(RELAY1,LOW);
      digitalWrite(RELAY2,HIGH);
      //Serial.println("Button Open");
  }
  else if (digitalRead(BUTTONCl) == 0)
  {
      //Close Door button
      digitalWrite(RELAY1,HIGH);
      digitalWrite(RELAY2,LOW);
      //Serial.println("Button Close");
  }
  else if (tm.Hour == 7 && (tm.Minute == 58 || tm.Minute == 59))
  {
      //Open Door timer
      digitalWrite(RELAY1,LOW);
      digitalWrite(RELAY2,HIGH);
      //Serial.println("Door Open");
      
  }
  else if (tm.Hour == 19 && (tm.Minute == 00 || tm.Minute == 1))
  {
      //Close door timer
      digitalWrite(RELAY1,HIGH);
      digitalWrite(RELAY2,LOW);
      //Serial.println("Door Close");
  }
  else
  {
    //Stop
    digitalWrite(RELAY1,HIGH);
    digitalWrite(RELAY2,HIGH);
  }
  }
}

Your sketch won't check the button unless you get a successful read of the RTC. You just have to figure out why the RTC stops working when you disconnect from USB.

1 Like

Most likely, the relays are taking too much power? What is your external 5V supply? How much current do your relays consume?

external 5V supply is 3A 15W stepped down from a 12V battery; I've tried disconnecting the RTC and using just the relay which works but I can't get the RTC to control w/o being connected to the computer. It's only a 4 relay chip with a 10 A max on the switch side.

Time for a schematic - hand drawn is okay. Fritzing is not

Scan2021-11-01_124658.pdf (504.9 KB)
forgive my drafting skills, but this is what my setup looks like independent of the USB connection

Edit: FWIW, I've now tried connecting both the 5V power from the transformer AND a wall outlet USB/120V connector at the same time and THIS works without connection to the computer. Definitely an amperage issue. Using a 5V breadboard power supply (with a 9V 700mA AC/DC convertor) connected directly to the relay doesn't supply enough power to run the relay, which makes even less sense to me.

Looks like you are connecting a 5V supply to the power input jack. You are supposed to use at least 7V on that jack.

The power input jack has a protection diode that will drop the voltage by about 0.6V to 4.4V. Your 4.4V then goes into the 5V regulator which needs over 6V in to get regulated 5V out. I suspect your "+5V" pin is somewhere under 4V.

To use an external 5V supply, connect it to GND and +5V, bypassing the internal 5V regulator.

1 Like

This was the issue, I gave up on my battery power and just have it powered by a 9V AC/DC convertor for now. Either way, code works. Thanks for your help!