{"id":5962,"date":"2019-07-25T12:19:28","date_gmt":"2019-07-25T16:19:28","guid":{"rendered":"https:\/\/itp.nyu.edu\/physcomp\/?page_id=5962"},"modified":"2022-11-06T18:17:13","modified_gmt":"2022-11-06T23:17:13","slug":"lab-using-a-real-time-clock","status":"publish","type":"page","link":"https:\/\/itp.nyu.edu\/physcomp\/labs\/lab-using-a-real-time-clock\/","title":{"rendered":"Lab: Using a Real-Time Clock"},"content":{"rendered":"\n<p>In this lab, you&#8217;ll learn how to use a real-time clock on a microcontroller.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Though this is written for the Arduino Nano 33 IoT and MKR modules, the principles apply to any real-time clock.<\/p>\n\n\n\n<p>A real-time clock (RTC) is an application-specific integrated circuit (ASIC) that&#8217;s designed to keep accurate time in hours, minutes, seconds, days, months, and years. Most real-time clocks have a synchronous serial interface to communicate with a microcontroller. The <a href=\"https:\/\/www.sparkfun.com\/products\/12708\">DS1307<\/a> from Dallas Semiconductor is a typical one. Each RTC generally has a library to interface with it as well.&nbsp; Many microcontrollers are incorporating an RTC into the microcontroller chip itself now as well. The SAMD M0+ chip that is the CPU of the MKRs and the Nano 33 IoT has an RTC built into it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Youll_Need_to_Know\"><\/span>What You\u2019ll Need to Know<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To get the most out of this lab, you should be familiar with the following concepts and you should install the Arduino IDE on your computer. You can check how to do so in the links below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a title=\"Microcontrollers: The Basics\" href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/microcontrollers\/microcontrollers-the-basics\/\">What is a microcontroller<\/a><\/li><li>Beginning&nbsp;<a title=\"Programming Terms and Programming Environments\" href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/programming\/programming-terms-and-programming-environments\/\">programming terms<\/a><\/li><li>The basics of&nbsp;<a title=\"Electricity: the Basics\" href=\"https:\/\/itp.nyu.edu\/physcomp\/lessons\/electronics\/electricity-the-basics\/\">electricity<\/a><\/li><li>What is a&nbsp;<a class=\"wikilink\" style=\"color: #37aad1;\" href=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/breadboard\/\">solderless breadboard<\/a>&nbsp;and <a title=\"Lab: Setting Up A Breadboard\" href=\"https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-electronics\/breadboard\/\">how to use one<\/a><\/li><li><a href=\"http:\/\/arduino.cc\/en\/Guide\/HomePage\">Getting Started with Arduino Guide<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Things_Youll_Need\"><\/span>Things You\u2019ll Need<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabTemplateNanoShort_bb.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabTemplateNanoShort_bb.png\" alt=\"Arduino Nano on a breadboard.\" class=\"wp-image-5903\" width=\"228\" height=\"359\"\/><\/a><figcaption>Figure 1. Breadboard view of Arduino Nano mounted on a breadboard.<\/figcaption><\/figure><\/div>\n\n\n\n<p><em>Image made with <a href=\"http:\/\/fritzing.org\/home\/\">Fritzing<\/a><\/em><\/p>\n\n\n\n<p><a href=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/LabTemplateNanoShort_bb.png\"><br><\/a>You&#8217;ll need an Arduino Nano 33 IoT or any of the MKR series Arduinos for this exercise. You don&#8217;t need any other parts, unless you plan to connect to external sensors.<\/p>\n\n\n\n<p>As shown in Figure 1, The Nano is mounted at the top of the breadboard, straddling the center divide, with its USB connector facing up. The top pins of the Nano are in row 1 of the breadboard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Program_the_Arduino\"><\/span>Program the Arduino<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Make sure you&#8217;re using the <a href=\"https:\/\/www.arduino.cc\/en\/main\/software\">Arduino IDE version 1.8.9 or later<\/a>. If you&#8217;ve never used the type of Arduino module that you&#8217;re using here (for example, a Nano 33 IoT), you may need to install the board definitions. Go to the Tools Menu \u2192 Board submenu \u2192 Board Manager. A new window will pop up. Search for your board&#8217;s name (for example, Nano 33 IoT), and the Boards manager will filter for the correct board. Click install and it will install the board definition.<\/p>\n\n\n\n<p>You&#8217;ll need to install the RTC library too. Go to the Sketch menu \u2192 Include Library&#8230; Submenu \u2192 Manage Libraries. A new window will pop up. Search for your the name of the library (RTCZero) and click the Install button. The Library manager will install the library. You might want to open the <a href=\"https:\/\/www.arduino.cc\/en\/Reference\/RTC\">RTC library documentation<\/a> in a browser as well. This library should work on any board that uses the SAMD M0+ processor.<\/p>\n\n\n\n<p>Real-time clocks are simple devices. You set the time, then it runs. When you want the time, you read the time. Depending on the software interface of the RTC you&#8217;re using, sometimes you can set the time using <a href=\"https:\/\/www.epochconverter.com\/\">UNIX Epoch time<\/a> as well, which&nbsp;is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970. The API may differ from one library to a next, but all RTC libraries will have commands to get and set the hour, minute, second, day, month, and year. The RTCZero is typical in that sense. Here is an example of how to set the time:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n#include &lt;RTCZero.h&gt;  \/\/ include the library\nRTCZero rtc;           \/\/ make an instance of the library\n\nvoid setup() {\n  Serial.begin(9600);\n\n  rtc.begin(); \/\/ initialize RTC\n\n  \/\/ Set the time\n  rtc.setHours(12);\n  rtc.setMinutes(34);\n  rtc.setSeconds(56);\n\n  \/\/ Set the date\n  rtc.setDay(23);\n  rtc.setMonth(6);\n  rtc.setYear(19);\n\n  \/\/ you can also set the time and date like this:\n  \/\/rtc.setTime(hours, minutes, seconds);\n  \/\/rtc.setDate(day, month, year);\n\n  \/\/ if you know the epoch time, you can do this:\n  rtc.setEpoch(1564069843);\n}\n<\/pre><\/div>\n\n\n<p>To read the time, you use the getter methods: getSecond, getMinute, getHour, etc.:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\nvoid loop() {\n  int h = rtc.getHours();\n\n  int m = rtc.getMinutes();\n  int s = rtc.getSeconds();\n  int d = rtc.getDay();\n  int mo = rtc.getMonth();\n  int yr = rtc.getYear();\n  long epoch = rtc.getEpoch();\n\n  \/\/ make a String for printing:\n  String dateTime = &quot;&quot;;\n  if (h &lt; 10) dateTime += &quot;0&quot;;\n  \/\/ add a zero for single-digit values:\n  dateTime += h;\n  dateTime += &quot;:&quot;;\n  if (m &lt; 10) dateTime += &quot;0&quot;;\n  \/\/ add a zero for single-digit values:\n  dateTime += m;\n  dateTime += &quot;:&quot;;\n  if (s &lt; 10) dateTime += &quot;0&quot;;\n  \/\/ add a zero for single-digit values:\n  dateTime += s;\n  dateTime += &quot;,&quot;;\n  if (d &lt; 10) dateTime += &quot;0&quot;;\n  \/\/ add a zero for single-digit values:\n  dateTime += d;\n  dateTime += &quot;\/&quot;;\n  if (mo &lt; 10) dateTime += &quot;0&quot;;\n  \/\/ add a zero for single-digit values:\n  dateTime += mo;\n  dateTime += &quot;\/&quot;;\n  if (yr &lt; 10) dateTime += &quot;0&quot;;\n  \/\/ add a zero for single-digit values:\n  dateTime += yr;\n  Serial.println(dateTime);\n  delay(1000);\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_the_RTC_Alarm\"><\/span>Using the RTC Alarm<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The RTCZero library also has the capability to set an alarm and call a function when the alarm goes off. Not all RTCs offer this feature, but since it&#8217;s built into the processor, it&#8217;s possible for the Nano 33 IoT and the MKR boards. Here&#8217;s a simple example that prints a message once a minute:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: arduino; title: ; notranslate\" title=\"\">\n#include &lt;RTCZero.h&gt;\nRTCZero rtc;\n\nvoid setup() {\n  Serial.begin(9600);\n\n  rtc.begin();\n  rtc.setTime(12, 23, 56);\n  rtc.setDate(23, 06, 19);\n\n  \/\/ set the alarm time:\n  rtc.setAlarmTime(0, 0, 0);\n  \/\/ alarm when seconds match the alarm time:\n  rtc.enableAlarm(rtc.MATCH_SS);\n  \/\/ attach a function to the alarm:\n  rtc.attachInterrupt(alarm);\n}\n\nvoid loop() {\n  \/\/ nothing happens here\n}\n\nvoid alarm() {\n  Serial.println(&quot;Top of the minute&quot;);\n}\n\ufeff\n<\/pre><\/div>\n\n\n<p>You can match just the seconds for an alarm once a minute (rtc.MATCH_SS) or the minutes and seconds for once an hour (rtc.MATCH_MMSS) or hours, minutes, seconds for once a day (rtc.MATCH_HHMMSS).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Maintaining_Time\"><\/span>Maintaining Time<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In order to be truly &#8220;real&#8221; time, an RTC needs constant power. The built-in RTC of the&nbsp;SAMD M0+ microcontroller will reset to &#8220;zero&#8221; when power is interrupted (but will continue to increment through a reset provided an initial time is not hardcoded in setup).<\/p>\n\n\n\n<p>For this reason, many RTC breakout boards, such as Adafruit&#8217;s <a href=\"https:\/\/learn.adafruit.com\/adafruit-ds3231-precision-rtc-breakout\/overview\">DS3231<\/a>&nbsp;board, include a battery holder, typically for the kind of long lasting coin-cell batteries found in watches. RTCs powered this way can maintain accurate time for several years without additional power. The <a href=\"https:\/\/www.arduino.cc\/en\/Reference\/RTC\">Arduino RTC page<\/a> has notes on maintaining power for the built-in RTC on Arduino Zero and later boards.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lab, you&#8217;ll learn how to use a real-time clock on a microcontroller.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":11,"menu_order":901,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[11,15,54],"tags":[],"class_list":["post-5962","page","type-page","status-publish","hentry","category-code","category-lab","category-microcontrollers"],"_links":{"self":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/5962"}],"collection":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/comments?post=5962"}],"version-history":[{"count":12,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/5962\/revisions"}],"predecessor-version":[{"id":10739,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/5962\/revisions\/10739"}],"up":[{"embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/pages\/11"}],"wp:attachment":[{"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/media?parent=5962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/categories?post=5962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.nyu.edu\/physcomp\/wp-json\/wp\/v2\/tags?post=5962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}