Skip to content

RTCZero Tutorial: Using the SAMD21 Real Time Clock

The Atmel SAMD21 is an amazing processor with a lot of built in functionality that previous processors, like the popular ATmega328p found in the TinyDuino, lack.

The example highlighted in this tutorial will show you how to display the date and time using the SAMD21 internal Real-Time Clock. This tutorial can be used with all of our SAMD21 processor products: TinyZero, TinyScreen+, RobotZero, and the WirelingZero

If you want an example on how to use the Real-Time Clock on the TinyScreen+, check out the TinyScreen+ RTC Time Display, External Interrupt & Sleep Mode Tutorial.


Materials

Hardware

Software


Software Setup

For the Arduino Sketch, you will need to have downloaded the RTCZero library. These can be found using the Arduino IDE Library Manager. Select Tools -> Library Manager and then type in "rtczero". Then you can press "Install".

Plug the SAMD21 processor into your computer with a micro USB cable. Make sure the power switch is turned to on! Open the SAMD21 RTC Arduino Example Sketch in the Arduino IDE.

Make the correct Tools selections for your development board in the Arduino IDE. If unsure, you can double check the Help page that mentions the Tools selections needed for any TinyCircuits processor.


Upload Program

Before uploading, be sure to initialize the hours, minutes, seconds, day, month, and year variables to the appropriate values. You can also write your own functions to set these values at runtime.

Code
/*
  An RTC demo for the SAMD21 boards: TinyZero, TinyScreen+, RobotZero, and WirelingZero.
*/

#include <RTCZero.h>

#if defined (ARDUINO_ARCH_AVR)
#define SerialMonitorInterface Serial
#include <SoftwareSerial.h>
#elif defined(ARDUINO_ARCH_SAMD)
#define SerialMonitorInterface SerialUSB
#endif

RTCZero rtc; // create an rtc object

/* Change these values to set the current initial time */
const byte hours = 13;
const byte minutes = 51;
const byte seconds = 0;

/* Change these values to set the current initial date */
const byte day = 3;
const byte month = 8;
const byte year = 20;

void setup() {
  rtc.begin(); // initialize RTC

  // Set the time
  rtc.setHours(hours);
  rtc.setMinutes(minutes);
  rtc.setSeconds(seconds);

  // Set the date
  rtc.setDay(day);
  rtc.setMonth(month);
  rtc.setYear(year);

  // you can use also
  //rtc.setTime(hours, minutes, seconds);
  //rtc.setDate(day, month, year);
}

void loop() {
  print_RTC();
  SerialMonitorInterface.println("- - - - - - - - - - - - - - - -");

  delay(1000);
}

void print2digits(int number) {
  if (number < 10) {
    SerialMonitorInterface.print("0"); // print a 0 before if the number is < than 10
  }
  SerialMonitorInterface.print(number);
}

void print_time() {
  SerialMonitorInterface.print("Time: ");

  // ...and time
  print2digits(rtc.getHours());
  SerialMonitorInterface.print(":");
  print2digits(rtc.getMinutes());
  SerialMonitorInterface.print(":");
  print2digits(rtc.getSeconds());
}

void print_date() {
  SerialMonitorInterface.print("\tDate: ");

  // Print date...
  print2digits(rtc.getDay());
  SerialMonitorInterface.print("/");
  print2digits(rtc.getMonth());
  SerialMonitorInterface.print("/");
  print2digits(rtc.getYear());
  SerialMonitorInterface.print(" ");

  SerialMonitorInterface.println();
}

void print_RTC() {
  print_time();
  print_date();
}

After uploading, open the Serial Monitor to view the time as it changes.


Downloads


Contact Us

If you have any questions or feedback, feel free to email us or make a post on our forum. Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it.

Thanks for making with us!