This commit is contained in:
2025-06-18 09:21:10 +08:00
commit b2b79996d7
478 changed files with 82728 additions and 0 deletions

View File

@ -0,0 +1,122 @@
/* Example implementation of an alarm using DS3231
*
* VCC and GND of RTC should be connected to some power source
* SDA, SCL of RTC should be connected to SDA, SCL of arduino
* SQW should be connected to CLOCK_INTERRUPT_PIN
* CLOCK_INTERRUPT_PIN needs to work with interrupts
*/
#include <RTClib.h>
// #include <Wire.h>
RTC_DS3231 rtc;
// the pin that is connected to SQW
#define CLOCK_INTERRUPT_PIN 2
void setup() {
Serial.begin(9600);
// initializing the rtc
if(!rtc.begin()) {
Serial.println("Couldn't find RTC!");
Serial.flush();
while (1) delay(10);
}
if(rtc.lostPower()) {
// this will adjust to the date and time at compilation
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
//we don't need the 32K Pin, so disable it
rtc.disable32K();
// Making it so, that the alarm will trigger an interrupt
pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);
// set alarm 1, 2 flag to false (so alarm 1, 2 didn't happen so far)
// if not done, this easily leads to problems, as both register aren't reset on reboot/recompile
rtc.clearAlarm(1);
rtc.clearAlarm(2);
// stop oscillating signals at SQW Pin
// otherwise setAlarm1 will fail
rtc.writeSqwPinMode(DS3231_OFF);
// turn off alarm 2 (in case it isn't off already)
// again, this isn't done at reboot, so a previously set alarm could easily go overlooked
rtc.disableAlarm(2);
// schedule an alarm 10 seconds in the future
if(!rtc.setAlarm1(
rtc.now() + TimeSpan(10),
DS3231_A1_Second // this mode triggers the alarm when the seconds match. See Doxygen for other options
)) {
Serial.println("Error, alarm wasn't set!");
}else {
Serial.println("Alarm will happen in 10 seconds!");
}
}
void loop() {
// print current time
char date[10] = "hh:mm:ss";
rtc.now().toString(date);
Serial.print(date);
// the stored alarm value + mode
DateTime alarm1 = rtc.getAlarm1();
Ds3231Alarm1Mode alarm1mode = rtc.getAlarm1Mode();
char alarm1Date[12] = "DD hh:mm:ss";
alarm1.toString(alarm1Date);
Serial.print(" [Alarm1: ");
Serial.print(alarm1Date);
Serial.print(", Mode: ");
switch (alarm1mode) {
case DS3231_A1_PerSecond: Serial.print("PerSecond"); break;
case DS3231_A1_Second: Serial.print("Second"); break;
case DS3231_A1_Minute: Serial.print("Minute"); break;
case DS3231_A1_Hour: Serial.print("Hour"); break;
case DS3231_A1_Date: Serial.print("Date"); break;
case DS3231_A1_Day: Serial.print("Day"); break;
}
// the value at SQW-Pin (because of pullup 1 means no alarm)
Serial.print("] SQW: ");
Serial.print(digitalRead(CLOCK_INTERRUPT_PIN));
// whether a alarm fired
Serial.print(" Fired: ");
Serial.print(rtc.alarmFired(1));
// Serial.print(" Alarm2: ");
// Serial.println(rtc.alarmFired(2));
// control register values (see https://datasheets.maximintegrated.com/en/ds/DS3231.pdf page 13)
// Serial.print(" Control: 0b");
// Serial.println(read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL), BIN);
// resetting SQW and alarm 1 flag
// using setAlarm1, the next alarm could now be configurated
if (rtc.alarmFired(1)) {
rtc.clearAlarm(1);
Serial.print(" - Alarm cleared");
}
Serial.println();
delay(2000);
}
void onAlarm() {
Serial.println("Alarm occured!");
}
/*static uint8_t read_i2c_register(uint8_t addr, uint8_t reg) {
Wire.beginTransmission(addr);
Wire.write((byte)reg);
Wire.endTransmission();
Wire.requestFrom(addr, (byte)1);
return Wire.read();
}*/

View File

