Real Time Clock (RTC) is used for monitoring time and maintaining a calendar. Particle Photon has in-built RTC module.
In order to use an RTC, we need to first program it with the current date and time.
We need to provide a continuous supply voltage to the internal RTC, backup registers and SRAM for maintaining updated time and date.
To use internal RTC module of Particle Photon, we need to connect CMOS battery (here 3 V) to its VBAT pin. When 3V3 is not present or power gets turned off, the supply voltage which is connected to VBAT will provide power to RTC and update time and date continuously. Here we used 3 Volt CMOS battery cell.
Interfacing Diagram
Particle Photon VBAT Pin Connection
Example 1
Let’s configure RTC of Particle Photon to current date and time. Then read the date and time continuously from RTC.
Now, write a program which enables the particle photon to sync to the current date and time(UTC). Once the RTC of Particle photon gets synced, then read the live date and time and display them on the Serial window.
Some of the following listed functions can be used to access Particle Photon’s RTC.
A) Particle.syncTime()
- This function is used to synchronize the current date and time of Particle Photon’s RTC with the Particle Cloud.
- When a device connected to the cloud this will happens automatically. If your device runs continuously for a long time, you may want to synchronize once per day or so.
Example
: Particle.syncTime();
B) setTime()
- Set the system time to the given timestamp.
Example
: Time.setTime(1413034662);
C) timeStr()
- This function returns the string of day, date and time for the given time (Wed May 21 01:08:47 2014).
Example
: Serial.print(Time.timeStr());
D) Time.zone()
- Set the time zone offset (+/-) from UTC.
Example
: Time.zone(+5.30)- India's time zone is +5.30 from the UTC. You can search your country’s time zone from here.
E) Time.now()
- To retrieve the current UNIX timestamp.
Example
: Time.now()
F) Time.hourFormat12()
- To retrieve the current time in 12 hour format.
Example
: Time.hourFormat12()- It returns 1-12 integer numbers.
G) Time.second()
- To gives the current time in second
Example
: Time.second()
H) Time.minute()
- To gives the current time in minute
Example
: Time.minute()
I) Time.hour()
- To gives the current time in hour
Example
: Time.hour()
B) Time.isPM()
- It returns the current time is PM or Not If current time is PM then it gives ‘1’ else ‘0’
Example
: Time.isPM()
C) Time.isAM()
- It returns the current time is AM or Not If current time is AM then it gives ‘1’ else ‘0’
Example
: Time.isAM()
Program
Sync the Particle Photon to current UTC time and read current date and time (UTC) using in-built RTC of Particle Photon.
void setup()
{
Particle.syncTime(); /*sync to current time to the particle cloud*/
Serial.begin(9600);
}
void loop()
{
Serial.println(Time.timeStr()); /* print on serial time, day and date */
delay(1000);
}
Output of RTC (UTC):
How to convert current UTC time to the Country Time zone?
To convert UTC time to time zone as per our country, we need to add/subtract offset value to/from UTC time for getting our country time zone.
E.g.
UTC Time –> 10.00 A.M
Now,
For India -> 10.00 A.M + 5.30 = 3.30 P.M
New York -> 10.00 A.M - 5 = 5 A.M
Example 2
Let’s convert UTC time to India’s Time zone using RTC of Particle photon.
Program
void setup()
{
Particle.syncTime(); /*sync to current time to the particle cloud*/
Serial.begin(9600);
}
void loop()
{
Time.zone(+5.30); /* add time zone of your country, india’s time zone is (+5.30)*/
Serial.print(Time.hourFormat12(Time.now())); /* print serially current hour*/
Serial.print(":");
Serial.print(Time.minute(Time.now()));/* print serially current minute*/
Serial.print(":");
Serial.print(Time.second(Time.now()));/* print serially second hour*/
if(!Time.isPM())
Serial.println(" AM");
else
Serial.println(" PM");
delay(1000);
}
Output of RTC (Local time):
Components Used |
||
---|---|---|
Particle Photon PHNTRAYH |
X 1 |
Downloads |
||
---|---|---|
|
Download |