The ESP32 is a powerful and versatile microcontroller with an embedded RTC module. The RTC module enables tracking of the current date and time, including seconds, minutes, hours, day of the week, day of the month, month, and year. It supports both 24-hour and 12-hour formats.
By default, the RTC utilizes an Internal 150 kHz RC oscillator as its clock source, eliminating the need for external components. This configuration ensures the lowest deep-sleep current consumption.
However, it's important to note that the frequency stability of the internal clock source can be affected by temperature fluctuations, resulting in an approximate 5% error in the internal RTC clock frequency. Consequently, there could be a deviation of 180 seconds (3 minutes) per hour from the actual time. To mitigate this, regular NTP synchronization with the internal RTC is recommended.
The ESP32's RTC can operate in deep sleep mode while accurately maintaining timekeeping functionality. It consumes minimal power in this state, making it particularly suitable for battery-powered applications requiring long-term timekeeping.
In the code provided below, we will demonstrate how to set the RTC time using a Unix Timestamp and how to output the time to the Serial Monitor.
settimeofday() | Set time with Unix Time Stamp |
gettimeofday() | Get Now's Unix Time Stamp |
time() | same as above, to get Unix Time Stamp |
localtime_r()
|
Convert Unix Time Stamp into Local Time Struct |
asctime()
|
Convert Time Struct into String |
ctime()
|
Convert Unix Time Stamp into String The output is the same as asctime() |
strftime()
|
Custom Time format from time struct into String |
Serial.println()
|
Serial Output with formatted Time |