Skip to main content

RTC alarm with an interrupt

What this example shows​

Set the time and an alarm on the onboard PCF85063A real-time clock (RTC) from epoch values, then let the RTC signal the alarm on its interrupt pin. The sketch shows the time in red once a minute and prints ALARM when its interrupt function has run. The alarm is set 60 seconds after the start time.

The sketch reads the RTC interrupt on ESP32 GPIO 2, so it sets that pin as an input with the pull-up resistor enabled.

Example code​

Select Board
Inkplate7SPECTRA_RTC_Interrupt_Alarm.ino
#include "Inkplate.h" // Include Inkplate library to the sketch

Inkplate inkplate; // Create an object on Inkplate library and also set library into 1-bit mode (BW)

volatile int _alarmFlag = 0; // Variable to store alarm flag

void IRAM_ATTR alarmISR() // This function will be called when alarm interrupt event happens
{ // NOTE: Function must be above setup() and loop()!
_alarmFlag = 1; // Set alarm flag
}

void setup()
{
pinMode(2, INPUT_PULLUP); // Set RTC INT pin on ESP32 GPIO2 as input with pullup resistor enabled

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(3); // Set text to be 3 times bigger than classic 5x7 px text

// Set RTC time and date via Epoch
inkplate.rtc.setEpoch(1770032087);
// Set alarm using Epoch
inkplate.rtc.setAlarmEpoch(inkplate.rtc.getEpoch() + 60, RTC_ALARM_MATCH_DHHMMSS);

// inkplate.rtc.setTime(12, 40, 30); // Or you can use other way to set the time and date
// inkplate.rtc.setDate(1, 2, 2, 2026);
// inkplate.rtc.setAlarm(50, 40, 12, 2, 1); // Set alarm 20 seconds from now

attachInterrupt(2, alarmISR, FALLING); // Set interrupt function and interrupt mode
}

void loop()
{
inkplate.clearDisplay(); // Clear frame buffer of display
inkplate.setCursor(60, 100); // Set position of the text
inkplate.setTextColor(INKPLATE_RED, INKPLATE_WHITE); // Set text color and background
inkplate.rtc.getRtcData(); // Get the time and date from RTC

// Print the time on screen
printTime(inkplate.rtc.getHour(), inkplate.rtc.getMinute(), inkplate.rtc.getSecond(), inkplate.rtc.getDay(), inkplate.rtc.getWeekday(), inkplate.rtc.getMonth(), inkplate.rtc.getYear());

if (_alarmFlag) // Check alarm flag
{
// _alarmFlag = 0; // Uncomment if you want to clear this flag
inkplate.rtc.clearAlarmFlag(); // It's recommended to clear alarm flag after alarm has occurred
inkplate.setCursor(200, 200); // Set position of the text
inkplate.print("ALARM"); // Print text
}

inkplate.display(); // Do a full refresh

delay(60000); // Wait one minute
}

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 leaves the line that clears _alarmFlag commented out. Once the interrupt has run, every later refresh prints ALARM until you reset the board or uncomment that line.

This example also uses pinMode(), attachInterrupt() 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 None

Creates 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 void

Initializes the library, the peripherals and the frame buffer. Call it once in setup().

ReturnsNothing.

inkplate.rtc.reset()returns void

Writes the default value to the RTC's first control register, clearing settings left from an earlier sketch.

ReturnsNothing.

inkplate.clearDisplay()returns void

Fills the frame buffer with white. It does not change the panel.

ReturnsNothing.

inkplate.display(bool _leaveOn = 0)returns void

Sends the whole frame buffer to the panel and refreshes it.

ReturnsNothing.

inkplate.setTextSize(uint8_t s)returns void

Sets the text magnification, here 3.

ReturnsNothing.

inkplate.rtc.setEpoch(uint32_t _epoch)returns void

Sets the RTC time and date from an epoch value (seconds since 1 January 1970), here 1770032087.

ReturnsNothing.

inkplate.rtc.getEpoch()returns uint32_t

Reads the RTC and returns its time and date as an epoch value. The sketch adds 60 to it to get the alarm time.

ReturnsThe RTC time as an epoch value.

inkplate.rtc.setAlarmEpoch(uint32_t _epoch, uint8_t _match)returns void

Sets the alarm to the time given as an epoch value and enables the alarm interrupt. _match is one of the library's RTC_ALARM_MATCH_* values; the sketch passes RTC_ALARM_MATCH_DHHMMSS (day, hours, minutes and seconds). The other values are RTC_ALARM_MATCH_SS, RTC_ALARM_MATCH_MMSS, RTC_ALARM_MATCH_HHMMSS and RTC_ALARM_MATCH_WHHMMSS.

ReturnsNothing.

inkplate.setCursor(int16_t x, int16_t y)returns void

Sets where the next printed text starts.

ReturnsNothing.

inkplate.setTextColor(uint16_t c, uint16_t bg)returns void

Sets the text color and the background color drawn behind each character, here red on white.

ReturnsNothing.

inkplate.rtc.getRtcData()returns void

Reads the time and date from the RTC; the get functions below return the values it read.

ReturnsNothing.

inkplate.rtc.getHour()returns uint8_t

Returns the hours.

ReturnsHours.

inkplate.rtc.getMinute()returns uint8_t

Returns the minutes.

ReturnsMinutes.

inkplate.rtc.getSecond()returns uint8_t

Returns the seconds.

ReturnsSeconds.

inkplate.rtc.getDay()returns uint8_t

Returns the day of the month.

ReturnsDay of the month.

inkplate.rtc.getWeekday()returns uint8_t

Returns the day of the week, 0 (Sunday) to 6.

ReturnsDay of the week.

inkplate.rtc.getMonth()returns uint8_t

Returns the month.

ReturnsMonth, 1 to 12.

inkplate.rtc.getYear()returns uint16_t

Returns the year: the value stored in the RTC plus 2000.

ReturnsFull year.

inkplate.rtc.clearAlarmFlag()returns void

Clears the alarm flag in the RTC so the next alarm can be detected.

ReturnsNothing.

inkplate.print(value, format)returns size_t

Prints 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 void

Sets a GPIO pin's mode. The sketch sets GPIO 2, the RTC interrupt pin, to INPUT_PULLUP.

ReturnsNothing.

attachInterrupt(uint8_t pin, void (*function)(void), int mode)returns void

Runs a function when the pin changes. The sketch runs alarmISR() when GPIO 2 falls (FALLING), which sets _alarmFlag.

ReturnsNothing.

delay(unsigned long ms)returns void

Waits the given number of milliseconds, here 60000 (one minute).

ReturnsNothing.

What to expect​

The screen shows the time and date in red in the form HH:MM:SS Weekday, DD/MM/ followed by the year. About a minute later the screen refreshes with the current time, and once the alarm interrupt has fired ALARM appears below it. The sketch prints nothing to the Serial Monitor.

🔗Open the complete example

examples/Inkplate7SPECTRA/Advanced/RTC/Inkplate7SPECTRA_RTC_Interrupt_Alarm

→