Simple RTC Clock with a SAMD Board

This example demonstrate how to use the RTC library methods.

Hardware Required

Circuit

Only your Arduino Board is needed for this example.

The circuit for this example.
The circuit for this example.

Code

1/*
2
3 Simple RTC for Arduino Zero and MKR1000
4
5 Demonstrates the use of the RTC library for the Arduino Zero and MKR1000
6
7 This example code is in the public domain
8
9 http://arduino.cchttps://www.arduino.cc/en/Tutorial/SimpleRTC
10
11 created by Arturo Guadalupi <a.guadalupi@arduino.cc>
12
13 15 Jun 2015
14
15 modified
16
17 18 Feb 2016
18
19 modified by Andrea Richetta <a.richetta@arduino.cc>
20
21 24 Aug 2016
22
23*/
24
25#include <RTCZero.h>
26
27/* Create an rtc object */
28
29RTCZero rtc;
30
31/* Change these values to set the current initial time */
32
33const byte seconds = 0;
34
35const byte minutes = 0;
36
37const byte hours = 16;
38
39/* Change these values to set the current initial date */
40
41const byte day = 15;
42
43const byte month = 6;
44
45const byte year = 15;
46
47void setup()
48{
49
50 Serial.begin(9600);
51
52 rtc.begin(); // initialize RTC
53
54 // Set the time
55
56 rtc.setHours(hours);
57
58 rtc.setMinutes(minutes);
59
60 rtc.setSeconds(seconds);
61
62 // Set the date
63
64 rtc.setDay(day);
65
66 rtc.setMonth(month);
67
68 rtc.setYear(year);
69
70 // you can use also
71
72 //rtc.setTime(hours, minutes, seconds);
73
74 //rtc.setDate(day, month, year);
75}
76
77void loop()
78{
79
80 // Print date...
81
82 print2digits(rtc.getDay());
83
84 Serial.print("/");
85
86 print2digits(rtc.getMonth());
87
88 Serial.print("/");
89
90 print2digits(rtc.getYear());
91
92 Serial.print(" ");
93
94 // ...and time
95
96 print2digits(rtc.getHours());
97
98 Serial.print(":");
99
100 print2digits(rtc.getMinutes());
101
102 Serial.print(":");
103
104 print2digits(rtc.getSeconds());
105
106 Serial.println();
107
108 delay(1000);
109}
110
111void print2digits(int number) {
112
113 if (number < 10) {
114
115 Serial.print("0"); // print a 0 before if the number is < than 10
116
117 }
118
119 Serial.print(number);
120}

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.