first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,175 @@
// CONNECTIONS:
// At24c32 SDA --> SDA
// At24c32 SCL --> SCL
// At24c32 VCC --> 5v
// At24c32 GND --> GND
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <EepromAT24C32.h>
SoftwareWire myWire(SDA, SCL);
EepromAt24c32<SoftwareWire> Rtc(myWire);
/* for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <EepromAT24C32.h>
EepromAt24c32<TwoWire> RtcEeprom(Wire);
// if the above doesn't work, and you don't have "address" pads or switches on
// the board to set the address, then try the common address of 7
// EepromAt24c32<TwoWire> RtcEeprom(Wire, 0b111);
//
// If you have any of the address pads on the board soldered together
// then you need to provide the state of those pads, normally they
// are connected to vcc with a reading of 1, if soldered they are
// grounded with a reading of 0. The bits are in the order A2 A1 A0
// thus the following would have the A2 soldered together
// EepromAt24c32<TwoWire> RtcEeprom(Wire, 0b011);
/* for normal hardware wire use above */
// nothing longer than 32 bytes
// rtc eeprom memory is 32 byte pages
// writing is limited to each page, so it will wrap at page
// boundaries.
// But reading is only limited by the buffer in Wire class which
// by default is 32
// example settings objec that will be serialized into and out of EEPROM
struct Settings
{
uint8_t size; // size of this structure for versioning/validation
uint8_t value1 : 6; // only 6 bits (0-63)
uint8_t value2 : 6; // only 6 bits (0-63)
uint8_t value3 : 6; // only 6 bits (0-63)
uint32_t value4;
};
// where in EEPROM we will store the settings
const uint8_t SettingsAddress = 0;
// handy routine to return true if there was an error
// but it will also print out an error message with the given topic
bool wasError(const char* errorTopic = "")
{
uint8_t error = RtcEeprom.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void setup ()
{
Serial.begin(115200);
//--------RTC EEPROM SETUP ------------
// if you are using ESP-01 then uncomment the line below to define pins for
// those available for SDA, SCL
// RtcEeprom.Begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
RtcEeprom.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
/* comment out on a second run to see that the info is stored long term */
// Store something in memory on the Eeprom
{
Settings mySettings =
{
sizeof(Settings), // size
42, // value1
21, // value2
7, // value3
18875, // value4
};
// store the settings, nothing longer than 32 bytes due to paging
uint8_t written = RtcEeprom.SetMemory(SettingsAddress,
reinterpret_cast<const uint8_t*>(&mySettings),
sizeof(mySettings));
wasError("setup setMemory settings");
}
/* end of comment out section */
}
void loop ()
{
delay(5000);
// read data
{
Settings retrievedSettings = { 0, 0, 0, 0, 0 }; // init to zero
// get our data from the address with the given size
uint8_t gotten = RtcEeprom.GetMemory(SettingsAddress,
reinterpret_cast<uint8_t*>(&retrievedSettings),
sizeof(Settings));
if (!wasError("loop getMemory settings"))
{
if (gotten != sizeof(Settings) ||
retrievedSettings.size != sizeof(Settings))
{
Serial.print("something didn't match, requested = ");
Serial.print(sizeof(Settings));
Serial.print(", gotten = ");
Serial.print(gotten);
Serial.print(", size = ");
Serial.print(retrievedSettings.size);
Serial.println();
}
Serial.print("data read (");
Serial.print(gotten);
Serial.println(")");
Serial.print(" size = ");
Serial.println(retrievedSettings.size);
Serial.print(" value1 = ");
Serial.println(retrievedSettings.value1);
Serial.print(" value2 = ");
Serial.println(retrievedSettings.value2);
Serial.print(" value3 = ");
Serial.println(retrievedSettings.value3);
Serial.print(" value4 = ");
Serial.println(retrievedSettings.value4);
Serial.println();
}
}
}

View File

@@ -0,0 +1,102 @@
#include "RtcAlarmManager.h"
#include <Wire.h>
#include <RtcDS1307.h> // Replace with the RTC you have
// foreward declare our alarm manager callback
void alarmCallback(uint8_t id, const RtcDateTime& alarm);
// global instance of the manager with three possible alarms
RtcAlarmManager<alarmCallback> Alarms(3);
// Replace with the RTC you have
RtcDS1307<TwoWire> Rtc(Wire);
void setup ()
{
Serial.begin(115200);
Serial.println("Initializing...");
//--------RTC SETUP ------------
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
// get the real date and time from a source like an already
// configured RTC module
RtcDateTime now = Rtc.GetDateTime();
// Sync the Alarms to current time
Alarms.Sync(now);
// NOTE: Due to this sketch not deleting alarms, the returned ids from
// AddAlarm can be assumed to start at zero and increment from there.
// Otherwise the ids would need to be captured and used in the callback
//
int8_t result;
// add an alarm to sync time from rtc at a regular interval,
// due to CPU timing variance, the Alarms time can get off over
// time, so this alarm will trigger a resync every 20 minutes
result = Alarms.AddAlarm(now, 20 * c_MinuteAsSeconds); // every 20 minutes
if (result < 0)
{
// an error happened
Serial.print("AddAlarm Sync failed : ");
Serial.print(result);
}
// add a daily alarm at 5:30am
RtcDateTime working(now.Year(), now.Month(), now.Day(), 5, 30, 0);
result = Alarms.AddAlarm(working, AlarmPeriod_Daily);
if (result < 0)
{
// an error happened
Serial.print("AddAlarm Daily failed : ");
Serial.print(result);
}
// add a weekly alarm for Saturday at 7:30am
working = RtcDateTime(now.Year(), now.Month(), now.Day(), 7, 30, 0);
working = working.NextDayOfWeek(DayOfWeek_Saturday);
result = Alarms.AddAlarm(working, AlarmPeriod_Weekly);
if (result < 0)
{
// an error happened
Serial.print("AddAlarm Weekly failed : ");
Serial.print(result);
}
Serial.println("Running...");
}
void loop ()
{
delay(1000); // simulating other work your sketch will do
Alarms.ProcessAlarms();
}
void alarmCallback(uint8_t id, [[maybe_unused]] const RtcDateTime& alarm)
{
// NOTE: Due to this sketch not deleting alarms, the returned ids from
// AddAlarm can be assumed to start at zero and increment from there.
// Otherwise the ids would need to be captured and used here
//
switch (id)
{
case 0:
{
// periodic sync from trusted source to minimize
// drift due to inaccurate CPU timing
RtcDateTime now = Rtc.GetDateTime();
Alarms.Sync(now);
}
break;
case 1:
Serial.println("DAILY ALARM: Its 5:30am!");
break;
case 2:
Serial.println("WEEKLY ALARM: Its Saturday at 7:30am!");
break;
}
}

View File

@@ -0,0 +1,134 @@
// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND
#include <RtcDS1302.h>
ThreeWire myWire(4,5,2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
#define countof(a) (sizeof(a) / sizeof(a[0]))
const char data[] = "what time is it";
void setup ()
{
Serial.begin(57600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
/* comment out on a second run to see that the info is stored long term */
// Store something in memory on the RTC
uint8_t count = sizeof(data);
uint8_t written = Rtc.SetMemory((const uint8_t*)data, count); // this includes a null terminator for the string
if (written != count)
{
Serial.print("something didn't match, count = ");
Serial.print(count, DEC);
Serial.print(", written = ");
Serial.print(written, DEC);
Serial.println();
}
/* end of comment out section */
}
void loop ()
{
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println(" +");
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
delay(5000);
// read data
uint8_t buff[20];
const uint8_t count = sizeof(buff);
// get our data
uint8_t gotten = Rtc.GetMemory(buff, count);
if (gotten != count)
{
Serial.print("something didn't match, count = ");
Serial.print(count, DEC);
Serial.print(", gotten = ");
Serial.print(gotten, DEC);
Serial.println();
}
Serial.print("data read (");
Serial.print(gotten);
Serial.print(") = \"");
// print the string, but terminate if we get a null
for (uint8_t ch = 0; ch < gotten && buff[ch]; ch++)
{
Serial.print((char)buff[ch]);
}
Serial.println("\"");
delay(5000);
}
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,100 @@
// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND
#include <RtcDS1302.h>
ThreeWire myWire(4,5,2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
void setup ()
{
Serial.begin(57600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
void loop ()
{
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,241 @@
// CONNECTIONS:
// DS1307 SDA --> SDA
// DS1307 SCL --> SCL
// DS1307 VCC --> 5v
// DS1307 GND --> GND
#define countof(a) (sizeof(a) / sizeof(a[0]))
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <RtcDS1307.h>
SoftwareWire myWire(SDA, SCL);
RtcDS1307<SoftwareWire> Rtc(myWire);
for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS1307.h>
RtcDS1307<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
// nothing longer than 32 bytes
// rtc eeprom memory is 32 byte pages
// writing is limited to each page, so it will wrap at page
// boundaries.
// But reading is only limited by the buffer in Wire class which
// by default is 32
// example settings objec that will be serialized into and out of EEPROM
struct Settings
{
uint8_t size; // size of this structure for versioning/validation
uint8_t value1;
uint16_t value2;
uint32_t value3;
float value4;
};
// where in EEPROM we will store the settings
const uint8_t SettingsAddress = 0;
// handy routine to return true if there was an error
// but it will also print out an error message with the given topic
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void setup ()
{
Serial.begin(115200);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
// Rtc.Begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
Serial.println("RTC is older than compile time, updating DateTime");
Rtc.SetDateTime(compiled);
}
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
wasError("setup SetSquareWavePin");
/* comment out on a second run to see that the info is stored long term */
// Store something in memory on the RTC
{
Settings mySettings =
{
sizeof(Settings), // size
42, // value1
420, // value2
18875, // value3
3.14159f, // value4
};
// store the settings, nothing longer than 32 bytes due to paging
uint8_t written = Rtc.SetMemory(SettingsAddress,
reinterpret_cast<const uint8_t*>(&mySettings),
sizeof(mySettings));
wasError("setup setMemory settings");
}
/* end of comment out section */
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
if (!wasError("loop IsDateTimeValid"))
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("loop GetDateTime"))
{
printDateTime(now);
Serial.println();
}
delay(5000);
// read data
{
Settings retrievedSettings = { 0, 0, 0, 0, 0 }; // init to zero
// get our data from the address with the given size
uint8_t gotten = Rtc.GetMemory(SettingsAddress,
reinterpret_cast<uint8_t*>(&retrievedSettings),
sizeof(Settings));
if (!wasError("loop getMemory settings"))
{
if (gotten != sizeof(Settings) ||
retrievedSettings.size != sizeof(Settings))
{
Serial.print("something didn't match, requested = ");
Serial.print(sizeof(Settings));
Serial.print(", gotten = ");
Serial.print(gotten);
Serial.print(", size = ");
Serial.print(retrievedSettings.size);
Serial.println();
}
Serial.print("data read (");
Serial.print(gotten);
Serial.println(")");
Serial.print(" size = ");
Serial.println(retrievedSettings.size);
Serial.print(" value1 = ");
Serial.println(retrievedSettings.value1);
Serial.print(" value2 = ");
Serial.println(retrievedSettings.value2);
Serial.print(" value3 = ");
Serial.println(retrievedSettings.value3);
Serial.print(" value4 = ");
Serial.println(retrievedSettings.value4);
Serial.println();
}
}
delay(5000);
}
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,176 @@
// CONNECTIONS:
// DS1307 SDA --> SDA
// DS1307 SCL --> SCL
// DS1307 VCC --> 5v
// DS1307 GND --> GND
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <RtcDS1307.h>
SoftwareWire myWire(SDA, SCL);
RtcDS1307<SoftwareWire> Rtc(myWire);
for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS1307.h>
RtcDS1307<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
// handy routine to return true if there was an error
// but it will also print out an error message with the given topic
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void setup ()
{
Serial.begin(115200);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
// Rtc.Begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
Serial.println("RTC is older than compile time, updating DateTime");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time, this is expected");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time, while not expected all is still fine");
}
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
wasError("setup SetSquareWavePin");
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
if (!wasError("loop IsDateTimeValid"))
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("loop GetDateTime"))
{
printDateTime(now);
Serial.println();
}
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,257 @@
// CONNECTIONS:
// DS3231 SDA --> SDA
// DS3231 SCL --> SCL
// DS3231 VCC --> 3.3v or 5v
// DS3231 GND --> GND
// SQW ---> (Pin19) Don't forget to pullup (4.7k to 10k to VCC)
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
SoftwareWire myWire(SDA, SCL);
RtcDS3231<SoftwareWire> Rtc(myWire);
for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
// Interrupt Pin Lookup Table
// (copied from Arduino Docs)
//
// CAUTION: The interrupts are Arduino numbers NOT Atmel numbers
// and may not match (example, Mega2560 int.4 is actually Atmel Int2)
// this is only an issue if you plan to use the lower level interupt features
//
// Board int.0 int.1 int.2 int.3 int.4 int.5
// ---------------------------------------------------------------
// Uno, Ethernet 2 3
// Mega2560 2 3 21 20 [19] 18
// Leonardo 3 2 0 1 7
#define RtcSquareWavePin 19 // Mega2560
#define RtcSquareWaveInterrupt 4 // Mega2560
// marked volatile so interrupt can safely modify them and
// other code can safely read and modify them
volatile uint16_t interuptCount = 0;
volatile bool interuptFlag = false;
void ISR_ATTR InteruptServiceRoutine()
{
// since this interupted any other running code,
// don't do anything that takes long and especially avoid
// any communications calls within this routine
interuptCount++;
interuptFlag = true;
}
// handy routine to return true if there was an error
// but it will also print out an error message with the given topic
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void setup ()
{
Serial.begin(115200);
// set the interupt pin to input mode
pinMode(RtcSquareWavePin, INPUT);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
Serial.println("RTC is older than compile time, updating DateTime");
Rtc.SetDateTime(compiled);
}
}
Rtc.Enable32kHzPin(false);
wasError("setup Enable32kHzPin");
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmBoth);
wasError("setup SetSquareWavePin");
// Alarm 1 set to trigger every day when
// the hours, minutes, and seconds match
RtcDateTime alarmTime = now + 88; // into the future
DS3231AlarmOne alarm1(
alarmTime.Day(),
alarmTime.Hour(),
alarmTime.Minute(),
alarmTime.Second(),
DS3231AlarmOneControl_HoursMinutesSecondsMatch);
Rtc.SetAlarmOne(alarm1);
wasError("setup SetAlarmOne");
// Alarm 2 set to trigger at the top of the minute
DS3231AlarmTwo alarm2(
0,
0,
0,
DS3231AlarmTwoControl_OncePerMinute);
Rtc.SetAlarmTwo(alarm2);
wasError("setup SetAlarmTwo");
// throw away any old alarm state before we ran
Rtc.LatchAlarmsTriggeredFlags();
wasError("setup LatchAlarmsTriggeredFlags");
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
// setup external interupt
// for some Arduino hardware they use interrupt number for the first param
attachInterrupt(RtcSquareWaveInterrupt, InteruptServiceRoutine, FALLING);
#else
// for some Arduino hardware they use interrupt pin for the first param
attachInterrupt(RtcSquareWavePin, InteruptServiceRoutine, FALLING);
#endif
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
if (!wasError("loop IsDateTimeValid"))
{
Serial.println("RTC lost confidence in the DateTime!");
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("loop GetDateTime"))
{
printDateTime(now);
Serial.println();
}
// we only want to show time every 10 seconds
// but we want to show responce to the interupt firing
for (int timeCount = 0; timeCount < 20; timeCount++)
{
if (Alarmed())
{
Serial.print(">>Interupt Count: ");
Serial.print(interuptCount);
Serial.println("<<");
}
delay(500);
}
}
bool Alarmed()
{
bool wasAlarmed = false;
if (interuptFlag) // check our flag that gets sets in the interupt
{
wasAlarmed = true;
interuptFlag = false; // reset the flag
// this gives us which alarms triggered and
// then allows for others to trigger again
DS3231AlarmFlag flag = Rtc.LatchAlarmsTriggeredFlags();
if (!wasError("alarmed LatchAlarmsTriggeredFlags"))
{
if (flag & DS3231AlarmFlag_Alarm1)
{
Serial.println("alarm one triggered");
}
if (flag & DS3231AlarmFlag_Alarm2)
{
Serial.println("alarm two triggered");
}
}
}
return wasAlarmed;
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,188 @@
// CONNECTIONS:
// DS3231 SDA --> SDA
// DS3231 SCL --> SCL
// DS3231 VCC --> 3.3v or 5v
// DS3231 GND --> GND
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
SoftwareWire myWire(SDA, SCL);
RtcDS3231<SoftwareWire> Rtc(myWire);
for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
// handy routine to return true if there was an error
// but it will also print out an error message with the given topic
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void setup ()
{
Serial.begin(115200);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
Serial.println("RTC is older than compile time, updating DateTime");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time, this is expected");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time, while not expected all is still fine");
}
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.Enable32kHzPin(false);
wasError("setup Enable32kHzPin");
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
wasError("setup SetSquareWavePin");
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
if (!wasError("loop IsDateTimeValid"))
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("loop GetDateTime"))
{
printDateTime(now);
Serial.println();
}
RtcTemperature temp = Rtc.GetTemperature();
if (!wasError("loop GetTemperature"))
{
temp.Print(Serial);
// you may also get the temperature as a float and print it
// Serial.print(temp.AsFloatDegC());
Serial.println("C");
}
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,163 @@
// CONNECTIONS:
// DS3231 SDA --> SDA
// DS3231 SCL --> SCL
// DS3231 VCC --> 3.3v or 5v
// DS3231 GND --> GND
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
SoftwareWire myWire(SDA, SCL);
RtcDS3231<SoftwareWire> Rtc(myWire);
for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
// handy routine to return true if there was an error
// but it will also print out an error message with the given topic
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void setup ()
{
Serial.begin(115200);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
Serial.println("RTC was not actively running");
}
}
else
{
Serial.println("RTC is actively running");
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
Serial.println("RTC is older than compile time, updating DateTime");
Rtc.SetDateTime(compiled);
}
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.Enable32kHzPin(false);
wasError("setup Enable32kHzPin");
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
wasError("setup SetSquareWavePin");
}
void loop ()
{
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("loop GetDateTime"))
{
printDateTime(now);
Serial.println();
}
for(;;)
{
Rtc.SetIsRunning(false);
if (!wasError("loop SetIsRunning"))
{
Serial.println(">>> Rtc ready for storage <<<");
}
delay(10000); // ten seconds
}
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,247 @@
// CONNECTIONS:
// DS3232 SDA --> SDA
// DS3232 SCL --> SCL
// DS3232 VCC --> 5v
// DS3232 GND --> GND
#define countof(a) (sizeof(a) / sizeof(a[0]))
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3232.h>
SoftwareWire myWire(SDA, SCL);
RtcDS3232<SoftwareWire> Rtc(myWire);
for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3232.h>
RtcDS3232<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
// nothing longer than 32 bytes
// rtc eeprom memory is 32 byte pages
// writing is limited to each page, so it will wrap at page
// boundaries.
// But reading is only limited by the buffer in Wire class which
// by default is 32
// example settings objec that will be serialized into and out of EEPROM
struct Settings
{
uint8_t size; // size of this structure for versioning/validation
uint8_t value1;
uint16_t value2;
uint32_t value3;
float value4;
};
// where in EEPROM we will store the settings
const uint8_t SettingsAddress = 0;
// handy routine to return true if there was an error
// but it will also print out an error message with the given topic
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void setup ()
{
Serial.begin(115200);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
// Rtc.Begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
Serial.println("RTC is older than compile time, updating DateTime");
Rtc.SetDateTime(compiled);
}
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.Enable32kHzPin(false);
wasError("setup Enable32kHzPin");
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
wasError("setup SetSquareWavePin");
/* comment out on a second run to see that the info is stored long term */
// Store something in memory on the RTC
{
Settings mySettings =
{
sizeof(Settings), // size
42, // value1
420, // value2
18875, // value3
3.14159f, // value4
};
// store the settings, nothing longer than 32 bytes due to I2C buffer
uint8_t written = Rtc.SetMemory(SettingsAddress,
reinterpret_cast<const uint8_t*>(&mySettings),
sizeof(mySettings));
wasError("setup setMemory settings");
if (written != sizeof(mySettings))
{
Serial.println("setup setMemory failed to write complete settings");
}
}
/* end of comment out section */
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
if (!wasError("loop IsDateTimeValid"))
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("loop GetDateTime"))
{
printDateTime(now);
Serial.println();
}
delay(5000);
// read data
{
Settings retrievedSettings = { 0,0,0,0,0 }; // init size to zero
// get our data from the address with the given size
uint8_t gotten = Rtc.GetMemory(SettingsAddress,
reinterpret_cast<uint8_t*>(&retrievedSettings),
sizeof(Settings));
if (!wasError("loop getMemory settings"))
{
if (gotten != sizeof(Settings) ||
retrievedSettings.size != sizeof(Settings))
{
Serial.print("something didn't match, requested = ");
Serial.print(sizeof(Settings));
Serial.print(", gotten = ");
Serial.print(gotten);
Serial.print(", size = ");
Serial.print(retrievedSettings.size);
Serial.println();
}
Serial.print("data read (");
Serial.print(gotten);
Serial.println(")");
Serial.print(" size = ");
Serial.println(retrievedSettings.size);
Serial.print(" value1 = ");
Serial.println(retrievedSettings.value1);
Serial.print(" value2 = ");
Serial.println(retrievedSettings.value2);
Serial.print(" value3 = ");
Serial.println(retrievedSettings.value3);
Serial.print(" value4 = ");
Serial.println(retrievedSettings.value4);
Serial.println();
}
}
delay(5000);
}
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,179 @@
// Reference for connecting SPI see https://www.arduino.cc/en/Reference/SPI
// CONNECTIONS:
// DS3234 MISO --> MISO
// DS3234 MOSI --> MOSI
// DS3234 CLK --> CLK (SCK)
// DS3234 CS (SS) --> 5 (pin used to select the DS3234 on the SPI)
// DS3234 VCC --> 3.3v or 5v
// DS3234 GND --> GND
// SQW ---> (Pin19) Don't forget to pullup (4.7k to 10k to VCC)
const uint8_t DS3234_CS_PIN = 5;
#include <SPI.h>
#include <RtcDS3234.h>
RtcDS3234<SPIClass> Rtc(SPI, DS3234_CS_PIN);
// Interrupt Pin Lookup Table
// (copied from Arduino Docs)
//
// CAUTION: The interrupts are Arduino numbers NOT Atmel numbers
// and may not match (example, Mega2560 int.4 is actually Atmel Int2)
// this is only an issue if you plan to use the lower level interupt features
//
// Board int.0 int.1 int.2 int.3 int.4 int.5
// ---------------------------------------------------------------
// Uno, Ethernet 2 3
// Mega2560 2 3 21 20 [19] 18
// Leonardo 3 2 0 1 7
// esp8266 (pin and interrupt should be the same thing)
// esp32 (pin and interrupt should be the same thing)
#define RtcSquareWavePin 19 // Mega2560
#define RtcSquareWaveInterrupt 4 // Mega2560
// marked volatile so interrupt can safely modify them and
// other code can safely read and modify them
volatile uint16_t interuptCount = 0;
volatile bool interuptFlag = false;
void ISR_ATTR InteruptServiceRoutine()
{
// since this interupted any other running code,
// don't do anything that takes long and especially avoid
// any communications calls within this routine
interuptCount++;
interuptFlag = true;
}
void setup ()
{
Serial.begin(115200);
while (!Serial);
// set the interupt pin to input mode
pinMode(RtcSquareWavePin, INPUT_PULLUP); // external pullup maybe required still
SPI.begin();
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3234SquareWavePin_ModeAlarmBoth);
// Alarm 1 set to trigger every day when
// the hours, minutes, and seconds match
RtcDateTime alarmTime = now + 88; // into the future
DS3234AlarmOne alarm1(
alarmTime.Day(),
alarmTime.Hour(),
alarmTime.Minute(),
alarmTime.Second(),
DS3234AlarmOneControl_HoursMinutesSecondsMatch);
Rtc.SetAlarmOne(alarm1);
// Alarm 2 set to trigger at the top of the minute
DS3234AlarmTwo alarm2(
0,
0,
0,
DS3234AlarmTwoControl_OncePerMinute);
Rtc.SetAlarmTwo(alarm2);
// throw away any old alarm state before we ran
Rtc.LatchAlarmsTriggeredFlags();
// setup external interupt
attachInterrupt(RtcSquareWaveInterrupt, InteruptServiceRoutine, FALLING);
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
}
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
// we only want to show time every 10 seconds
// but we want to show responce to the interupt firing
for (int timeCount = 0; timeCount < 20; timeCount++)
{
if (Alarmed())
{
Serial.print(">>Interupt Count: ");
Serial.print(interuptCount);
Serial.println("<<");
}
delay(500);
}
}
bool Alarmed()
{
bool wasAlarmed = false;
if (interuptFlag) // check our flag that gets sets in the interupt
{
wasAlarmed = true;
interuptFlag = false; // reset the flag
// this gives us which alarms triggered and
// then allows for others to trigger again
DS3234AlarmFlag flag = Rtc.LatchAlarmsTriggeredFlags();
if (flag & DS3234AlarmFlag_Alarm1)
{
Serial.println("alarm one triggered");
}
if (flag & DS3234AlarmFlag_Alarm2)
{
Serial.println("alarm two triggered");
}
}
return wasAlarmed;
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,139 @@
// Reference for connecting SPI see https://www.arduino.cc/en/Reference/SPI
// CONNECTIONS:
// DS3234 MISO --> MISO
// DS3234 MOSI --> MOSI
// DS3234 CLK --> CLK (SCK)
// DS3234 CS (SS) --> 5 (pin used to select the DS3234 on the SPI)
// DS3234 VCC --> 3.3v or 5v
// DS3234 GND --> GND
const uint8_t DS3234_CS_PIN = 5;
#include <SPI.h>
#include <RtcDS3234.h>
RtcDS3234<SPIClass> Rtc(SPI, DS3234_CS_PIN);
const char data[] = "what time is it";
void setup ()
{
Serial.begin(115200);
while (!Serial);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
SPI.begin();
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.SetSquareWavePin(DS3234SquareWavePin_ModeNone);
/* comment out on a second run to see that the info is stored long term */
// Store something in memory on the RTC
Rtc.SetMemory(0, 13); // address of a data item
uint8_t written = Rtc.SetMemory(13, (const uint8_t*)data, sizeof(data) - 1); // remove the null terminator strings add
Rtc.SetMemory(1, written); // size of data time
/* end of comment out section */
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
delay(5000);
// read data
// get the offset we stored our data from address zero
uint8_t address = Rtc.GetMemory(0);
if (address != 13)
{
Serial.println("address didn't match");
}
else
{
// get the size of the data from address 1
uint8_t count = Rtc.GetMemory(1);
uint8_t buff[20];
// get our data from the address with the given size
uint8_t gotten = Rtc.GetMemory(address, buff, count);
if (gotten != count ||
count != sizeof(data) - 1) // remove the extra null terminator strings add
{
Serial.print("something didn't match, count = ");
Serial.print(count, DEC);
Serial.print(", gotten = ");
Serial.print(gotten, DEC);
Serial.println();
}
Serial.print("data read (");
Serial.print(gotten);
Serial.print(") = \"");
for (uint8_t ch = 0; ch < gotten; ch++)
{
Serial.print((char)buff[ch]);
}
Serial.println("\"");
}
delay(5000);
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,115 @@
// Reference for connecting SPI see https://www.arduino.cc/en/Reference/SPI
// CONNECTIONS:
// DS3234 MISO --> MISO
// DS3234 MOSI --> MOSI
// DS3234 CLK --> CLK (SCK)
// DS3234 CS (SS) --> 5 (pin used to select the DS3234 on the SPI)
// DS3234 VCC --> 3.3v or 5v
// DS3234 GND --> GND
const uint8_t DS3234_CS_PIN = 5;
#include <SPI.h>
#include <RtcDS3234.h>
RtcDS3234<SPIClass> Rtc(SPI, DS3234_CS_PIN);
void setup ()
{
Serial.begin(115200);
while (!Serial);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
SPI.begin();
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue
Rtc.SetDateTime(compiled);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3234SquareWavePin_ModeNone);
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
RtcTemperature temp = Rtc.GetTemperature();
temp.Print(Serial);
// you may also get the temperature as a float and print it
// Serial.print(temp.AsFloatDegC());
Serial.println("C");
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,269 @@
// CONNECTIONS:
// PCF8563 SDA --> SDA
// PCF8563 SCL --> SCL
// PCF8563 VCC --> 3.3v or 5v
// PCF8563 GND --> GND
// PCF8563 INT ---> (Pin19) Don't forget to pullup (4.7k to 10k to VCC)
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <RtcPCF8563.h>
SoftwareWire myWire(SDA, SCL);
RtcPCF8563<SoftwareWire> Rtc(myWire);
for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcPCF8563.h>
RtcPCF8563<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
// Interrupt Pin Lookup Table
// (copied from Arduino Docs)
//
// CAUTION: The interrupts are Arduino numbers NOT Atmel numbers
// and may not match (example, Mega2560 int.4 is actually Atmel Int2)
// this is only an issue if you plan to use the lower level interupt features
//
// Board int.0 int.1 int.2 int.3 int.4 int.5
// ---------------------------------------------------------------
// Uno, Ethernet 2 3
// Mega2560 2 3 21 20 [19] 18
// Leonardo 3 2 0 1 7
#define RtcInterruptPin 19 // Mega2560
#define RtcInterruptInterrupt 4 // Mega2560
// marked volatile so interrupt can safely modify them and
// other code can safely read and modify them
volatile uint16_t interuptCount = 0;
volatile bool interuptFlag = false;
void ISR_ATTR InteruptServiceRoutine()
{
// since this interupted any other running code,
// don't do anything that takes long and especially avoid
// any communications calls within this routine
interuptCount++;
interuptFlag = true;
}
// handy routine to return true if there was an error
// but it will also print out an error message with the given topic
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void setup ()
{
Serial.begin(115200);
// set the interupt pin to input mode
pinMode(RtcInterruptPin, INPUT);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
Serial.println("RTC is older than compile time, updating DateTime");
Rtc.SetDateTime(compiled);
}
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.StopAlarm();
wasError("setup StopAlarm");
Rtc.StopTimer();
wasError("setup StopTimer");
Rtc.SetSquareWavePin(PCF8563SquareWavePinMode_None);
wasError("setup SetSquareWavePin");
// Alarm set to trigger every day when
// the hours and minutes match
RtcDateTime alarmTime = now + 88; // into the future
PCF8563Alarm alarm(
alarmTime.Day(), // will be ignored in this example
alarmTime.Hour(),
alarmTime.Minute(),
alarmTime.DayOfWeek(), // will be ignored in this example
PCF8563AlarmControl_MinuteMatch | PCF8563AlarmControl_HourMatch
);
Rtc.SetAlarm(alarm);
wasError("setup SetAlarm");
// Timer set to trigger in 100 seconds
//
Rtc.SetTimer(PCF8563TimerMode_Seconds, 100);
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
// setup external interupt
// for some Arduino hardware they use interrupt number for the first param
attachInterrupt(RtcInterruptInterrupt, InteruptServiceRoutine, FALLING);
#else
// for some Arduino hardware they use interrupt pin for the first param
attachInterrupt(RtcInterruptPin, InteruptServiceRoutine, FALLING);
#endif
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
if (!wasError("loop IsDateTimeValid"))
{
Serial.println("RTC lost confidence in the DateTime!");
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("loop GetDateTime"))
{
printDateTime(now);
Serial.println();
}
// we only want to show time every 10 seconds
// but we want to show responce to the interupt firing
for (int timeCount = 0; timeCount < 20; timeCount++)
{
if (Alarmed())
{
Serial.print(">>Interupt Count: ");
Serial.print(interuptCount);
Serial.println("<<");
}
delay(500);
}
}
bool Alarmed()
{
bool result = false;
if (interuptFlag) // check our flag that gets sets in the interupt
{
interuptFlag = false; // reset the flag
// calling LatchAlarmTriggeredFlag() will return if
// the alarm was what triggered the interrupt and also
// resets the alarm flag inside the rtc which then allows
// for alarms to trigger again.
// note that the same int pin is also used for
// the timer trigger if also used
bool wasAlarmed = Rtc.LatchAlarmTriggeredFlag();
if (!wasError("alarmed LatchAlarmTriggeredFlag"))
{
if (wasAlarmed)
{
result = true;
Serial.println("alarm triggered");
}
}
// calling LatchTimerTriggeredFlag() will return if
// the timer was what triggered the interrupt and also
// resets the timer flag inside the rtc which then allows
// for timers to trigger again.
// note that the same int pin is also used for
// the alarm trigger if also used
bool wasTimerExpired = Rtc.LatchTimerTriggeredFlag();
if (!wasError("alarmed LatchTimerTriggeredFlag"))
{
if (wasTimerExpired)
{
result = true;
Serial.println("timer triggered");
}
}
}
return result;
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}

View File

@@ -0,0 +1,181 @@
// CONNECTIONS:
// PCF8563 SDA --> SDA
// PCF8563 SCL --> SCL
// PCF8563 VCC --> 3.3v or 5v
// PCF8563 GND --> GND
/* for software wire use below
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
#include <RtcPCF8563.h>
SoftwareWire myWire(SDA, SCL);
RtcPCF8563<SoftwareWire> Rtc(myWire);
for software wire use above */
/* for normal hardware wire use below */
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcPCF8563.h>
RtcPCF8563<TwoWire> Rtc(Wire);
/* for normal hardware wire use above */
// handy routine to return true if there was an error
// but it will also print out an error message with the given topic
bool wasError(const char* errorTopic = "")
{
uint8_t error = Rtc.LastError();
if (error != 0)
{
// we have a communications error
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
// for what the number means
Serial.print("[");
Serial.print(errorTopic);
Serial.print("] WIRE communications error (");
Serial.print(error);
Serial.print(") : ");
switch (error)
{
case Rtc_Wire_Error_None:
Serial.println("(none?!)");
break;
case Rtc_Wire_Error_TxBufferOverflow:
Serial.println("transmit buffer overflow");
break;
case Rtc_Wire_Error_NoAddressableDevice:
Serial.println("no device responded");
break;
case Rtc_Wire_Error_UnsupportedRequest:
Serial.println("device doesn't support request");
break;
case Rtc_Wire_Error_Unspecific:
Serial.println("unspecified error");
break;
case Rtc_Wire_Error_CommunicationTimeout:
Serial.println("communications timed out");
break;
}
return true;
}
return false;
}
void setup ()
{
Serial.begin(115200);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
#if defined(WIRE_HAS_TIMEOUT)
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
#endif
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
if (!wasError("setup IsDateTimeValid"))
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
if (!wasError("setup GetIsRunning"))
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("setup GetDateTime"))
{
if (now < compiled)
{
Serial.println("RTC is older than compile time, updating DateTime");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time, this is expected");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time, while not expected all is still fine");
}
}
// never assume the Rtc was last configured by you, so
// just clear them to your needed state
Rtc.StopAlarm();
wasError("setup StopAlarm");
Rtc.StopTimer();
wasError("setup StopTimer");
Rtc.SetSquareWavePin(PCF8563SquareWavePinMode_None);
wasError("setup SetSquareWavePin");
}
void loop ()
{
if (!Rtc.IsDateTimeValid())
{
if (!wasError("loop IsDateTimeValid"))
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
}
RtcDateTime now = Rtc.GetDateTime();
if (!wasError("loop GetDateTime"))
{
printDateTime(now);
Serial.println();
}
delay(10000); // ten seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[26];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}