RTC library error

As part of learning how to show real clock times I found and ran the sketch 'interrupts1Hz' in my Sketches folder:

C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\libraries\RTClib\examples\interrupts1Hz\interrupts1Hz.ino

It ran OK.

But after saving it with a new name (as it insisted on) it loaded but immediately gave this message in the serial monitor: Couldn't find RTC

What mistake or misunderstanding is causing that please?

The RTC library file is located here:

C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\libraries\RTClib\RTClib.h

My newly saved sketch folder is here:

C:\Users\terry\Dropbox\Electronics\Arduino\SKETCHES\My Sketches\TIME\interrupts1Hz-Terry-1

And the original code (apart from my change of baud to 115200) is here:

/***********************************************************************

    Combining RTClib with the avr-libc timing functions
    ===================================================

The standard way of getting the current time and date with RTClib is to
call the now() method of the appropriate RTC class. This, however, is
somewhat slow, as it involves communicating with the RTC through the I2C
bus. An alternative, more lightweight method involves configuring the
RTC to deliver one pulse per second to an interrupt pin, and counting
the seconds in the interrupt handler. The timekeeping is then entirely
handled in the Arduino, with no I2C communication with the RTC other
than during the initialization phase.

On AVR-based Arduinos (Uno, Nano, Micro, ...), the Arduino core library
is built on top of avr-libc, which is an implementation of the standard
C library for the AVR platform. This library provides the standard C
functions for handling time:[1] time(), gmtime(), mktime(), etc. The
time() function is normally used to retrieve the current time from the
operating system, but since we have no operating system, the avr-libc
provides its own non-standard functions for implementing a time source:

 - set_system_time() initializes the library's idea of the current time
 - system_tick() steps the system time by one second.

This sketch demonstrates how to combine RTClib and avr-libc in order to
handle the timekeeping entirely on the Arduino from an interrupt
delivered by the RTC:

 - RTClib is used to configure the RTC and retrieve the initial time
 - avr-libc is used for regular timekeeping

This sketch only works on AVR-based Arduinos, as it relies on
non-standard functions provided by avr-libc.

[1] https://www.nongnu.org/avr-libc/user-manual/group__avr__time.html

***********************************************************************/

#include <RTClib.h>
#include <time.h>  // standard C timing functions

// Pin receiving the one-pulse-per-second signal from the RTC.
// This should be an interrupt-capable pin.
const uint8_t pin1pps = 2;

// We will use the PCF8523 RTC, which has the handy "Second Timer".
// Other RTCs could be used with their "square wave" output configured
// to 1 Hz.
RTC_PCF8523 rtc;

