Getting a "timestamp" of when data is collected is entirely down to you.

Most Arduinos don't have any concept of the current time, only the time since the program started running. To know what the time "now" is you have to have some mechanism to tell the Arduino what the time is, along with a method of keeping track of that time.

There are devices called Real-Time Clock (RTC) modules which keep track of the time for you. They don't magically know the time - you still have to tell them at least once.

You could tell it the time through the serial port to set the clock - from then on (assuming the RTC has power) the RTC will know what the time is.

Another option to get the time into the RTC is to use an internet connection (ESP8266, WiFi shield, Ethernet shield, etc) to perform a Network Time Protocol (NTP) query to a time server on the internet (such as pool.ntp.org) to get the current time and update the RTC. This should be done regularly to correct any drift in the RTC.

Once you have an RTC and a method of setting the time you can query the time whenever you sample some data and store that time along with the data in whatever way is most suitable for your situation.

Answer from Majenko on Stack Exchange
Top answer
1 of 2
2

Getting a "timestamp" of when data is collected is entirely down to you.

Most Arduinos don't have any concept of the current time, only the time since the program started running. To know what the time "now" is you have to have some mechanism to tell the Arduino what the time is, along with a method of keeping track of that time.

There are devices called Real-Time Clock (RTC) modules which keep track of the time for you. They don't magically know the time - you still have to tell them at least once.

You could tell it the time through the serial port to set the clock - from then on (assuming the RTC has power) the RTC will know what the time is.

Another option to get the time into the RTC is to use an internet connection (ESP8266, WiFi shield, Ethernet shield, etc) to perform a Network Time Protocol (NTP) query to a time server on the internet (such as pool.ntp.org) to get the current time and update the RTC. This should be done regularly to correct any drift in the RTC.

Once you have an RTC and a method of setting the time you can query the time whenever you sample some data and store that time along with the data in whatever way is most suitable for your situation.

2 of 2
1

It depends on how you define the time stamp. A Unix timestamp is the number of seconds elapsed since Unix epoch time, i.e. January 1 1970 00:00 UTC, this is a very common time stamp. You can setup with NTP via the internet, or you can use a RTC on your board. How you do it depends on how accurate you want it and what you have available. Try this link it may help: https://currentmillis.com/

Top answer
1 of 4
15

NTP is the proven way of getting time remotely. NTP libraries, like @Marcel denotes, is making UDP connections to a server with an interval. So you do not need to do any polling to the server before using it.

Here is the usage of an NTP library with a one-hour offset and one minute refresh interval:

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#define NTP_OFFSET   60 * 60      // In seconds
#define NTP_INTERVAL 60 * 1000    // In miliseconds
#define NTP_ADDRESS  "europe.pool.ntp.org"

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);

void setup(){
  timeClient.begin();
}

void loop() {
  timeClient.update();
}

To get a timestamp or formatted time anytime you want, use the functions:

String formattedTime = timeClient.getFormattedTime();
unsigned long epcohTime =  timeClient.getEpochTime();
2 of 4
2

Looking at that code I don't think this microcontroller directly has a clock built in as mentioned. Since you have Wi-Fi though you could make a web query to get it instead.

I'd use a REST query to a place like this:

https://timezonedb.com/api

Which will give you back a JSON formatted time. If you only need accuracy +/- a few seconds that will be fine. You could lower the bandwidth / improve battery life by setting the time like this and then using an internal timer to calculate an offset instead of making a query every time you need a time stamp. Eventually you would need to requery the time and 'correct' it since your timer on that probably is not accurate for long periods of time, plus it could eventually roll over.

If you need the time more accurately then that you will likely need a clock. You could try to do a bit of correction based on ping, but all in all how accurate it needs to be is based on your project requirements.

