Arduino now() function explanation

Posted on

In making Arduino projects related to time, generally, you will often use the now() function. Therefore, in this article, I will explain the Arduino now() function, and how it works.

The now() function is not standard C++ library or in Arduino, However, this is derived from the Time library created by Michael Margolis which you can visit here.

If you print the results directly with the serial monitor with the command:

Serial.println(now());

Then the now() function will return the current time as seconds since Jan 1, 1970, so it will produce a set of seconds values like 1673196305.


How to convert to UTC time?

It’s very simple. If you want to print that value to UTC time, use this syntax:

  Serial.print(year(utc_time));
  Serial.print('/');
  Serial.print(month(utc_time));
  Serial.print('/');
  Serial.print(day(utc_time));
  Serial.print(' ');
  Serial.print(hour(utc_time));
  Serial.print(':');
  Serial.print(minute(utc_time));
  Serial.print(':');
  Serial.println(second(utc_time));

Hopefully, this article can help you.

Read more:

3 comments

    1. The now() function from the Time library by Michael Margolis returns the current time as the number of seconds since January 1, 1970. This function does not provide sub-second precision. It returns the time as a 32-bit unsigned long value, which has a maximum value of around 4,294,967,295, or about 136 years. Therefore the precision is in seconds, not hundredths of seconds.

      If you need sub-second precision, you might consider using other libraries or methods, such as timers or counters, or perhaps libraries that provide microsecond precision.

Leave a Reply

Your email address will not be published. Required fields are marked *