Skip to content

Custom RTC Time

Kurtis Dinelle edited this page Apr 7, 2022 · 5 revisions

The real-time clocks built into the Hypnos (DS3231) and Adafruit Adalogger Featherwing (PCF8523) may be initialized upon reset via two ways: automatically at compile time (using local system time) or manually via the serial monitor. Unfortunately, due to the way Loom is compiled, the time generated during compilation may not always be as accurate as one would like. This guide will demonstrate step-by-step how to set a custom time and will only need to be repeated if the coin-cell battery is removed or the RTC is otherwise reset.

Table of Contents

Modifying Config

Currently, the RTC module in Loom defaults to using compilation time (however, this is planned to change in a future Loom version). So for now, you will need to tell the RTC to use custom time manually via the config file. To do this, open your config.h file and make sure your entry for the RTC module matches one of the following (depending on if you're using the Hypnos or Adalogger).

Hypnos (DS3231)

{\
	'name':'DS3231',\
	'params':[10,true,true]\
},\

Adalogger (PCF8523)

{\
	'name':'PCF8523',\
	'params':[10,true,true]\
},\
  • The first parameter (10) represents the timezone and can be set to whatever you please. For the purpose of this guide, we will be using Pacific Daylight Time (PDT). The full list of timezones and there associated value includes:

    • 0("WAT")
    • 1("AT")
    • 2("ADT")
    • 3("AST")
    • 4("EDT")
    • 5("EST")
    • 6("CDT")
    • 7("CST")
    • 8("MDT")
    • 9("MST")
    • 10("PDT")
    • 11("PST")
    • 12("AKDT")
    • 13("AKST")
    • 14("HST")
    • 15("SST")
    • 16("GMT")
    • 17("BST")
    • 18("CET")
    • 19("EET")
    • 20("EEST")
    • 21("BRT")
    • 22("ZP4")
    • 23("ZP5")
    • 24("ZP6")
    • 25("ZP7")
    • 26("AWST")
    • 27("ACST")
    • 28("AEST")
  • The second parameter sets use_local_time to true, which allows you to log local time alongside UTC time.

  • The third parameter sets custom_time to true, which allows you to use the serial monitor to set the time. You must have the serial monitor open after the sketch uploads for this to work.

Compiling & Uploading Sketch

Once the RTC has been reset and config.h has been modified to allow for custom time, you can then go ahead and compile and upload a sketch making use of the RTC (if you do not currently have one, you may use any one of the examples provided by Loom by going to Files > Examples > Loom > RTC in the Arduino IDE).

To do this, ensure your board is plugged into USB and receiving power. Then, click the upload button in the top-left corner of the IDE.

If everything worked correctly, you will see a message saying "Done uploading."

Entering Custom Time (for both DS3231 and PCF8523)

IMPORTANT
If you have inherited a board with a coin-cell battery already in it, it is possible the RTC has already been programmed. This means you will not be prompted to enter a custom time. If that is the case, see the steps below to reset the RTC before continuing with this section.

  1. The sketch will then wait a bit for you to open the serial monitor before continuing, which you can do by clicking the magnifying glass in the top-right corner of the IDE.
  1. Once the serial monitor is open, you should see a prompt from the RTC asking you to input a custom time. You will be prompted for year, month, day, hour, minute, and second (in that order).
  1. To send input to the device, locate the input field at the top of the serial monitor. Type in your times here (following the format given by the prompt), then hit Enter or click Send.
  1. Once you have entered input for all time components, the RTC will initialize with your custom time and begin execution of the sketch. If you have entered incorrect time data, the RTC will initialize with current UTC time and may be inaccurate. To start over, you must go back to the Resetting the RTC step and continue again from there.

  2. If everything went correctly, you may still notice that the RTC is reporting a UTC time converted from your custom time in the serial monitor. This is normal, and is done to keep things consistent between time zones. However, if you are logging data, you will notice in the log files on your SD card that the correct custom time is logged as well. At this point, as long as the coin-cell battery is not removed or the RTC is not otherwise reset, the RTC will continue to use your custom time between startups and sketch uploads and will no longer need to be manually set.

Resetting the RTC

In order for the RTC to be reinitialized with a new time, it must first be reset. To do this, the coin-cell battery must be removed then reinserted. However, before attempting this, you must ensure the Hypnos/Adalogger is not plugged into USB or otherwise receiving any power. Once you are ready, follow either the steps for the Hypnos or the Adalogger below depending on which device you are using.

Hypnos

  1. Locate the coin-cell battery on the opposite side of the SD card
  1. Using a thin tool, gently push the battery out of the holder from behind through the small gap
  1. Gently remove the battery with your fingers
  1. Wait roughly three seconds (allows time for the RTC to lose power and reset)
  2. Reinsert the battery fully

Adalogger

  1. Locate the coin-cell battery on the same side as the SD card
  1. Gently push down on the metal latch to pop the battery out of place
  1. Gently remove the battery with your fingers
  1. Wait roughly three seconds (allows time for the RTC to lose power and reset)
  2. Reinsert the battery fully