🌐
Random Nerd Tutorials
randomnerdtutorials.com › home › project › esp32 › getting date and time with esp32 on arduino ide (ntp client)
Getting Date and Time with ESP32 (NTP Client) | Random Nerd Tutorials
June 12, 2024 - The date is saved on the dayStamp variable, and the time on the timeStamp variable.The time is requested and printed in every second. Upload the code to the ESP32. Make sure you have the right board and COM port selected. After uploading the code, press the ESP32 “Enable” button, and you should get the date and time every second as shown in the following figure. In this tutorial we’ve shown you how to easily get date and time with the ESP32 on the Arduino IDE using an NTP server.
🌐
Circuit Basics
circuitbasics.com › using-an-arduino-ethernet-shield-for-timekeeping
How to Get the Date and Time on an Arduino
October 22, 2024 - This timestamp is the number of seconds elapsed since NTP epoch ( 01 January 1900 ). To get the current UTC time, we just need to subtract the seconds elapsed since the NTP epoch from the timestamp received.
🌐
Random Nerd Tutorials
randomnerdtutorials.com › home › project › esp32 › get epoch/unix time with the esp32 (arduino)
Get Epoch/Unix Time with the ESP32 (Arduino) | Random Nerd Tutorials
February 16, 2021 - Learn how to request Epoch time or Unix time using the ESP32 board with Arduino IDE. Getting the epoch time can be useful to timestamp your readings, etc...
🌐
PJRC
pjrc.com › teensy › td_libs_DateTime.html
DateTime Arduino Library, timekeeping and time data manipulation on Teensy
PJRC Store Teensy 3.0, $19 Teensy 2.0, $16 Teensy++ 2.0, $24 USB Cable, $4 Teensy Main Page Getting Started How-To Tips Code Library Projects Teensyduino Main Tutorial Download+Install Basic Usage Digital I/O PWM & Tone Timing USB Serial USB Keyboard USB Mouse USB Joystick USB MIDI USB Flight ...
🌐
YouTube
youtube.com › watch
Arduino Serial Monitor Timestamps: demo and explanations - YouTube
The "Show timestamp" feature in the Arduino Serial Monitor (IDE version 1.8.8 and above) tells us when the PC receives strings from the Arduino's Serial. In ...
Published   January 13, 2019
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Millis vs Timestamp - Programming - Arduino Forum
July 14, 2021 - Hello, I apologize for the basic question but I am wondering if this would be the preferred process for you all. I am just getting my feet wet and this forum has been super helpful in the past. I have been using the serial timestamp with my data coming from the Nano 33 BLE, typically the readings are 17:46:27.316, 17:46:27.420, 17:46:27.529... etc.
Find elsewhere
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Pulling TimeStamp from the Serial Monitor? - Programming - Arduino Forum
December 19, 2018 - Greetings all, I'll keep this short and simple: Is there a way to grab the timestamp from the monitor in the IDE and save it out as a variable (e.g. for use in a file header)? I'm using an Arduino UNO + Ethernet Shield v2 (for writing to SD card) on IDE v1.8.7 to record analog signals from ...
🌐
Arduino
playground.arduino.cc › Code › DateTime
Arduino Playground - DateTime
August 8, 2022 - The DateTime library adds timekeeping capability to the Arduino without requiring external hardware. It allows a sketch to get the current second, minute, hour, day, month and year.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Timestamp in milliseconds - Programming - Arduino Forum
April 6, 2024 - How do you get the timestamp in milliseconds for #include "time.h"? The way I do it it fees a bit hacky. #include "time.h" int64_t timestamp; int64_t test; const char* ntpServer = "pool.ntp.org"; unsigned long getTime() { time_t now; struct tm timeinfo; if (!getLocalTime(&timeinfo)) { return(0); } time(&now); return now; } void setup(){ Serial.begin(115200); configTime(0, 0, ntpServer); } void loop(){ //Get current timestamp timestamp = getTime(); Seria...
🌐
GitHub
github.com › Erriez › ErriezTimestamp
GitHub - Erriez/ErriezTimestamp: Timestamp library for Arduino · GitHub
This is a timestamp library for Arduino that can be used to measure execution time in microseconds or milliseconds.
Starred by 3 users
Forked by 2 users
Languages   C++
🌐
Arduino
arduino.cc › reference › en › libraries › timestamp32bits
timestamp32bits - Arduino Reference
May 8, 2022 - This library allows to generate 32 bits timestamps that are compatible with valid current UNIX timestamps for the time period between 1/1/1970 and 2/7/2106 06:28:15. Now it also allows to generate custom timestamps specifying the EPOCH year. ... This library is compatible with all architectures so you should be able to use it on all the Arduino boards.
🌐
Blues
discuss.blues.com › troubleshooting
Read timestamp with arduino - Troubleshooting - Blues Community Forum
February 27, 2021 - I added this code to “Example1_NotecardBasics.ino”: JTIME ncTime = 0; rsp = notecard.requestAndResponse(notecard.newRequest("card.time")); if (rsp != NULL) { ncTime = JGetNumber(rsp, "time"); notecard.deleteResponse(rsp); ...
🌐
Arduino Forum
forum.arduino.cc › other hardware › sensors
Time stamping the sensor output - Sensors - Arduino Forum
May 31, 2015 - Hi everyone, I wanted to know how to timestamp my sensor output in the serial monitor so that I may get the reading against the corrosponding date and time. I don't want to use the RTC chip for this purpose and I want t…
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Creating an timestamp ? - Programming - Arduino Forum
September 27, 2024 - I dont have code because I cant work out how to do this. The functional requirement though are these. 1.) Given a year, month, day, hour,minute and second I want to return a timestamp 2.) Given that timestamp I want to…
🌐
Arduino Getting Started
arduinogetstarted.com › tutorials › arduino-log-data-with-timestamp-to-sd-card
Arduino - Log Data with Timestamp to SD Card | Arduino Getting Started
1 week ago - Arduino - How to log data with timestamp a to multiple files on Micro SD Card , one file per day · The time information is get from a RTC module and written to Micro SD Card along with data.
🌐
Arduino
docs.arduino.cc › libraries › timestamp32bits
Arduino
The Arduino environment can be extended through the use of libraries. Libraries provide extra functionality for use in sketches. To use a library in a sketch, select it from Sketch > Import Library.