Arduino now() function explanation

Posted on

The now() function in the Arduino Time library returns the number of seconds elapsed since the start of the Unix epoch, which is January 1st, 1970. It’s not part of the standard C++ library or the Arduino core libraries but is derived from a library created by Michael Margolis.

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

Serial.println(now());

it will return a large number of seconds, which represents the number of seconds that have passed since January 1st, 1970. For example, the value 1673196305 represents a specific moment in time. To convert this value into a more readable format, such as a date and time, you can use other functions provided by the Time library.

By using the now() function, you can easily keep track of the current time and use it to perform time-based tasks in your Arduino projects. For example, you can use the now() function to keep track of the time elapsed between two events, or you can use it to perform actions based on the current time, such as switching on a light at a specific time of day.

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));

In short, the now() a function is a useful tool for working with time-related projects in Arduino, and it’s easy to use. Simply call the function, and it will return the number of seconds elapsed since January 1st, 1970. Hopefully, this article can help you.

Read more: