How to get RTC from Teensy 4.0 (after power down, and the back up)

KrisKasprzak

Well-known member
What exactly does one have to do to set/get time from a Teensy 4.0 using it's built in RTC? I cannot get a 4.0 to retain date/time.

Teensy 4.0
Win 11
Arduino 2.3.10
Teensy 4.0 connected to nothing except CR2032 connected to VBAT and GND (tried GND and the other GND)
CR2032 coin cell with 3.0 volts
VUSB has NOT been cut

Upload this code
Code:
#include <TimeLib.h>

time_t tt;

void setup() {

  Serial.begin(115200);

  tt = rtc_get();
  Serial.printf("RTC: %s", ctime(&tt));
  setTime(tt);
  tt = now();
  Serial.printf("now: %s", ctime(&tt));

}

void loop() {

}

upload code and I get this, which I assume is getting the time from my PC--great, thanks.

RTC: Sun Jun 28 21:07:53 2026
now: Sun Jun 28 21:07:53 2026


unplug plug T4 and I time is not retained
RTC: Tue Jan 1 00:00:00 2019
now: Tue Jan 1 00:00:00 2019


I have tried this code as well
Code:
#include <TimeLib.h>

void setup() {
  // Start the serial port so we can print messages to the computer
  Serial.begin(9600);
 
  // Wait up to 5 seconds for the Serial Monitor to open
  while (!Serial && (millis() < 5000));

  // Tell the Time library to use the Teensy's internal RTC
  setSyncProvider(getTeensy3Time);

  // Check if the Teensy successfully read the time from the RTC
  if (timeStatus() != timeSet) {
    Serial.println("Warning: Unable to sync with the RTC!");
  } else {
    Serial.println("Success: RTC system time has been set!");
  }
}

void loop() {
  // Print the current digital time to the Serial Monitor every second
  printDigitalTime();
  delay(1000);
}

// Function to format and print the time nicely
void printDigitalTime() {
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print("/");
  Serial.print(month());
  Serial.print("/");
  Serial.print(year());
  Serial.println();
}

// Helper function to print a leading zero for single-digit minutes and seconds
void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10) {
    Serial.print('0');
  }
  Serial.print(digits);
}

// Core helper function that fetches the time directly from the Teensy hardware RTC
time_t getTeensy3Time() {
  return Teensy3Clock.get();
}


at code upload....
Success: RTC system time has been set!
21:24:04 28/6/2026
21:24:05 28/6/2026
21:24:06 28/6/2026
21:24:07 28/6/2026
21:24:08 28/6/2026


// power down power up
Success: RTC system time has been set!
0:00:00 1/1/2019
0:00:01 1/1/2019
0:00:02 1/1/2019



I have some dream code for my 3.2 that gets RTC and displays to the user, If loss if VBAT, the user can enter a menu to change day/year etc. Works like a million bucks.

None of that code works with my 4.0, I found some code in a thread--doesn't work for me. I simplified my 3.2 code and nothing

Sorry but I'm extremely frustrated with these 4.0's. I've spent weeks trying to get stuff migrated from a 3.2 but I find one brick wall after another.

Dead 4.0?

Anyone have an idea here?
 
It's been awhile but try this code:

Code:
  // This sets the system time (NOT the Teensy RTC Clock)
  setTime(tt); // seting the System Time Library only here.
  // now we can use the system time to update the Teensy's RTC bits
  // This sets the RTC Clock from System Time Library - epoch stylee, just like it wants :)
  Teensy3Clock.set(now());
 
It should get automatically set when you upload from a PC. The output you're getting the first time you run the first sketch shows this. It's just not keeping it when powered off - are you sure your battery is good?
 
Is it possible the battery holder has been wired backwards (red/black wires swapped)?
Check at T4.0 VBAT pin to verify battery voltage is present and correct polarity.
 
@BriComp OK I missed that, but battery may easily be down as Teensy RTC (I beleave also 4.0) is very power hungry. Much more than T3.2
 
Yes, that's why I always use RV-3928_C7. I've had one going for years on the original battery. Extremely accurate as well.
 
The Teensy has TWO clocks. The Teensy clock and the RTC clock.
These need to be synched.

In your setup put
Code:
setSyncProvider(getTeensy3Time);
I believe that this links the two clocks together.

To get the time when running use
Code:
time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}
Of course you need to check that the RTC has been set with a time and if not do it!
 
Back
Top