Help with Teensy 4.1 RTC Initialization

Math51

New member
Hi everyone,

I am working on a new project with the Teensy 4.1 and will need to use the Real Time Clock (RTC). This is my first time working with the RTC on this board.

I am developing my code in the PlatformIO IDE, using the Arduino framework for the Teensy. To test the RTC features, I wrote a simple program that reads the RTC's hours, minutes, and seconds every second and sends these values to the serial port. The code works fine, and I can see the time values in the terminal.

However, the displayed values show that the RTC starts at 0. According to the Teensy 4.1 information page (https://www.pjrc.com/store/teensy41.html), it states:
"Teensy Loader automatically initializes the RTC to your PC's time while uploading."
I expected the RTC to be initialized to the same date and time as my PC.

Did I misunderstand the Teensy documentation? Or is there a specific configuration required in the Teensy Loader to initialize the RTC to the PC's date and time?

Any advice or clarification would be greatly appreciated!

Thanks in advance for your help!

Math
 
Without code, hard to correctly diagnose but...here's some stuff to think about:

Teensy Loader will initialize the RTC clock when you program the Teensy and maintain power. If you power it down and up again, without a battery maintaining the RTC, it will reset to zero.

You also will need to have in setup():

Code:
setSyncProvider(get_TeensyTime);

and implement:

Code:
time_t get_TeensyTime(void) {
  return Teensy3Clock.get();
}
 
Thank you for your response!

Regarding your point:
Teensy Loader will initialize the RTC clock when you program the Teensy and maintain power.
I observe the time values in the serial terminal right after uploading the program, so the Teensy has not been powered down and back up.

Here is the program I am using for testing:
C++:
#include <Arduino.h>
#include <TimeLib.h>

// Variable globale pour mémoriser l'état de la LED
bool ledState = false;

void setup() {
  // put your setup code here, to run once:
  // Initialisation du port série
  Serial.begin(115200);

  // Initialisation de la LED intégrée
  pinMode(LED_BUILTIN, OUTPUT);

  // Éteindre la LED au démarrage
  digitalWrite(LED_BUILTIN, LOW); 
}

void loop() {
  // put your main code here, to run repeatedly:
  // Vérifie si le port série est prêt
  if (Serial) {
    // Affiche l'heure, les minutes et les secondes au format HH:MM:SS
    Serial.print("Heure : ");
    Serial.print(hour());
    Serial.print(":");
    Serial.print(minute());
    Serial.print(":");
    Serial.println(second());
    }

  // Inverser l'état de la LED
  ledState = !ledState; // Si true devient false, et inversement
  digitalWrite(LED_BUILTIN, ledState ? HIGH : LOW);

  // Attendre une seconde
  delay(1000);
}

Could the issue be related to something I am missing in my code? Or is there something else I need to configure in the Teensy Loader to make the RTC sync properly?

Thanks again for your assistance!

Math
 
@Math51: You're still missing the two additions that @beermat spelled out at the bottom of his post. I can attest to the fact that, if you make these two additional changes to your sketch (in addition to attaching the required battery), the T4.1 RTC will then keep time correctly, even when primary power is removed.

Hope that helps . . .

Mark J Culross
KD5RXT
 
Back
Top