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

Thread: Can't get RTC to save time and remember on power up

  1. #1
    Senior Member
    Join Date
    Apr 2017
    Posts
    187

    Can't get RTC to save time and remember on power up

    All,

    I have a Teensy 3.2 with a soldered crystal and when powered via usb it seems to store and display correct time.

    I have a 3 volt batter + connected to the VBAT and ground connected to GND (the pin next to pin 0 NOT the pin at the end of the board). Both GND's appear to be connected.

    I can program the time, but upon disconnect of power (either USB or by power at 3.3 PIN), the programmed time is not stored. Upload the attached code where I have a setTime with arbitrary time. then comment the setTime upload and see how time is not stored.

    How to persist time using setTime?

    Code:
    #include <TimeLib.h>
    
    time_t RTCTime;
    
    
    void setup() {
    
      Serial.begin(9600);
      setSyncProvider(getTeensy3Time);
    
      // setTime(hr,min,sec,day,month,yr);
    
      //  comment and see if saved data sticks--it will not, so how do you save dates and time?
      setTime(3, 3, 3, 8, 19, 2019);
    
    }
    
    void loop() {
    
      Serial.print(hour());
      Serial.print(":");
      Serial.print(minute());
      Serial.print(":");
      Serial.print(second());
      Serial.print(" ");
      Serial.print(day());
      Serial.print("/");
      Serial.print(month());
      Serial.print("/");
      Serial.print(year());
      Serial.println();
    
      delay(1000);
    
    }
    
    time_t getTeensy3Time() {
      return Teensy3Clock.get();
    }

  2. #2
    Senior Member
    Join Date
    Apr 2017
    Posts
    187
    OK the plot thickens...while I eagerly wait for some assistance in manually programming the RTC and saving the setting, I ran a sketch to program the date/time based on the serial data from the PC--this seems to save and mimic the time setting from my PC. however....If I power down my teensy wait several minutes, and power back it up (not with USB but 3v3 connected to 3v3 pin), the date and time is what was just before power down--meaning it will not keep updating time during power off.

    SO... how is the RTC on a teensy 3.2 supposed to work w/o USB connection? Or is it?

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,773
    Indeed either GND pin is the same and will work.

    It seems the battery is not properly presenting 3V to the VBat pin? Without good VBat connected it will run the RTC when powered.

    Uploaded the code to a Teensy 4 - where TeensyLoader sets the time on that device, and it has a CR2032 putting 3V to VBat.

    So removing the setTime() code properly uses the set or stored time on this T4.

    edit for crosspost: How/Why is 3V3 presented on a USB powered device? Unless VIN<>VUSB trace is cut that should not be.

  4. #4
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,269
    Quote Originally Posted by KrisKasprzak View Post
    OK the plot thickens...while I eagerly wait for some assistance in manually programming the RTC and saving the setting, I ran a sketch to program the date/time based on the serial data from the PC--this seems to save and mimic the time setting from my PC. however....If I power down my teensy wait several minutes, and power back it up (not with USB but 3v3 connected to 3v3 pin), the date and time is what was just before power down--meaning it will not keep updating time during power off.

    SO... how is the RTC on a teensy 3.2 supposed to work w/o USB connection? Or is it?
    It has worked for me in the past, when I hooked up the RTC and soldered in the crystal. I would look into the solder joints on the crystal and Vbat/Ground. I dunno, maybe the crystal suffered from too much heat while soldering, and no longer works?

    Though I did learn not to use rechargeable coin cell batteries. Those don't last too long.

  5. #5
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,201
    i think your sketch needs Teensy3Clock.set() to set the RTC time. See File > Examples > Time > TimeTeensy3

    https://www.pjrc.com/teensy/td_libs_Time.html

    a sketch setting RTC from compile TIME DATE
    Code:
    /*
       TimeRTC.pde
       example code illustrating Time library with Real Time Clock.
       K66/K64 have RTC crystal
    */
    
    #include <TimeLib.h>
    
    // Unixtimeseconds from 1. January 1970  00:00:00
    // to 1. January 2000   00:00:00 UTC-0
    #define SECONDS_FROM_1970_TO_2000 946684800
    
    int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    time_t getTeensy3Time()
    {
      return Teensy3Clock.get();
    }
    void setup() {
      timefromcompiler(__DATE__, __TIME__);
      // set the Time library to use Teensy 3.0's RTC to keep time
      setSyncProvider(getTeensy3Time);
    
      Serial.begin(115200);
      while (!Serial);  // Wait for Arduino Serial Monitor to open
      delay(100);
      if (timeStatus() != timeSet)
        Serial.println("Unable to sync with the RTC");
      else
        Serial.println("RTC has set the system time");
    }
    
    void loop() {
      digitalClockDisplay();
      delay(1000);
    }
    
    void digitalClockDisplay() {
      // digital clock display of the time
      printDigits(hour());
      Serial.print(":");
      printDigits(minute());
      Serial.print(":");
      printDigits(second());
      Serial.print(" ");
      printDigits(day());
      Serial.print(".");
      printDigits(month());
      Serial.print(".");
      Serial.println(year());
    }
    
    void printDigits(int digits) {
      // utility function for digital clock display: prints preceding colon and leading 0
      if (digits < 10)
        Serial.print('0');
      Serial.print(digits);
    }
    
    int conv2d(char* p) {
      int v = 0;
      if ('0' <= *p && *p <= '9')
        v = *p - '0';
      return 10 * v + *++p - '0';
    }
    
    void timefromcompiler(char* date, char* time) {
      int _days, _month, _year, _hour, _minute, _second;
      uint32_t _ticks;
    
      //Day
      _days = conv2d(date + 4);
    
      //Month
      switch (date[0]) {
        case 'J': _month = date[1] == 'a' ? 1 : _month = date[2] == 'n' ? 6 : 7; break;
        case 'F': _month = 2; break;
        case 'A': _month = date[2] == 'r' ? 4 : 8; break;
        case 'M': _month = date[2] == 'r' ? 3 : 5; break;
        case 'S': _month = 9; break;
        case 'O': _month = 10; break;
        case 'N': _month = 11; break;
        case 'D': _month = 12; break;
      }
    
      //Year
      _year = conv2d(date + 9);
    
      //Time
      _hour = conv2d(time);
      _minute = conv2d(time + 3);
      _second = conv2d(time + 6);
    
      //Date & Time to Unixtime
      for (int i = 1; i < _month; ++i)
        _days += daysInMonth[i - 1];
      if (_month > 2 && _year % 4 == 0)
        ++_days;
      _days += 365 * _year + (_year + 3) / 4 - 1;
    
      _ticks = ((_days * 24 + _hour) * 60 + _minute) * 60 + _second;
      _ticks += SECONDS_FROM_1970_TO_2000;
    
      Teensy3Clock.set(_ticks);
    }
    Also, read about __rtc_localtime()
    Last edited by manitou; 08-20-2019 at 10:25 AM.

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,773
    Quote Originally Posted by manitou View Post
    i think your sketch needs Teensy3Clock.set() to set the RTC time. See File > Examples > Time > TimeTeensy3

    https://www.pjrc.com/teensy/td_libs_Time.html
    As noted a T4 used and TeensyLoader to set initial time in the end. Using the indicated "setTime(3, 3, 3, 8, 19, 2019);" that gave a time in 2020 { when uploading with TyCommander that doesn't know to set the time on upload } - so just ignored and commented it out. But that appears to not provide a valid set of the RTC time as used.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,597
    Quote Originally Posted by manitou View Post
    i think your sketch needs Teensy3Clock.set() to set the RTC time. See File > Examples > Time > TimeTeensy3
    Yup, this is the right answer. Calling setTime() only changes the volatile time in the Time library.

    That library was designed for cases like GPS and networking where you learn the correct time only occasionally and want to sync it to a known source. It's a little confusing to use with a RTC, because you might expect it to always defer to the RTC. But Time keeps track on its own. You need to change the actual source where it's sync'ing to actually change the date/time.

  8. #8
    Senior Member
    Join Date
    Apr 2017
    Posts
    187
    yep setting the teensy3clock was it, it now works like a million bucks.

    Code:
      setTime(rHour, rMinute, rSecond, rDay, rMonth, rYear);
      Teensy3Clock.set(now());

Posting Permissions

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