Time in Arduino programming with examples

Time in Arduino:

Arduino Time- in this article, I am going to show you how to use the time function in Arduino with programming examples


Time Functions in Arduino:

These instructions do not belong to any object, so they are written directly:

millis():

this Arduino time returns the number of milliseconds (ms) since the Arduino board started running the current sketch. This number will reset to zero after approximately 50 days (when its value exceeds the maximum allowed by its type, which is “unsigned long”). It has no parameters.

micros():

this Arduino time function returns the number of microseconds (µs) since the board Arduino started running the current sketch. This number –of type “unsigned long ”- will reset to zero after approximately 70 minutes. This instruction has a resolution of 4 µs (that is, the returned value is always a multiple of four). Remember that 1000 µs is a millisecond and therefore, 1000000 µs is one second. It has no parameters.



delay():

this Arduino time pauses the sketch for the specified number of milliseconds as a parameter –of type “unsigned long”.It has no return value.

delayMicroseconds():

this Arduino time pauses the sketch for the number of microseconds specified as a parameter –of type “unsigned long”.Currently, The maximum value that can be used with precision is 16383. To wait greater than this, it is recommended to use the delay() instruction. The minimum value that can be used accurately is 3 µs. It has no return value.


Examples of Arduino time functions:

Example1: how to use micros() function of time in Arduino programming:

Output:

arduino time

As you can see by the “Serial monitor” how it goes increasing the time that passes since the sketch started. The value observed increases approximately one second at a time.


Example2: how to use millis() function of time in Arduino programming:

Output:

arduion time

In the above code you can see a way to count the time elapsed between two specific moments. The procedure is to save in a variable the value returned by millis() at the initial time, and save in another different variable the value returned by millis() at the final time, to then subtract one from the other and thus find out the elapsed time.



Related Articles:

for loop in Arduino programming

Thermistor or Temperature Sensor With Arduino

Photoresistor | LDR Arduino based Projects

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: