My Experience Using Nano Every With DS3231 Breakout

I wanted to document my steps/experience using the Nano Every with the Adafruit DS3231 breakout.

Step 1: Setup my hardware. After attaching the headers to the Nano Every and DS3231 breakout, I put them on a breadboard along with an LED and resistor connected between the SQW pin of the DS3231 breakout and 5V supplied by the Nano Every. I did not have the correct button cell for the DS3231 battery backup, so I used a 2xAAA holder to get the 3V battery backup. This is a temporary solution until my CR1220 button cells arrive. See picture of my breadboard setup.

Step 2: I installed the Arduino IDE version 1.8.16 for MacOS.

Step 3: I went to the Nano Every docs page (https://docs.arduino.cc/hardware/nano-every) and clicked on "Quick Start Guide", and followed the steps to install the megaAVR core, etc. This was critical to get the Nano Every to show up in the "Boards" list under Arduino IDE.

Step 4: I made sure that the Arduino IDE settings were correct for my board and port.
Tools > Boards > megAVR Boards > Arduino Nano Every
Tools > Emulation > None
Tools > Port > /dev/cu.usbmodem14401

Step 5: Install the Adafruit BusIO library by using Tools > Manage Libraries
and searching for "Adafruit BusIO" and selecting the latest version and clicking install.

Step 6: Install the RTC library by getting the zip file from the link on this page Overview | Adafruit DS3231 Precision RTC Breakout | Adafruit Learning System and using Sketch > Include Library > Add .ZIP Library.

Step 7: Plug in the USB cable and try running the basic blinking LED program from File > Examples > Basics > Blink. I played around with the time durations and re-uploaded to make sure everything was working correctly. I always get the following message but as far as I can tell, it is not critical: "avrdude: jtagmkII_initialize(): Cannot locate "flash" and "boot" memories in description"

Step 8: Open the DS3231 example from File > Examples > RTClib > ds3231 and run it. Also open Tools > Serial Monitor so you can see the output. I was getting some inconsistent behavior with uploads, but the biggest "AH HA" moment was that I needed to make sure the BAUD setting on the serial monitor was set to 57600 - which was the setting used in the DS3231 example sketch. Once I had that set correctly, the sketch ran perfectly.

Step 9: My main goal was to enable the SQW pin of the DS3231 breakout to output a 1Hz signal. There was not an example file for doing this for the DS3231, but there is an example for the similar DS1307 - File > Examples > DS1307SqwPin. I opened this and made a few mods - basically replacing DS3231 for DS1307 for the library call, etc. Also, mode "ON" does not exist in the DS3231, so I removed that from the print_mode section. I will copy my exact code to a reply.

Step 10: Once I had that working, I combined those two sketches into a new sketch that just set the Sqw pin to 1Hz and did not cycle through the options. It also just reports the current time, not the other info printed in the DS3231 example. I will copy that code in as well.

The cool think about the DS3231 is that if you have it battery backed up, if you pull main power than then re-apply power with the Arduino disconnected, the DS3231 will come back up in the same state - ie with the 1Hz output setting I gave it - and with the correct date and time. Now I have an accurate 1Hz source I can use in any project, Arduino or no.

From start to finish, this only took me about 2 hours to get familiarized with the tools and the programming language. I am super pleased with the Arduino and the DS3231 so far. Pictures and code to follow.

2 Likes

Code for DS3231SqwPin sketch based on the DS1307SqwPin sketch:


#include "RTClib.h"

RTC_DS3231 rtc; 

int mode_index = 0;

Ds3231SqwPinMode modes[] = { DS3231_OFF, DS3231_SquareWave1HZ, DS3231_SquareWave1kHz, DS3231_SquareWave4kHz, DS3231_SquareWave8kHz};


void print_mode() {
  Ds3231SqwPinMode mode = rtc.readSqwPinMode(); 

  Serial.print("Sqw Pin Mode: ");
  switch(mode) {
  case DS3231_OFF:              Serial.println("OFF");       break;
  case DS3231_SquareWave1HZ:    Serial.println("1Hz");       break;
  case DS3231_SquareWave1kHz:   Serial.println("1kHz");      break;
  case DS3231_SquareWave4kHz:   Serial.println("4kHz");      break;
  case DS3231_SquareWave8kHz:  Serial.println("8kHz");       break;
  default:                      Serial.println("UNKNOWN");   break;
  }
}

void setup () {
  Serial.begin(57600);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  print_mode();
}

void loop () {
  rtc.writeSqwPinMode(modes[mode_index++]);
  print_mode();

  if (mode_index > 5) {
    mode_index = 0;
  }

  delay(5000);
}

1 Like

My sketch that just sets the 1Hz output and reads the time every second. I also corrected a formatting issue for single digit minutes and seconds.

#include "RTClib.h"

RTC_DS3231 rtc; 

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int mode_index = 0;

Ds3231SqwPinMode modes[] = { DS3231_OFF, DS3231_SquareWave1Hz, DS3231_SquareWave1kHz, DS3231_SquareWave4kHz, DS3231_SquareWave8kHz};


void print_mode() {
  Ds3231SqwPinMode mode = rtc.readSqwPinMode(); 

  Serial.print("Sqw Pin Mode: ");
  switch(mode) {
  case DS3231_OFF:              Serial.println("OFF");       break;
  case DS3231_SquareWave1Hz:    Serial.println("1Hz");       break;
  case DS3231_SquareWave1kHz:   Serial.println("1kHz");      break;
  case DS3231_SquareWave4kHz:   Serial.println("4kHz");      break;
  case DS3231_SquareWave8kHz:  Serial.println("8kHz");       break;
  default:                      Serial.println("UNKNOWN");   break;
  }
}

void setup () {
  Serial.begin(57600);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }
  rtc.writeSqwPinMode(modes[1]);
  print_mode();
}

void loop () {
  
  //rtc.writeSqwPinMode(modes[1]);
  //print_mode();

    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    if(now.minute() < 10){
    Serial.print("0");
    }    
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    if(now.second() < 10){
    Serial.print("0");
    }
    Serial.print(now.second(), DEC);
    Serial.println();

  delay(1000);
  
}
1 Like

Picture of my hardware setup:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.