@ -0,0 +1,120 @@
/* Using DS3231 (or other supported RTC) with a custom TwoWire instance
*
* If using a microcontroller which supports additional i2c ports,
* such as the SAMD21's SERCOMX, a user can define a custom i2c bus
* to use with an RTC.
* This example builds the custom i2c bus using SERCOM0 and leverages the "wiring_private.h" APIs
*
* Connecting the device:
* VCC and GND of RTC should be connected to some power source
* SDA, SCL of RTC should be connected to the custom SDA and SCL pins.
* In this particular example we are using a Nano 33 IoT and routing
* the custom Wire instance over pins 6 (SDA) and 5 (SCL)
*
* This example will work with Arduino Zero, any Arduino MKR board based on SAMD21, Nano 33 IoT
* and any board by Adafruit, Sparkfun, Seeed Studio based on the same microcontroller
*
*/
#include <Wire.h>
#include "wiring_private.h"
#include <RTClib.h>
/* Defining the custom TwoWire instance for SAMD21 */
TwoWire myWire(&sercom0, 6, 5); // Create the new wire instance assigning it to pin 0 and 1
extern "C"{
void SERCOM0_Handler(void);
void SERCOM0_Handler(void) {
myWire.onService();
}
}
/* Creating a new DS3231 object */
RTC_DS3231 myRTC;
String daysNames[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
String monthsNames[] = {
"-",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
void setup() {
Serial.begin(57600);
Serial.println("start");
unsigned long setupStartTime = millis();
/*** Waiting for Serial to be ready or timeout ***/
while(!Serial && millis() - setupStartTime < 3000);
/*
* Initialising pins 6 and 5 to be routed to the SERCOM0 pads 0 and 1 in order
* to be used as SDA and SCL. Without this step the periphearl won't be patched through
*/
pinPeripheral(6, PIO_SERCOM_ALT); // PAD[0] //Assign SDA function to pin 0
pinPeripheral(5, PIO_SERCOM_ALT); // PAD[1] //Assign SCL function to pin 1
/* We now pass our custom TwoWire object to the RTC instance */
myRTC.begin(&myWire);
/*
* From this moment on every operation on the RTC will work as expected
* But the i2c bus being used will be the one we manually created using SERCOM0
*/
/*
* Creating a Date object with
* YEAR, MONTH, DAY (2021, January, 1)
* HOUR, MINUTE, SECONDS (0, 0, 0)
* Midnight of January 1st, 2021
*/
DateTime newDT = DateTime(2021, 1, 1, 0, 0, 0);
/* Pushing that date/time to the RTC */
myRTC.adjust(newDT);
Serial.println("setup done");
}
void loop() {
/* creating a temporary date/time object to store the data coming from the RTC */
DateTime dt = myRTC.now();
/* printing that data to the Serial port in a meaningful format */
Serial.println("************");
Serial.print(daysNames[dt.dayOfTheWeek()]);
Serial.print(" ");
Serial.print(monthsNames[dt.month()]);
Serial.print(" ");
Serial.print(dt.day());
Serial.print(", ");
Serial.println(dt.year());
Serial.print(dt.hour());
Serial.print(":");
Serial.print(dt.minute());
Serial.print(":");
Serial.println(dt.second());
/* Delays are bad, but let's not flood the Serial for this silly example */
delay(500);
}

View File

@ -0,0 +1,105 @@
// Simple date conversions and calculations
#include "RTClib.h"
void showDate(const char* txt, const DateTime& dt) {
Serial.print(txt);
Serial.print(' ');
Serial.print(dt.year(), DEC);
Serial.print('/');
Serial.print(dt.month(), DEC);
Serial.print('/');
Serial.print(dt.day(), DEC);
Serial.print(' ');
Serial.print(dt.hour(), DEC);
Serial.print(':');
Serial.print(dt.minute(), DEC);
Serial.print(':');
Serial.print(dt.second(), DEC);
Serial.print(" = ");
Serial.print(dt.unixtime());
Serial.print("s / ");
Serial.print(dt.unixtime() / 86400L);
Serial.print("d since 1970");
Serial.println();
}
void showTimeSpan(const char* txt, const TimeSpan& ts) {
Serial.print(txt);
Serial.print(" ");
Serial.print(ts.days(), DEC);
Serial.print(" days ");
Serial.print(ts.hours(), DEC);
Serial.print(" hours ");
Serial.print(ts.minutes(), DEC);
Serial.print(" minutes ");
Serial.print(ts.seconds(), DEC);
Serial.print(" seconds (");
Serial.print(ts.totalseconds(), DEC);
Serial.print(" total seconds)");
Serial.println();
}
void setup () {
Serial.begin(57600);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
DateTime dt0 (0, 1, 1, 0, 0, 0);
showDate("dt0", dt0);
DateTime dt1 (1, 1, 1, 0, 0, 0);
showDate("dt1", dt1);
DateTime dt2 (2009, 1, 1, 0, 0, 0);
showDate("dt2", dt2);
DateTime dt3 (2009, 1, 2, 0, 0, 0);
showDate("dt3", dt3);
DateTime dt4 (2009, 1, 27, 0, 0, 0);
showDate("dt4", dt4);
DateTime dt5 (2009, 2, 27, 0, 0, 0);
showDate("dt5", dt5);
DateTime dt6 (2009, 12, 27, 0, 0, 0);
showDate("dt6", dt6);
DateTime dt7 (dt6.unixtime() + 3600); // One hour later.
showDate("dt7", dt7);
DateTime dt75 = dt6 + TimeSpan(0, 1, 0, 0); // One hour later with TimeSpan addition.
showDate("dt7.5", dt75);
DateTime dt8 (dt6.unixtime() + 86400L); // One day later.
showDate("dt8", dt8);
DateTime dt85 = dt6 + TimeSpan(1, 0, 0, 0); // One day later with TimeSpan addition.
showDate("dt8.5", dt85);
DateTime dt9 (dt6.unixtime() + 7 * 86400L); // One week later.
showDate("dt9", dt9);
DateTime dt95 = dt6 + TimeSpan(7, 0, 0, 0); // One week later with TimeSpan addition.
showDate("dt9.5", dt95);
DateTime dt10 = dt6 + TimeSpan(0, 0, 42, 42); // Fourty two minutes and fourty two seconds later.
showDate("dt10", dt10);
DateTime dt11 = dt6 - TimeSpan(7, 0, 0, 0); // One week ago.
showDate("dt11", dt11);
TimeSpan ts1 = dt6 - dt5;
showTimeSpan("dt6-dt5", ts1);
TimeSpan ts2 = dt10 - dt6;
showTimeSpan("dt10-dt6", ts2);
}
void loop () {
}

View File

@ -0,0 +1,82 @@
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
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);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
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(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days, 12 hours, 30 minutes, and 6 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 12h + 30m + 6s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}

View File

@ -0,0 +1,63 @@
// SQW/OUT pin mode using a DS1307 RTC connected via I2C.
//
// According to the data sheet (http://datasheets.maxim-ic.com/en/ds/DS1307.pdf), the
// DS1307's SQW/OUT pin can be set to low, high, 1Hz, 4.096kHz, 8.192kHz, or 32.768kHz.
//
// This sketch reads the state of the pin, then iterates through the possible values at
// 5 second intervals.
//
// NOTE:
// You must connect a pull up resistor (~10kohm) from the SQW pin up to VCC. Without
// this pull up the wave output will not work!
#include "RTClib.h"
RTC_DS1307 rtc;
int mode_index = 0;
Ds1307SqwPinMode modes[] = { DS1307_OFF, DS1307_ON, DS1307_SquareWave1HZ, DS1307_SquareWave4kHz, DS1307_SquareWave8kHz, DS1307_SquareWave32kHz};
void print_mode() {
Ds1307SqwPinMode mode = rtc.readSqwPinMode();
Serial.print("Sqw Pin Mode: ");
switch(mode) {
case DS1307_OFF: Serial.println("OFF"); break;
case DS1307_ON: Serial.println("ON"); break;
case DS1307_SquareWave1HZ: Serial.println("1Hz"); break;
case DS1307_SquareWave4kHz: Serial.println("4.096kHz"); break;
case DS1307_SquareWave8kHz: Serial.println("8.192kHz"); break;
case DS1307_SquareWave32kHz: Serial.println("32.768kHz"); 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);
}

View File

@ -0,0 +1,62 @@
// Example of using the non-volatile RAM storage on the DS1307.
// You can write up to 56 bytes from address 0 to 55.
// Data will be persisted as long as the DS1307 has battery power.
#include "RTClib.h"
RTC_DS1307 rtc;
void printnvram(uint8_t address) {
Serial.print("Address 0x");
Serial.print(address, HEX);
Serial.print(" = 0x");
Serial.println(rtc.readnvram(address), HEX);
}
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 old RAM contents on startup.
Serial.println("Current NVRAM values:");
for (int i = 0; i < 6; ++i) {
printnvram(i);
}
// Write some bytes to non-volatile RAM storage.
// NOTE: You can only read and write from addresses 0 to 55 (i.e. 56 byte values).
Serial.println("Writing NVRAM values.");
// Example writing one byte at a time:
rtc.writenvram(0, 0xFE);
rtc.writenvram(1, 0xED);
// Example writing multiple bytes:
uint8_t writeData[4] = { 0xBE, 0xEF, 0x01, 0x02 };
rtc.writenvram(2, writeData, 4);
// Read bytes from non-volatile RAM storage.
Serial.println("Reading NVRAM values:");
// Example reading one byte at a time.
Serial.println(rtc.readnvram(0), HEX);
Serial.println(rtc.readnvram(1), HEX);
// Example reading multiple bytes:
uint8_t readData[4] = {0};
rtc.readnvram(readData, 4, 2);
Serial.println(readData[0], HEX);
Serial.println(readData[1], HEX);
Serial.println(readData[2], HEX);
Serial.println(readData[3], HEX);
}
void loop () {
// Do nothing in the loop.
}

