Go Down

Topic: RTC not working correctly (Read 178 times) previous topic - next topic

xmaureenr

Hi all!

I'm trying to make my Real Time Clock working, but it's not using the correct time. For example: it's 17:25 in real time and it's using 17:10. The clock is correctly counting the seconds, but it's not the actual time.

Does anyone have experience with the RTC and knows what's going wrong?

Thanks for your help!

This is the code I used ...

//rtc----------
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

#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
#endif

RTC_DS1307 rtc;

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

//lcd---------
#include <LiquidCrystal.h>
#define ROTARY_ANGLE_SENSOR A0

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//------------

void setup () {

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

  Serial.begin(57600);
  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(2017, 10, 3, 17, 40, 0));
    // 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));
  }

  lcd.begin(16, 2);
  lcd.print("hello world!");
}

void loop () {
  DateTime now = rtc.now();
  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.setCursor(3, 1);
  lcd.print(now.minute(), DEC);
  delay(500);
}

dannable

Obvious question first - which RTC are you using?
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Nick_Pyner

Nothing is clear about what you are trying to do, I assume you have a DS1307 and recklessly assume you are using a normal arduino.  If that is the case, and you are prepared to focus on the problem as stated, you might find the following useful.
http://bildr.org/2011/03/ds1307-arduino/
Note that it uses two separate programmes, and it works with DS3231 as well.

xmaureenr

I'm using a 'Tiny RTC I2C modules', I guess it's the same as a DS1307 and a Seeduino.

Nick_Pyner

The Tiny RTC modules I have use a DS1307.

cattledog

How have you set the time on the rtc?

The code you posted only sets the time if the rtc is not running. Yours is running.

Code: [Select]
  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(2017, 10, 3, 17, 40, 0));
    // 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));
  }


Move the rtc.adjust() statement outside of the if() block, run the code once to set the time, then comment out tjhe rtc.adjust() line so that it won't reset the time every time the code restarts.

Go Up