I have a an arduino NANO 33 BLE and I'm trying to get the current time and date. Based on the documentation, the arduino has nRF52840 microcontroller which comes with a 24 RTC timer It is listed in the nordic documentation as well. However, I'm not sure exactly how to use it to get the current time; Based on the MbedOS documentation for Time I wrote out the code to print out the current time and date in conjunction with reading the data:

#include "mbed.h"
#include <Time.h> 
#include <RTClib.h>

time_t rawtime; 
struct tm *info; 

void setup() {

//resistor val = 100K

void loop() {
  rawtime = time(NULL); 
  info = localtime(&rawtime);
  int val = analogRead(A1);
  Serial.print(asctime(info) );

However it just prints out this:

XX-Thu Jan 1 00:08:30 1970

Where the XX is the correct sensor data. Why is the time not the current time but the beginning unix epoch ? This code was run in the Desktop Arduino IDE in Win10

  • 1
    if you buy a wall clock, put a battery into it and don't do anything else, then do you expect the correct time to be shown?
    – jsotola
    Feb 17, 2023 at 18:58
  • @jsotola doesnt the rawtime = time(NULL); time(&rawtime); acts like setting the time ? Or am I misinterpreting that line of code ?
    – Riperoni
    Feb 22, 2023 at 19:50
  • where would the current time/date come from? ... duckduckgo.com/?q=jan+1st+1970&ia=web
    – jsotola
    Feb 22, 2023 at 20:11

1 Answer 1


I was going to write something more elaborate, but in short the RTC in the NRF52840 is not a "clock" so much as an asynchronous timer/counter. As in, you can run it from a clock source that isn't related to the system clock that times your code and it may (or may not) continue to count when you're in a sleep mode. So, you may be able to use this async. timer/counter as a building block in constructing what amounts to an approximation of a real-time clock (like a DS3231). That you might use it that way isn't assumed by Nordic, or mbed, or Arduino, or libc/time.h, and RTCLib.h concerns itself with accessing specific models external realtime clock chips with no direct connection to time.h. So there's nothing to support your specific use case in each of these things.

So you can try to build a real-time clock functionality out of it. But if you're like most Arduino users you're probably better off getting a DS3231 or something like it. You will probably end up with more accurate time keeping and battery backup, etc than you would if you build this functionality yourself out of the NRF52's counter.

If you have a need or strong desire to use <time.h>, that can usually be done by completing its implementation in some implementation specific documented way. Basically you'd retrieve the time fields out of something like the DS3231 an then use that to set what amounts to a time_t variable that backs time() and then to call regularly call some kind of time advance function from an interrupt routine based on the square wave output from the DS3231 (or whatever). If you really want to make use of the async. counter yourself, all of that applies there as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.