Tutorial: Using Shield with Arduino
I welcome you to my review of MAX31343 Shield. My review is split to multiple chapters linked in following table of contents. Main page of review (marked with yellow star) contains summary, final score, and description of all chapters. Most important chapters of review begin with “Review” in name and contains detailed description of thing under review. There are chapters reviewing RTC chip, evaluation shield, bundled PICO board and their MCU. There are also described projects which I have done for evaluating this shield and finally there one tutorial (this article) showing how to easily use roadtested shield with Arduino.
Review Table of Contents
- Introduction, Overview of Table of Contents, Summary and Score
- Review of RTC Chip
- Review of Evaluation Shield
- Review of MAX32625 MCU and PICO Board
- Tutorial: Using Shield with Arduino (this article)
- Project 1: Platform Independent Library
- Project 2: Time Syncing over BLE
- Project 3: Linux Driver
In this tutorial I will use library provided by Maxim. It is very good that chip vendor provides Arduino library for their own chip. It is also good for non-Arduino users because they can use this library as a reference code. I also implemented my own library. I will not use it in this tutorial, but I will describe it in one of project. In tutorial I will describe how to setup jumpers, how to connect shield to Arduino and how to write easy firmware which will print clock configured in chip, setup alarm, configure interrupt and I will also show how to handle interrupt from MAX31343 chip and shield on Arduino.
Jumper setup
I have described jumpers in chapter Review of Evaluation Shield ion more details then I will provide there. Now I will describe only recommended configuration for usage with Arduino. Most jumpers were configured correctly from the factory but some of them should be adjusted for correct operation.
At first you must connect jumpers connecting power supplies to chip. There are four jumpers related to this. Connect JU5, JU11, JU12 and JU13. All of them should be fitted by default.
You need to select which package variant of chip you want to evaluate. It is configured using JU10 and you should connect this jumper to bottom position (position near P7 connector) unless you soldered and want to evaluate second package variant. Jumper most probably came correctly fitted from factory.
Then you need to connect all signals to Arduino connector. Connect JU1, JU3, JU6, JU7 and JU8. All of them should be fitted by default.
At last you need to select source for main and backup powering chips. Select supercapacitor as a backup supply. It is done using JU2 and this is most probably only configuration which differs from original factory configuration. By default, no backup supply is selected, and backup supply is connected to ground. For powering chip in time when your Arduino is disconnected you must switch the jumper to bottom pin with label SUPER CAP. Main power supply you can configure using jumper JU4 and you can choose any of 1.8V, 3.3V and 5V no matter what Arduino you use. It is because board have voltage level shifters and as far as your Arduino provide correctly connected IOREF source, it will work correctly. This is good thing because you can safely set this jumper to position 5V (which is the best option for achieving most efficient backup super capacitor charging) even you use 3.3V Arduino (or other board with this connector).
Shield connection
Before you connect shield to your Arduino you need to disconnect PICO board from left connector. You should not have connected shield to both boards (PICO and Arduino) at once. Details about this restriction are described in chapter Review of Evaluation Shield. After disconnecting you can connect shield to your Arduino and power it.
Library
Go to MAX31343 page on Maxim Website and download library. Note that there are two similarly named files. Download file with uppercase D in Driver word. Library with lowercase D is older version with missing metadata for Arduino IDE and some bugs.
Install downloaded library to Arduino IDE using Sketch > Include Library > Add .ZIP Library….
Then you can include it into your sketch using Sketch > Include Library > Max31343RTC.
At beginning add one additional include. Include time.h file which contains tm (abbreviation of time) structure which we will use later.
#include <time.h>
Now we must declare pointer to Max31343 object which we will initiate later in setup function. This object will allow us to utilize library functions.
Max31343* max31343;
Now move to setup function. Initialize Serial class and then create new instance of Max31343 object with pointer to Wire interface which is used for controlling I2C bus. On most Arduinos it is just Wire but if your Arduino support multiple I2C buses you can try Wire1, Wire2, …. For example on Arduino Nano 33 BLE Sense it is Wire1.
Serial.begin(9600); max31343 = new Max31343(&Wire);
Now move to loop function. In this function we will get current time. We must allocate one tm structure, and pass pointer to this structure to get_time method of max31343 object.
struct tm now; max31343->get_time(&now);
Now we can print retrieved time. Tm structure has members like tm_sec, tm_min, … for time fields but there is easiest method of formatting time to string using strftime function. Functions expect pointer to buffer, size of buffer, formatting string and pointer to time structure which should be formatted to buffer. We must allocate some buffer, call mentioned function and after that print filled buffer.
char buffer[64]; strftime(buffer, sizeof(buffer), "It is: %Y-%m-%d %H:%M:%S", &now); Serial.print(buffer);
Finally, you need to print newline character and wait some time to next round.
Serial.println(); delay(250);
You can run your code and you should see time from RTC chip printed approximately 4 times per second. If you see incorrect time you need to modify your sketch by calling set_time method or you can disconnect your shield from Arduino, connect PICO board again and configure time using provided GUI software which you can download at the same page where you have downloaded Arduino library.
Alarm and interrupt
Chip offers more features than just saving clock when system is not powered on. One of feature is alarm. You can configure time and mode which defines how frequently alarm triggers (for example every minute, every hour, every month, only once, …) and second feature is that chip can trigger interrupt which can trigger interrupt in your Arduino. I will show configuration of alarm which triggers every hour when minute is 51 and second is 10. It triggers for example at 16:51:10, then at 17:51:10, then at 18:51:10, ….
In setup function define structure with definition of parameters specifying when should alarm trigger. If you are configuring regularly triggering alarm you do not need to fill all parameters of structure. In case of hourly alarm, you need to configure at least minute when it should trigger (51 in my case) and second when it should trigger (10 in my case).
struct tm alarm; alarm.tm_sec = 10; alarm.tm_min = 51;
Now you can “upload” your configuration to chip using set_alarm method. You need to select instance of alarm (Chip supports two alarms. Alarm1 supports more modes then alarm2). Second parameter is pointer to tm structure and third parameter define how frequently your alarm should trigger.
max31343->set_alarm(Max31343::ALARM1, &alarm, Max31343::ALARM_PERIOD_HOURLY);
Now we need to enable interrupt. This consists of two steps:
- You need to enable generating interrupt by MAX31343 chip
- You need to configure “reception” of interrupt in your Arduino
Following two lines will do this. First line specify that you want to enable generating interrupt by MAX31343 chip when interrupt from alarm1 occur. Second line configure interrupt line (interrupt wire is routed to D2 pin on Arduino) and when interrupt is triggered line is pulled low (falling edge of signal). Second parameter of configuring interrupt is name of function which should be called when interrupt occur.
max31343->irq_enable(Max31343::INTR_ID_ALARM1); attachInterrupt(digitalPinToInterrupt(2), interruptHandler, FALLING);
Now we must define interrupt handler which I have referred in previous command. It is very important to make interrupt handler as short as possible. You can’t do any action using MAX31343 library because it will result into deadlock by a nature of I2C implementation on Arduino. So, we will only set some flag to 1 there and final processing of interrupt we will do in loop function where we can call MAX31343 library safely.
bool intFlag = false; void interruptHandler() { intFlag = true; }
Now go to loop function and add check between printing buffer and new line checking for interrupt flag. If flag occurs clear them.
if (intFlag) { intFlag = false; }
Inside this check after clearing flag we will get interrupt details from MAX31343 chip and after checking when we are really sure that interrupt is caused by alarm, we will print message about this.
int intr_source = max31343->get_interrupt_source(); if (intr_source == Max31343::INTR_ID_ALARM1){ Serial.print(" (alarm fired!)"); }
That is all. You can run your code. It will print 4 times per second actual clock and every hour at 51 minute and 10 second it will print information that alarm was triggered.
Complete sketch is available to download below. I hope that this tutorial was useful to you. My opinion about RTC chip, it’s features, provided library and some other things are in chapters mentioned at the beginning of this post.