View File

@ -0,0 +1,86 @@
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
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);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
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(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days, 12 hours, 30 minutes, 6 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 12h + 30m + 6s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.print("Temperature: ");
Serial.print(rtc.getTemperature());
Serial.println(" C");
Serial.println();
delay(3000);
}

View File

@ -0,0 +1,100 @@
/***********************************************************************
Combining RTClib with the avr-libc timing functions
===================================================
The standard way of getting the current time and date with RTClib is to
call the now() method of the appropriate RTC class. This, however, is
somewhat slow, as it involves communicating with the RTC through the I2C
bus. An alternative, more lightweight method involves configuring the
RTC to deliver one pulse per second to an interrupt pin, and counting
the seconds in the interrupt handler. The timekeeping is then entirely
handled in the Arduino, with no I2C communication with the RTC other
than during the initialization phase.
On AVR-based Arduinos (Uno, Nano, Micro, ...), the Arduino core library
is built on top of avr-libc, which is an implementation of the standard
C library for the AVR platform. This library provides the standard C
functions for handling time:[1] time(), gmtime(), mktime(), etc. The
time() function is normally used to retrieve the current time from the
operating system, but since we have no operating system, the avr-libc
provides its own non-standard functions for implementing a time source:
- set_system_time() initializes the library's idea of the current time
- system_tick() steps the system time by one second.
This sketch demonstrates how to combine RTClib and avr-libc in order to
handle the timekeeping entirely on the Arduino from an interrupt
delivered by the RTC:
- RTClib is used to configure the RTC and retrieve the initial time
- avr-libc is used for regular timekeeping
This sketch only works on AVR-based Arduinos, as it relies on
non-standard functions provided by avr-libc.
[1] https://www.nongnu.org/avr-libc/user-manual/group__avr__time.html
***********************************************************************/
#include <RTClib.h>
#include <time.h> // standard C timing functions
// Pin receiving the one-pulse-per-second signal from the RTC.
// This should be an interrupt-capable pin.
const uint8_t pin1pps = 2;
// We will use the PCF8523 RTC, which has the handy "Second Timer".
// Other RTCs could be used with their "square wave" output configured
// to 1 Hz.
RTC_PCF8523 rtc;
void setup() {
// Initialize the serial port.
Serial.begin(57600);
while (!Serial); // wait for serial port to connect. Needed for native USB
// Initialize the RTC.
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (!rtc.initialized() || rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
rtc.deconfigureAllTimers(); // undo previous configuration, if any
// Initialize the system time from the RTC time. Both avr-libc and
// DateTime::secondstime() use the start of year 2000 as their
// reference "epoch".
set_system_time(rtc.now().secondstime());
// Keep the time in sync using the one-pulse-per-second output of the
// RTC as an interrupt source and calling system_tick() from the
// interrupt service routine.
pinMode(pin1pps, INPUT_PULLUP);
rtc.enableSecondTimer();
attachInterrupt(digitalPinToInterrupt(pin1pps), system_tick, FALLING);
}
void loop() {
// From here on, we only use the standard C timing functions.
// time() returns the current time as a single number of type time_t,
// this is the number of seconds elapsed since a reference "epoch".
time_t now = time(nullptr);
// gmtime() converts the time to a broken-down form (year, month...)
// similar to the DateTime class. Unlike localtime(), it doesn't
// attempt timezone conversions.
struct tm *broken_down_time = gmtime(&now);
// asctime() returns a textual representation of the date and time as
// a C string (pointer to a character array). The format is similar to
// the DateTime::toString() format "DDD MMM DD hh:mm:ss YYYY".
Serial.println(asctime(broken_down_time));
delay(1000);
}

View File

@ -0,0 +1,115 @@
// Date and time functions using a PCF8523 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_PCF8523 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
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);
}
if (! rtc.initialized() || rtc.lostPower()) {
Serial.println("RTC is NOT initialized, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
//
// Note: allow 2 seconds after inserting battery or applying external power
// without battery before calling adjust(). This gives the PCF8523's
// crystal oscillator time to stabilize. If you call adjust() very quickly
// after the RTC is powered, lostPower() may still return true.
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
// When the RTC was stopped and stays connected to the battery, it has
// to be restarted by clearing the STOP bit. Let's do this to ensure
// the RTC is running.
rtc.start();
// The PCF8523 can be calibrated for:
// - Aging adjustment
// - Temperature compensation
// - Accuracy tuning
// The offset mode to use, once every two hours or once every minute.
// The offset Offset value from -64 to +63. See the Application Note for calculation of offset values.
// https://www.nxp.com/docs/en/application-note/AN11247.pdf
// The deviation in parts per million can be calculated over a period of observation. Both the drift (which can be negative)
// and the observation period must be in seconds. For accuracy the variation should be observed over about 1 week.
// Note: any previous calibration should cancelled prior to any new observation period.
// Example - RTC gaining 43 seconds in 1 week
float drift = 43; // seconds plus or minus over oservation period - set to 0 to cancel previous calibration.
float period_sec = (7 * 86400); // total obsevation period in seconds (86400 = seconds in 1 day: 7 days = (7 * 86400) seconds )
float deviation_ppm = (drift / period_sec * 1000000); // deviation in parts per million (μs)
float drift_unit = 4.34; // use with offset mode PCF8523_TwoHours
// float drift_unit = 4.069; //For corrections every min the drift_unit is 4.069 ppm (use with offset mode PCF8523_OneMinute)
int offset = round(deviation_ppm / drift_unit);
// rtc.calibrate(PCF8523_TwoHours, offset); // Un-comment to perform calibration once drift (seconds) and observation period (seconds) are correct
// rtc.calibrate(PCF8523_TwoHours, 0); // Un-comment to cancel previous calibration
Serial.print("Offset is "); Serial.println(offset); // Print to control offset
}
void loop () {
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(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days, 12 hours and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 12h + 30m + 6s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}

View File

@ -0,0 +1,164 @@
/**************************************************************************/
/*
Countdown Timer using a PCF8523 RTC connected via I2C and Wire lib
with the INT/SQW pin wired to an interrupt-capable input.
According to the data sheet, the PCF8523 can run countdown timers
from 244 microseconds to 10.625 days:
https://www.nxp.com/docs/en/data-sheet/PCF8523.pdf#page=34
This sketch sets a countdown timer, and executes code when it reaches 0,
then blinks the built-in LED like BlinkWithoutDelay, but without millis()!
NOTE:
You must connect the PCF8523's interrupt pin to your Arduino or other
microcontroller on an input pin that can handle interrupts, and that has a
pullup resistor. The pin will be briefly pulled low each time the countdown
reaches 0. This example will not work without the interrupt pin connected!
On Adafruit breakout boards, the interrupt pin is labeled 'INT' or 'SQW'.
*/
/**************************************************************************/
#include "RTClib.h"
RTC_PCF8523 rtc;
// Input pin with interrupt capability
// const int timerInterruptPin = 2; // Most Arduinos
const int timerInterruptPin = 5; // Adafruit Feather M0/M4/nRF52840
// Variables modified during an interrupt must be declared volatile
volatile bool countdownInterruptTriggered = false;
volatile int numCountdownInterrupts = 0;
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);
}
pinMode(LED_BUILTIN, OUTPUT);
// Set the pin attached to PCF8523 INT to be an input with pullup to HIGH.
// The PCF8523 interrupt pin will briefly pull it LOW at the end of a given
// countdown period, then it will be released to be pulled HIGH again.
pinMode(timerInterruptPin, INPUT_PULLUP);
Serial.println(F("\nStarting PCF8523 Countdown Timer example."));
Serial.print(F("Configured to expect PCF8523 INT/SQW pin connected to input pin: "));
Serial.println(timerInterruptPin);
Serial.println(F("This example will not work without the interrupt pin connected!\n\n"));
// Timer configuration is not cleared on an RTC reset due to battery backup!
rtc.deconfigureAllTimers();
Serial.println(F("First, use the PCF8523's 'Countdown Timer' with an interrupt."));
Serial.println(F("Set the countdown for 10 seconds and we'll let it run for 2 rounds."));
Serial.println(F("Starting Countdown Timer now..."));
// These are the PCF8523's built-in "Timer Source Clock Frequencies".
// They are predefined time periods you choose as your base unit of time,
// depending on the length of countdown timer you need.
// The minimum length of your countdown is 1 time period.
// The maximum length of your countdown is 255 time periods.
//
// PCF8523_FrequencyHour = 1 hour, max 10.625 days (255 hours)
// PCF8523_FrequencyMinute = 1 minute, max 4.25 hours
// PCF8523_FrequencySecond = 1 second, max 4.25 minutes
// PCF8523_Frequency64Hz = 1/64 of a second (15.625 milliseconds), max 3.984 seconds
// PCF8523_Frequency4kHz = 1/4096 of a second (244 microseconds), max 62.256 milliseconds
//
//
// These are the PCF8523's optional 'Low Pulse Widths' of time the interrupt
// pin is held LOW at the end of every countdown (frequency 64Hz or longer).
//
// PCF8523_LowPulse3x64Hz = 46.875 ms 3/64ths second (default)
// PCF8523_LowPulse4x64Hz = 62.500 ms 4/64ths second
// PCF8523_LowPulse5x64Hz = 78.125 ms 5/64ths second
// PCF8523_LowPulse6x64Hz = 93.750 ms 6/64ths second
// PCF8523_LowPulse8x64Hz = 125.000 ms 8/64ths second
// PCF8523_LowPulse10x64Hz = 156.250 ms 10/64ths second
// PCF8523_LowPulse12x64Hz = 187.500 ms 12/64ths second
// PCF8523_LowPulse14x64Hz = 218.750 ms 14/64ths second
//
//
// Uncomment an example below:
// rtc.enableCountdownTimer(PCF8523_FrequencyHour, 24); // 1 day
// rtc.enableCountdownTimer(PCF8523_FrequencyMinute, 150); // 2.5 hours
rtc.enableCountdownTimer(PCF8523_FrequencySecond, 10); // 10 seconds
// rtc.enableCountdownTimer(PCF8523_Frequency64Hz, 32); // 1/2 second
// rtc.enableCountdownTimer(PCF8523_Frequency64Hz, 16); // 1/4 second
// rtc.enableCountdownTimer(PCF8523_Frequency4kHz, 205); // 50 milliseconds
attachInterrupt(digitalPinToInterrupt(timerInterruptPin), countdownOver, FALLING);
// This message proves we're not blocked while counting down!
Serial.println(F(" While we're waiting, a word of caution:"));
Serial.println(F(" When starting a new countdown timer, the first time period is not of fixed"));
Serial.println(F(" duration. The amount of inaccuracy for the first time period is up to one full"));
Serial.println(F(" clock frequency. Example: just the first second of the first round of a new"));
Serial.println(F(" countdown based on PCF8523_FrequencySecond may be off by as much as 1 second!"));
Serial.println(F(" For critical timing, consider starting actions on the first interrupt."));
}
// Triggered by the PCF8523 Countdown Timer interrupt at the end of a countdown
// period. Meanwhile, the PCF8523 immediately starts the countdown again.
void countdownOver () {
// Set a flag to run code in the loop():
countdownInterruptTriggered = true;
numCountdownInterrupts++;
}
// Triggered by the PCF8523 Second Timer every second.
void toggleLed () {
// Run certain types of fast executing code here:
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
void loop () {
if (countdownInterruptTriggered && numCountdownInterrupts == 1) {
Serial.println(F("1st countdown interrupt triggered. Accurate timekeeping starts now."));
countdownInterruptTriggered = false; // don't come in here again
} else if (countdownInterruptTriggered && numCountdownInterrupts == 2) {
Serial.println(F("2nd countdown interrupt triggered. Disabling countdown and detaching interrupt.\n\n"));
rtc.disableCountdownTimer();
detachInterrupt(digitalPinToInterrupt(timerInterruptPin));
delay(2000);
Serial.println(F("Now, set up the PCF8523's 'Second Timer' to toggle the built-in LED at 1Hz..."));
attachInterrupt(digitalPinToInterrupt(timerInterruptPin), toggleLed, FALLING);
rtc.enableSecondTimer();
Serial.println(F("Look for the built-in LED to flash 1 second ON, 1 second OFF, repeat. "));
Serial.println(F("Meanwhile this program will use delay() to block code execution briefly"));
Serial.println(F("before moving on to the last example. Notice the LED keeps blinking!\n\n"));
delay(20000); // less accurate, blocks execution here. Meanwhile Second Timer keeps running.
rtc.disableSecondTimer();
detachInterrupt(digitalPinToInterrupt(timerInterruptPin));
Serial.println(F("Lastly, set up a Countdown Timer that works without attaching an interrupt..."));
rtc.enableCountdownTimer(PCF8523_Frequency64Hz, 32, PCF8523_LowPulse8x64Hz);
Serial.println(F("Look for the LED to turn on every 1/2 second and stay lit for 1/8th of a second."));
Serial.println(F("The countdown was set to a source clock frequency of 64 Hz (1/64th of a second)"));
Serial.println(F("for a length of 32 time periods. 32 * 1/64th of a second is 1/2 of a second."));
Serial.println(F("The low pulse duration was set to 125 ms, or 1/8th of a second."));
Serial.println(F("The loop() keeps the built-in LED set to the opposite state of the INT/SQW pin."));
countdownInterruptTriggered = false; // don't come in here again
}
// While countdown running, INT/SQW pullup to HIGH, set LED to LOW (off)
// When countdown is over, INT/SQW pulled down LOW, set LED to HIGH (on)
digitalWrite(LED_BUILTIN, !digitalRead(timerInterruptPin));
}

View File

@ -0,0 +1,92 @@
// Date and time functions using a PCF8563 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_PCF8563 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(115200);
#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);
}
if (rtc.lostPower()) {
Serial.println("RTC is NOT initialized, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
//
// Note: allow 2 seconds after inserting battery or applying external power
// without battery before calling adjust(). This gives the PCF8523's
// crystal oscillator time to stabilize. If you call adjust() very quickly
// after the RTC is powered, lostPower() may still return true.
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
// When the RTC was stopped and stays connected to the battery, it has
// to be restarted by clearing the STOP bit. Let's do this to ensure
// the RTC is running.
rtc.start();
}
void loop () {
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(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days, 12 hours and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 12h + 30m + 6s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}

View File

@ -0,0 +1,89 @@
// Date and time functions using a PCF8563 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_PCF8563 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// use D2 for INT0; attach to CLKOUT pin on RTC
const uint8_t INT_PIN = 2;
// flag to update serial; set in interrupt callback
volatile uint8_t tick_tock = 1;
// INT0 interrupt callback; update tick_tock flag
void set_tick_tock(void) {
tick_tock = 1;
}
void setup () {
Serial.begin(115200);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
pinMode(INT_PIN, INPUT); // set up interrupt pin
digitalWrite(INT_PIN, HIGH); // turn on pullup resistors
// attach interrupt to set_tick_tock callback on rising edge of INT0
attachInterrupt(digitalPinToInterrupt(INT_PIN), set_tick_tock, RISING);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC is NOT initialized, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
//
// Note: allow 2 seconds after inserting battery or applying external power
// without battery before calling adjust(). This gives the PCF8523's
// crystal oscillator time to stabilize. If you call adjust() very quickly
// after the RTC is powered, lostPower() may still return true.
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
// When the RTC was stopped and stays connected to the battery, it has
// to be restarted by clearing the STOP bit. Let's do this to ensure
// the RTC is running.
rtc.start();
// turn on 1Hz clock out, used as INT0 for serial update every second
rtc.writeSqwPinMode(PCF8563_SquareWave1Hz);
}
void loop () {
// check if time display should be output
if(tick_tock) {
DateTime now = rtc.now();
char time_format[] = "hh:mm:ss AP";
char date_format[] = "MM/DD/YYYY";
Serial.println(now.toString(time_format));
Serial.println(now.toString(date_format));
Serial.println();
tick_tock = 0;
}
}

View File

@ -0,0 +1,59 @@
// Date and time functions using just software, based on millis() & timer
#include "RTClib.h"
RTC_Millis rtc;
void setup () {
Serial.begin(57600);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
// following line sets the RTC to the date & time this sketch was compiled
rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
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(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" seconds since 1970: ");
Serial.println(now.unixtime());
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}

View File

@ -0,0 +1,62 @@
/* Timestamp functions using a DS1307 RTC connected via I2C and Wire lib
**
** Useful for file name
** ` SD.open(time.timestamp()+".log", FILE_WRITE) `
**
**
** Created: 2015-06-01 by AxelTB
** Last Edit:
*/
#include "RTClib.h"
RTC_DS1307 rtc;
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);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop() {
DateTime time = rtc.now();
//Full Timestamp
Serial.println(String("DateTime::TIMESTAMP_FULL:\t")+time.timestamp(DateTime::TIMESTAMP_FULL));
//Date Only
Serial.println(String("DateTime::TIMESTAMP_DATE:\t")+time.timestamp(DateTime::TIMESTAMP_DATE));
//Full Timestamp
Serial.println(String("DateTime::TIMESTAMP_TIME:\t")+time.timestamp(DateTime::TIMESTAMP_TIME));
Serial.println("\n");
//Delay 5s
delay(5000);
}

View File

@ -0,0 +1,66 @@
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
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);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop() {
DateTime now = rtc.now();
//buffer can be defined using following combinations:
//hh - the hour with a leading zero (00 to 23)
//mm - the minute with a leading zero (00 to 59)
//ss - the whole second with a leading zero where applicable (00 to 59)
//YYYY - the year as four digit number
//YY - the year as two digit number (00-99)
//MM - the month as number with a leading zero (01-12)
//MMM - the abbreviated English month name ('Jan' to 'Dec')
//DD - the day as number with a leading zero (01 to 31)
//DDD - the abbreviated English day name ('Mon' to 'Sun')
char buf1[] = "hh:mm";
Serial.println(now.toString(buf1));
char buf2[] = "YYMMDD-hh:mm:ss";
Serial.println(now.toString(buf2));
char buf3[] = "Today is DDD, MMM DD YYYY";
Serial.println(now.toString(buf3));
char buf4[] = "MM-DD-YYYY";
Serial.println(now.toString(buf4));
delay(1000);
}