// 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 #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; 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() { Serial.begin(19200); // Serial port to PC while (!Serial); // for Leonardo Serial.println(F("Initialized ds3231 example3 v1.0")); // initialize the RTC rtc.begin(); delay(500); // allow serial data to go out } void loop() { printTime(); // read temperature register int8_t valMSB = read_i2c_register(DS3231_ADDRESS, 0x11); uint8_t valLSB = read_i2c_register(DS3231_ADDRESS, 0x12); double temp = valMSB; temp += (double)(valLSB>>6)/ 4.0; // convert to F temp = 9.0*temp / 5.0 + 32.0; Serial.print("Temperature is "); Serial.println(temp); delay(64000); }