RTC timer
What this example shows
Set the time and a 30-second countdown timer on the onboard PCF85063A real-time clock (RTC), show the time on the screen once a minute, and print Timer! in red once the timer has run out. The timer is used once: the sketch switches it off after it has fired.
The sketch sets ESP32 GPIO 2, the RTC interrupt pin, as an input with the pull-up resistor enabled.
Example code
#include "Inkplate.h" // Include Inkplate library to the sketch
Inkplate inkplate; // Create an object on Inkplate library
// Set clock
uint8_t hour = 12;
uint8_t minutes = 50;
uint8_t seconds = 30;
// Set date and weekday (NOTE: In weekdays 0 means Sunday, 1 means Monday, ...)
uint8_t weekday = 1;
uint8_t day = 20;
uint8_t month = 7;
uint8_t year = 26;
// Set up a 30 seconds timer
int countdown_time = 30;
void setup()
{
inkplate.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
inkplate.rtc.reset(); // reset RTC if there is some data in it
inkplate.clearDisplay(); // Clear frame buffer of display
inkplate.display(); // Put clear image on display
inkplate.setTextSize(4); // Set text to be 4 times bigger than classic 5x7 px text
pinMode(2, INPUT_PULLUP); // Set RTC INT pin on ESP32 GPIO2 as input with pullup resistor enabled
inkplate.rtc.setTime(hour, minutes, seconds); // Send time to RTC
inkplate.rtc.setDate(weekday, day, month, year); // Send date to RTC
// Set up a timer
/* source_clock
* RTC::TIMER_CLOCK_4096HZ -> clk = 4096Hz -> min timer = 244uS -> MAX timer = 62.256mS
* RTC::TIMER_CLOCK_64HZ -> clk = 64Hz -> min timer = 15.625mS -> MAX timer = 3.984s
* RTC::TIMER_CLOCK_1HZ -> clk = 1Hz -> min timer = 1s -> MAX timer = 255s
* RTC::TIMER_CLOCK_1PER60HZ -> clk = 1/60Hz -> min timer = 60s -> MAX timer = 4h15min
* value
* coundowntime in seconds
* int_enable
* true = enable interrupt; false = disable interrupt
* int_pulse
* true = interrupt generate a pulse; false = interrupt follows timer flag
*/
inkplate.rtc.timerSet(RTC::TIMER_CLOCK_1HZ, countdown_time, true, false);
}
void loop()
{
inkplate.rtc.getRtcData(); // Get the time and date from RTC
seconds = inkplate.rtc.getSecond(); // Store senconds in a variable
minutes = inkplate.rtc.getMinute(); // Store minutes in a variable
hour = inkplate.rtc.getHour(); // Store hours in a variable
day = inkplate.rtc.getDay(); // Store day of month in a variable
weekday = inkplate.rtc.getWeekday(); // Store day of week in a variable
month = inkplate.rtc.getMonth(); // Store month in a variable
year = inkplate.rtc.getYear(); // Store year in a variable
inkplate.clearDisplay(); // Clear content in frame buffer
inkplate.setCursor(60, 300); // Set position of the text
inkplate.setTextColor(INKPLATE_BLACK, INKPLATE_WHITE); // Set text color and background
printTime(hour, minutes, seconds, day, weekday, month, year); // Print the time on screen
if (inkplate.rtc.checkTimerFlag()) // Check if timer event has occurred
{
inkplate.rtc.clearTimerFlag(); // It's recommended to clear timer flag after timer has occurred
inkplate.rtc.disableTimer(); // Disable timer if you want to make it one time only. Is you want to be repeatable, comment this line
inkplate.setCursor(400, 400); // Set new position for cursor
inkplate.setTextColor(INKPLATE_RED, INKPLATE_WHITE); // Set text color and background
inkplate.print("Timer!");
}
inkplate.display(); // Do a full refresh
delay(60000); // Delay between refreshes.
}
void printTime(uint8_t _hour, uint8_t _minutes, uint8_t _seconds, uint8_t _day, uint8_t _weekday, uint8_t _month,
uint16_t _year)
{
// Write time and date info on screen
char *wday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
print2Digits(_hour);
inkplate.print(':');
print2Digits(_minutes);
inkplate.print(':');
print2Digits(_seconds);
inkplate.print(' ');
inkplate.print(wday[_weekday]);
inkplate.print(", ");
print2Digits(_day);
inkplate.print('/');
print2Digits(_month);
inkplate.print('/');
inkplate.print(_year, DEC);
}
void print2Digits(uint8_t _d)
{
if (_d < 10)
inkplate.print('0');
inkplate.print(_d, DEC);
}
The sketch stores the year as 26, but rtc.setDate() expects the full year and subtracts 2000 from it, and rtc.getYear() returns the full year. So the year on the screen will not read 2026. To get the right year, pass the full year (for example 2026) to setDate() in a uint16_t variable.
This example also uses pinMode() and delay(), which are part of the Arduino core for the ESP32 and come with the ESP32 board package (arduino-esp32), not the Inkplate library.
Functions used
Inkplate()returns NoneCreates the Inkplate object. Inkplate 7SPECTRA has a single six-color mode, so the constructor takes no mode argument.
ReturnsConstructor; no return value.
inkplate.begin()returns voidInitializes the library, the peripherals and the frame buffer. Call it once in setup().
ReturnsNothing.
inkplate.rtc.reset()returns voidWrites the default value to the RTC's first control register, clearing settings left from an earlier sketch.
ReturnsNothing.
inkplate.clearDisplay()returns voidFills the frame buffer with white. It does not change the panel.
ReturnsNothing.
inkplate.display(bool _leaveOn = 0)returns voidSends the whole frame buffer to the panel and refreshes it.
ReturnsNothing.
inkplate.setTextSize(uint8_t s)returns voidSets the text magnification, here 4.
ReturnsNothing.
inkplate.rtc.setTime(uint8_t hour, uint8_t minute, uint8_t sec, bool isPM = false)returns voidWrites the time to the RTC. isPM is only used in 12-hour mode.
ReturnsNothing.
inkplate.rtc.setDate(uint8_t weekday, uint8_t day, uint8_t month, uint16_t yr)returns voidWrites the date to the RTC and marks it as set. Weekday 0 is Sunday. yr is the full year; the function subtracts 2000 before writing it.
ReturnsNothing.
inkplate.rtc.timerSet(rtcCountdownSrcClock source_clock, uint8_t value, bool int_enable, bool int_pulse)returns voidStarts the countdown timer. source_clock is the timer's clock: RTC::TIMER_CLOCK_4096HZ, RTC::TIMER_CLOCK_64HZ, RTC::TIMER_CLOCK_1HZ (1 s to 255 s) or RTC::TIMER_CLOCK_1PER60HZ (60 s to 4 h 15 min). value is the countdown in units of that clock, so seconds with TIMER_CLOCK_1HZ. int_enable true lets the timer raise the RTC interrupt. int_pulse true makes the interrupt a pulse; false makes it follow the timer flag. The sketch passes TIMER_CLOCK_1HZ, 30, true, false.
ReturnsNothing.
inkplate.rtc.getRtcData()returns voidReads the time and date from the RTC; the get functions below return the values it read.
ReturnsNothing.
inkplate.rtc.getSecond()returns uint8_tReturns the seconds.
ReturnsSeconds.
inkplate.rtc.getMinute()returns uint8_tReturns the minutes.
ReturnsMinutes.
inkplate.rtc.getHour()returns uint8_tReturns the hours.
ReturnsHours.
inkplate.rtc.getDay()returns uint8_tReturns the day of the month.
ReturnsDay of the month.
inkplate.rtc.getWeekday()returns uint8_tReturns the day of the week, 0 (Sunday) to 6.
ReturnsDay of the week.
inkplate.rtc.getMonth()returns uint8_tReturns the month.
ReturnsMonth, 1 to 12.
inkplate.rtc.getYear()returns uint16_tReturns the year: the value stored in the RTC plus 2000.
ReturnsFull year.
inkplate.setCursor(int16_t x, int16_t y)returns voidSets where the next printed text starts.
ReturnsNothing.
inkplate.setTextColor(uint16_t c, uint16_t bg)returns voidSets the text color and the background color drawn behind each character: black on white for the time, red on white for the timer message.
ReturnsNothing.
inkplate.rtc.checkTimerFlag()returns boolReports whether the timer has run out. It does not clear the flag.
Returnstrue once the timer flag is set.
inkplate.rtc.clearTimerFlag()returns voidClears the timer flag so the next timer event can be detected.
ReturnsNothing.
inkplate.rtc.disableTimer()returns voidStops the timer so it fires only once. Remove this line from the sketch if you want the timer to repeat.
ReturnsNothing.
inkplate.print(value, format)returns size_tPrints text, a character or a number into the frame buffer, like Serial.print(). DEC prints a number in decimal.
ReturnsNumber of bytes printed.
pinMode(uint8_t pin, uint8_t mode)returns voidSets a GPIO pin's mode. The sketch sets GPIO 2, the RTC interrupt pin, to INPUT_PULLUP.
ReturnsNothing.
delay(unsigned long ms)returns voidWaits the given number of milliseconds, here 60000 (one minute).
ReturnsNothing.
What to expect
The first screen shows the time and date in black in the form HH:MM:SS Weekday, DD/MM/ followed by the year. On the refresh a minute later, after the 30-second timer has run out, Timer! appears in red lower on the screen next to the new time. The sketch prints nothing to the Serial Monitor.
🔗Open the complete example
examples/Inkplate7SPECTRA/Advanced/RTC/Inkplate7SPECTRA_RTC_Timer