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
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
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?
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?