Tick raw Unix time in RV-1805 RTC

Hi guyz. Hope all is well.

My project is simple. I set the time on a RV-1805 Real Time Clock Module using the code below.
( SparkFun Real Time Clock Module - RV-1805 (Qwiic) - BOB-14558 - SparkFun Electronics )

Can this RTC be set to count Unix time instead? By that I mean, can I directly feed 1721743817 into the RTC and then ask the RTC to increase this 1721743817 number by 1 for every real second that ticks in real life?

Right now, the code below sets the time in normal human format and prints the time in normal human format.

My project prefers that I work directly with the 'raw Unix' time.

The datasheet and the manual do not mention any info that helps.

Ty 4 the replies!

/*
  Setting time from the RV-1805 Real Time Clock
  By: Andy England
  SparkFun Electronics
  Date: 2/22/2017
  License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

  Feel like supporting our work? Buy a board from SparkFun!
  https://www.sparkfun.com/products/14642

  This example shows how to set the time on the RTC to the compiler time or a custom time.

  Hardware Connections:
    Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
    Plug the RTC into the shield (any port)
    Open the serial monitor at 9600 baud
*/

#include <SparkFun_RV1805.h>

RV1805 rtc;

//The below variables control what the date will be set to
int hund = 50;
int sec = 2;
int minute = 18;
int hour = 7;
int date = 25;
int month = 6;
int year = 2018;
int day = 5;

void setup() {

  Wire.begin();

  Serial.begin(9600);
  Serial.println("Read Time from RTC Example");

  if (rtc.begin() == false) {
    Serial.println("Something went wrong, check wiring");
  }

  //Use the time from the Arduino compiler (build time) to set the RTC
  //Keep in mind that Arduino does not get the new compiler time every time it compiles. to ensure the proper time is loaded, open up a fresh version of the IDE and load the sketch.
  if (rtc.setToCompilerTime() == false) {
    Serial.println("Something went wrong setting the time");
  }

  //Uncomment the below code to set the RTC to your own time
  /*if (rtc.setTime(hund, sec, minute, hour, date, month, year, day) == false) {
    Serial.println("Something went wrong setting the time");
    }*/

  Serial.println("RTC online!");
}

void loop() {
  if (rtc.updateTime() == false) //Updates the time variables from RTC
  {
    Serial.print("RTC failed to update");
  }
  
  String currentTime = rtc.stringTime();
  Serial.println(currentTime);
}

The datasheet would tell.

https://www.microcrystal.com/fileadmin/Media/Products/RTC/App.Manual/RV-1805-C3_App-Manual.pdf

The register descriptions in the application manual don't mention a register for it.
The SparkFun library does not provide a function for it.

So the answer is "no".

You can install the time library that has means to convert the epoch time to a tmElements struct (containing year, month, day, hour, minute, seconds) that you can pass to the rtc.setTime() method.

It's the function of an RTC to do that. Which problem are you trying to solve?

As of now, I just want to directly set the Unix Epoch time directly on the RTC. I am happy for now with that.

You can use breakTime(); look at the examples for further assistance.

Create your own with a spare Arduino.
Something like this, not tested.

//    FILE: unix_time_rtc.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.0
//    DATE: 2024-07-23
// PURPOSE: simulate a unix time clock
//     URL:

#include "Arduino.h"
#include "Wire.h"

volatile uint32_t unixtime = 0;
uint32_t lastUpdate = 0;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  Serial.println(__FILE__);

  Wire.begin(0x77);
  Wire.onReceive(setUnixTime);
  Wire.onRequest(getUnixTime);
}

void loop()
{
  uint32_t now = millis();
  if ((now - lastUpdate) >= 1000)
  {
    lastUpdate += 1000;
    noInterrupts();
    unixtime++;
    interrupts();
  }
}

//  LSB FIRST
void setUnixTime(int howMany)
{
  if (howMany != 4) return;
  uint32_t ut = Wire.read();
  ut += (Wire.read() << 8);
  ut += (Wire.read() << 16);
  ut += (Wire.read() << 24);
  noInterrupts();
  unixtime = ut;
  interrupts();
}

//  LSB FIRST
void getUnixTime()
{
  noInterrupts();
  uint32_t ut = unixtime;
  interrupts();

  for (int i = 0; i < 4; i++)
  {
    Wire.write(ut & 0xFF);
    ut >>= 8;
  }
}

// -- END OF FILE --

wow. ty so much!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.