// This code is written by Paul MacDougal for the December 11, 2017 meeting of TriEmbed.org // It is released to the public domain // It does what it does. // DS3231 breakout board is: // Vcc (3.3v) // SDA (A4) // SCL (A5) // NC // GND // pin 3 of SOIC16 package (int/sqw) is hooked to SQW_PIN // pin 1 of SOIC16 package (32kHz) is hooked to Hz32_PIN #include #include #include #include "RTClib.h" uint8_t read_i2c_register(uint8_t addr, uint8_t reg); void write_i2c_register(uint8_t addr, uint8_t reg, uint8_t val); RTC_DS3231 rtc; #define SQW_PIN 2 #define Hz32_PIN 8 void printTime() { DateTime now = rtc.now(); if (now.month() < 10) { Serial.print("0"); } Serial.print(now.month()); Serial.print('/'); if (now.day() < 10) { Serial.print("0"); } Serial.print(now.day()); Serial.print('/'); Serial.print(now.year()); Serial.print(" "); if (now.hour() < 10) { Serial.print("0"); } 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(); } void setup() { pinMode(SQW_PIN, INPUT_PULLUP); pinMode(Hz32_PIN, INPUT_PULLUP); Serial.begin(19200); // Serial port to PC while (!Serial); // for Leonardo Serial.println(F("Initialized ds3231 example2 v1.0")); // initialize the RTC rtc.begin(); // Set SQW to 8kHz uint8_t val = read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL); val |= 0x08; // set RS1 val &= ~0x14; // clear RS2, INTCN write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, val); // Enable 32kHz output val = read_i2c_register(DS3231_ADDRESS, 0x0F); val |= 0x08; // set EN32kHz write_i2c_register(DS3231_ADDRESS, 0x0F, val); } void loop() { printTime(); delay(500); }