void setup() {

  // Initialize the serial port.
  Serial.begin(115200);
  while (!Serial); // wait for serial port to connect. Needed for native USB

  // Initialize the RTC.
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
  if (!rtc.initialized() || rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  rtc.deconfigureAllTimers();  // undo previous configuration, if any

  // Initialize the system time from the RTC time. Both avr-libc and
  // DateTime::secondstime() use the start of year 2000 as their
  // reference "epoch".
  set_system_time(rtc.now().secondstime());

  // Keep the time in sync using the one-pulse-per-second output of the
  // RTC as an interrupt source and calling system_tick() from the
  // interrupt service routine.
  pinMode(pin1pps, INPUT_PULLUP);
  rtc.enableSecondTimer();
  attachInterrupt(digitalPinToInterrupt(pin1pps), system_tick, FALLING);
}

void loop() {

  // From here on, we only use the standard C timing functions.
  // time() returns the current time as a single number of type time_t,
  // this is the number of seconds elapsed since a reference "epoch".
  time_t now = time(nullptr);

  // gmtime() converts the time to a broken-down form (year, month...)
  // similar to the DateTime class. Unlike localtime(), it doesn't
  // attempt timezone conversions.
  struct tm *broken_down_time = gmtime(&now);

  // asctime() returns a textual representation of the date and time as
  // a C string (pointer to a character array). The format is similar to
  // the DateTime::toString() format "DDD MMM DD hh:mm:ss YYYY".
  Serial.println(asctime(broken_down_time));

  delay(1000);
}

What sort of RTC have you connected and how is it connected to what sort of Arduino?

Terrypin:
But after saving it with a new name (as it insisted on) it loaded but immediately gave this message in the serial monitor: Couldn't find RTC

That message is coming from the code running on the arduino, not the compiler. It means that the arduino could not properly communicate with the RTC when it tried to initialize it, probably because of a wiring error. If the message were referring to problems finding the RTC library, you would get the error while compiling in the IDE, and would never get to the point of loading the code onto the arduino board.

countrypaul:
What sort of RTC have you connected and how is it connected to what sort of Arduino?

Duh - I didn't realise I had to connect a physical Real Time Clock!

That was because it ran OK with the original. However , I cannot now repeat that !Just get the same error.

Well at least it was easy to find the cause of the problem!

Terrypin:
Duh - I didn't realise I had to connect a physical Real Time Clock!

That was because it ran OK with the original. However , I cannot now repeat that !Just get the same error.

You didn't answer the question.

Look in the examples and see if there is a sketch for rtc_millis, that will run a softwate rtc and let you use the library functions without a hardware rtc.

The example sketch is called softrtc in Adafruit's version of rtclib.

There is probably nothing wrong with the sketch. If you doubt that, run some RTC example sketch. The problem is likely to be in your wiring, or a module defect.

aarg:
There is probably nothing wrong with the sketch. If you doubt that, run some RTC example sketch. The problem is likely to be in your wiring, or a module defect.

Or the fact he didn't know he has to attach and RTC as it is not integral.

Terrypin:
Duh - I didn't realise I had to connect a physical Real Time Clock!

That was because it ran OK with the original. However , I cannot now repeat that !Just get the same error.

Did you have the serial monitor set to the correct baud rate at that time? If not, the serial monitor may have shown random characters, or nothing at all, because of the baud rate mismatch.

david_2018:
Did you have the serial monitor set to the correct baud rate at that time? If not, the serial monitor may have shown random characters, or nothing at all, because of the baud rate mismatch.

He reported he got the message "Couldn't find RTC" which is issued by the code on the Arduino when it attempts to start the RTC. Since he had no RTC attached not surprising that he got that message.

I think when someone says,

Duh - I didn't realise I had to connect a physical Real Time Clock!

You can assume that they connected one before proceeding...

aarg:
I think when someone says,You can assume that they connected one before proceeding...

Perhaps, but not in this case! I don't have a 'RTC'. I assumed the message was referring to the library of that name.

david_2018:
Did you have the serial monitor set to the correct baud rate at that time? If not, the serial monitor may have shown random characters, or nothing at all, because of the baud rate mismatch.

Thanks. I do often see those random characters, but in this case the baud rates were matched.

There is some confusion about where you now stand. You haven't provided any details about the hardware wiring, which is probably the problem anyway...

aarg:
There is probably nothing wrong with the sketch. If you doubt that, run some RTC example sketch. The problem is likely to be in your wiring, or a module defect.

This was an 'RTC example sketch' as far as I was concerned. It doesn't work because I now realise the message 'Couldn't find RTC' meant 'Couldn't find RTC module'. A tad too cryptic for me.

aarg:
There is some confusion about where you now stand. You haven't provided any details about the hardware wiring, which is probably the problem anyway...

There is no hardware! Apart, of course, from a UNO, necessary to upload the sketch.

That sketch was found by searching my \Sketches folder for *.ino files containing the string 'Time.h'.

I'm simply looking for a way to log (i.e. to serial print) the time at which various overnight events occur in several projects.

You can run a software rtc, but that will require that you add code so that you can set the time from the serial monitor.

There is an option to have the serial monitor add a timestamp to each line, that may satisfy your needs.

aarg:
I think when someone says,

Duh - I didn't realise I had to connect a physical Real Time Clock!

You can assume that they connected one before proceeding...I think when someone says,You can assume that they connected one before proceeding...

I would have thought that if they say that, in reeponse to be asked what is it, that it would be pretty obvious they did not have one.

This may be a language/cultural thing - where is everyone on this thread from?
I'm from Yorkshire, England.
My guess is that Terrypin is also from the UK, but Aarg I would think US, and David probably US also.

No I'm from Canada. We speak both US and UK English. :slight_smile: