Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: Teensy TimeLib question ...

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    9

    Teensy TimeLib question ...

    Good afternoon all.

    I have been experimenting with the TimeLib library example with a view to displaying the time & date in an application I am devloping.
    I find that I can get TimeLib to synchronise with the host PC correctly and hence display, and keep up with the PC time.

    However, that seems to ONLY work when the code is freshly uploaded into the Teensy (4.1) which then then rebooted by the IDE. If I close down the IDE, and then reboot the Teensy, the current time is not picked up - instead the time defaults to the UNIX initial date (01/01/1970 00:00:00).

    So has anyone any hints on what must be done to enable the time to be picked up from a PC when the Teensy has not booted from a fresh program load via the IDE?

    Thanks a lot ...

    Kenneth Spencer

  2. #2
    Junior Member
    Join Date
    Sep 2020
    Posts
    19
    Vbat pin
    if you connect the Vbat pin to the + side of a 3v coin battery, and connect the ground pin to the - side of the battery, it will use the coin battery to keep the time.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    9
    Quote Originally Posted by Ed70 View Post
    Vbat pin
    if you connect the Vbat pin to the + side of a 3v coin battery, and connect the ground pin to the - side of the battery, it will use the coin battery to keep the time.
    Thank you Ed ...

    ... the issue is not really keeping the Teensy Time going. It is the fact that only if the program is started by an upload from the IDE does the TimeLib appear to collect the time from the host PC and using for getting the correct time. Following a startup under those circumstances all is well.

    BUT if the Teensy is started by simply being connected to the PC (i.e. no reboot under contol of the IDE) then it never picks up the current time, instead starting from UNIX time-zero.

    I may be able to cut the code down to managable size and post it here for someone to try. I suspect that the Teensy is only in touch with the PC when the IDE, and possibly the Serial Monitor is around for the PC link. Maybe we need some other means of comms with the PC in order to pick up the time from the PC.

    Kenneth Spencer

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,088
    TimeLib defaults to starting at 0 and is meant to sync to a source of accurate time.

    Teensy has a RTC which keeps track of date/time. That RTC is set automatically to your PC's time on every upload. But if it loses power and you don't have a coin cell connected to VBAT, then of course it has to default to something when power comes back up and no communication has happened to a PC.

    TimeLib can sync to Teensy's RTC. This doesn't happen automatically, because TimeLib is designed to work on all Arduino compatible boards and be able to sync to all sorts of time sources, so there isn't any default sync'd source. You have to cause TimeLib to sync to Teensy's RTC in your code. In Arduino, click File > Examples > Time > TimeTeensy3 for an example.

  5. #5
    Junior Member
    Join Date
    Jul 2020
    Posts
    9
    Quote Originally Posted by PaulStoffregen View Post
    TimeLib defaults to starting at 0 and is meant to sync to a source of accurate time.

    Teensy has a RTC which keeps track of date/time. That RTC is set automatically to your PC's time on every upload. But if it loses power and you don't have a coin cell connected to VBAT, then of course it has to default to something when power comes back up and no communication has happened to a PC.

    TimeLib can sync to Teensy's RTC. This doesn't happen automatically, because TimeLib is designed to work on all Arduino compatible boards and be able to sync to all sorts of time sources, so there isn't any default sync'd source. You have to cause TimeLib to sync to Teensy's RTC in your code. In Arduino, click File > Examples > Time > TimeTeensy3 for an example.
    Thank you Paul, for that reply. It is what I expected, so I will see what I can do with the TimeTeensy3 code. I have already examined that code, but I think that by default it relies on getting the time with the upload as it doesn't pick the time up otherwise. So I will have a second look.

    In the meantime, thanks, as always, for your help.

    Kenneth Spencer

  6. #6
    Junior Member
    Join Date
    Mar 2021
    Posts
    3

    A simple but frustrating (S)RTC objective

    Using a 4.0 with Vbat powered (and verified), I am trying to programatically fetch the current time from the RTC, subtract 1 hour from it, store the adjusted time back to the RTC, remove USB power (not Vbat), reconnect USB power (no code is uploaded) and observe that the RTC is still 1 hour behind. No matter what I do, it always seems to be that the correct, unadjusted time is in the RTC. It is as though the 1 hour subtraction did not occur. I am not trying to get my code debugged for me, but rather to find a simple, single function example of how to do this. Any help would be appreciated...

  7. #7
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,154
    Quote Originally Posted by qqwasder View Post
    Using a 4.0 with Vbat powered (and verified), I am trying to programatically fetch the current time from the RTC, subtract 1 hour from it, store the adjusted time back to the RTC, remove USB power (not Vbat), reconnect USB power (no code is uploaded) and observe that the RTC is still 1 hour behind. No matter what I do, it always seems to be that the correct, unadjusted time is in the RTC. It is as though the 1 hour subtraction did not occur. I am not trying to get my code debugged for me, but rather to find a simple, single function example of how to do this. Any help would be appreciated...
    Please give the exact code to reproduce this observation.

  8. #8
    Junior Member
    Join Date
    Mar 2021
    Posts
    3
    Quote Originally Posted by WMXZ View Post
    Please give the exact code to reproduce this observation.
    Thanks for the quick reply. Development is on latest vs code and platformio. With Vbat at 3.3v, after uploading this code at 8AM, before shows 8:00:00, after after 7:00:00. Removing USB power and restoring it (but NOT uploading) it again shows 8:00:00 and 7:00:00 plus the delay(5000) (to do the power on/off). I was expecting before to be 7:00:00 (or so). What I am trying to do is, say, adjust for daylight savings time WITHOUT access to a PC (perhaps using a button). Thanks for the help...Jeff
    -------------------------------------------------------------------------------------
    #include <Arduino.h>
    #include <TimeLib.h>

    void setup() {
    Serial.begin(115200);
    while(!Serial);
    delay(5000);
    time_t t = Teensy3Clock.get();
    setTime(t);
    Serial.printf("Teensy 4.0 clock before: %d:%02d:%02d\n",hour(),minute(),second());
    t -= 60*60;
    setTime(t);
    Serial.printf("Teensy 4.0 clock after: %d:%02d:%02d\n",hour(),minute(),second());
    }
    void loop() {
    Serial.printf("Teensy 4.0 clock reads: %d:%02d:%02d\n",hour(),minute(),second());
    delay(1000);
    }

  9. #9
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,447
    Your code doesn't write the changed time back to the RTC.
    This one should work:
    Code:
    #include <Arduino.h>
    #include <TimeLib.h>
    
    void setup()
    {
        while (!Serial) {}
    
        time_t t = Teensy3Clock.get();                                                        // Read out the current RTC time...
        setTime(t);                                                                           // ... and store it to the system clock
        Serial.printf("Teensy 4.0 clock before: %d:%02d:%02d\n", hour(), minute(), second()); 
        t -= 60 * 60;                                                                         // subtract 1 hour
        setTime(t);                                                                           // and write new time to the system clock
        Teensy3Clock.set(t);                                                                  // <----  WRITE THE NEW TIME BACK TO THE RTC
        Serial.printf("Teensy 4.0 clock after: %d:%02d:%02d\n", hour(), minute(), second());
    }
    void loop()
    {
        Serial.printf("Teensy 4.0 clock reads: %d:%02d:%02d\n", hour(), minute(), second());
        delay(1000);
    }

  10. #10
    Junior Member
    Join Date
    Mar 2021
    Posts
    3
    Thanks luni, that add fixed all my problems with this code. Really appreciated!...Jeff
    --------------------------------------------------------
    Quote Originally Posted by luni View Post
    Your code doesn't write the changed time back to the RTC.
    This one should work:
    Code:
    #include <Arduino.h>
    #include <TimeLib.h>
    
    void setup()
    {
        while (!Serial) {}
    
        time_t t = Teensy3Clock.get();                                                        // Read out the current RTC time...
        setTime(t);                                                                           // ... and store it to the system clock
        Serial.printf("Teensy 4.0 clock before: %d:%02d:%02d\n", hour(), minute(), second()); 
        t -= 60 * 60;                                                                         // subtract 1 hour
        setTime(t);                                                                           // and write new time to the system clock
        Teensy3Clock.set(t);                                                                  // <----  WRITE THE NEW TIME BACK TO THE RTC
        Serial.printf("Teensy 4.0 clock after: %d:%02d:%02d\n", hour(), minute(), second());
    }
    void loop()
    {
        Serial.printf("Teensy 4.0 clock reads: %d:%02d:%02d\n", hour(), minute(), second());
        delay(1000);
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •