RTC returns unreadable Information

Hello

I am new to Arduino and have been reading the Book: Getting Started with Arduino (Massimo Banzi / Michael Shiloh).

In chapter 8 they show how to install and test an RTC on the analogue pins. A3 is set to HIGH and A2 set to LOW. The RTC return on the serial monitor: #⸮qr@q(⸮⸮a'⸮y⸮#⸮qr⸮"*I⸮PGa!m⸮⸮⸮⸮r⸮⸮M⸮cR⸮'⸮"⸮Qf⸮⸮⸮i⸮⸮Bg⸮a{⸮B⸮⸮B⸮r ⸮⸮⸮C⸮⸮

Here the sketch:
// RTC Test

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

RTC_DS1307 rtc;

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

void setup () {
// setting A3 to 5V an A2 to GRN for testing
pinMode(A3, OUTPUT);
pinMode(A2,OUTPUT);
digitalWrite(A3, HIGH);
digitalWrite(A2, LOW);

//official Sketch
while (!Serial); // for Leonardo/Micro/Zero

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(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('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
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 midnight 1/1/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 + TimeSpan(7,12,30,6));

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

Where could be the problem?

Thank you
moses

You told the Arduino to communicate using: Serial.begin(57600);

Did you also change the serial monitor baud rate to match?

Paul

Hi Paul

Thank you for the quick answer!

I guess I downloaded the wrong library.

I searched another one and it seems to work better. I needed a DS3231 instead of the DS1307.

Is it possible that this was the problem ?

Cheers,

moses

moserroger:
Hi Paul

Thank you for the quick answer!

I guess I downloaded the wrong library.

I searched another one and it seems to work better. I needed a DS3231 instead of the DS1307.

Is it possible that this was the problem ?

Cheers,

moses

No, because the strings your program tries to print come out garbage, according to your description.

Paul

it seems to work better.

What does "work better" mean, in this case?

Paul, you were right, I tried the ds1307 code one more time and realized that the baud setting in the serial monitor was wrong. Sorry!

Since yesterday I have been playing around a little bit with different libraries and they worked fine. Unfortunately I encountered a new difficulty. In one library I was able to set the day and the time but not the date.

  // Initialize the rtc object
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(MONDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(20, 11, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(12, 31, 2018);   // Set the date to January 1st, 2014
}

Original:

  // Initialize the rtc object
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014
}

Has anyone an idea what could be going wrong?

Thanks!

I encountered a new difficulty. In one library I was able to set the day and the time but not the date.Has anyone an idea what could be going wrong?

Please post complete code. Please provide a link to the library you are using.

Are you saying that the original example code to set the time/date to 12:00:00 on 1/1/2014 works properly, but when you try to set it to 20:11:00 on 12/31/2018 it does not? Does the library have a 12/24 hour time setting?

Hi Cattledog

This is the whole sketch:

// DS3231_Serial_Easy
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// A quick demo of how to use my DS3231-library to 
// quickly send time and date information over a serial link
//
// To use the hardware I2C (TWI) interface of the Arduino you must connect
// the pins as follows:
//
// Arduino Uno/2009:
// ----------------------
// DS3231:  SDA pin   -> Arduino Analog 4 or the dedicated SDA pin
//          SCL pin   -> Arduino Analog 5 or the dedicated SCL pin
//
// Arduino Leonardo:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 2 or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 3 or the dedicated SCL pin
//
// Arduino Mega:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL pin
//
// Arduino Due:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
//
// The internal pull-up resistors will be activated when using the 
// hardware I2C interfaces.
//
// You can connect the DS3231 to any available pin but if you use any
// other than what is described above the library will fall back to
// a software-based, TWI-like protocol which will require exclusive access 
// to the pins used, and you will also have to use appropriate, external
// pull-up resistors on the data and clock signals.
//

#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

void setup()
{
  // Setup Serial connection
  Serial.begin(115200);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {}
  
  // Initialize the rtc object
  rtc.begin();
  
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(MONDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(20, 11, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(12, 31, 2018);   // Set the date to January 1st, 2014
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating :)
  delay (1000);
}

After initial difficulties the last time I tested the code worked fine on my Arduino. I could also set the time. But for some reason the date would not adapt.

Link to the library:
http://www.rinkydinkelectronics.com/library.php?id=73

Thank you!

moses

//rtc.setDate(12, 31, 2018);
rtc.setDate(31, 12, 2018);

The format for that library is

setDate(uint8_t date, uint8_t mon, uint16_t year)

The serial easy library example for that library uses a confusing date.

 rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014

In general, many people have problems using the Rinky Dink library. A far better library in my opinion is Jack Christensens DS3232RTC.h. It is available through the library manager. It works for the DS3231. It integrates with the Time libary which you will also need to add.