Arduino now() function explanation

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:

4 thoughts on “Arduino now() function explanation”

  1. Pingback: How to reduce memory consumption Arduino - CHIPPIKO

    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.

  2. Pingback: How to Define Pins in Arduino for Beginners - CHIPPIKO

Leave a Comment

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

Scroll to Top