Zum Hauptinhalt springen

Real-time clock

What this example shows​

Set the time and date on the onboard real-time clock (RTC), read them back once a second and show them on the screen. The RTC keeps counting on its own, so the sketch only writes the time once, in setup().

The screen is redrawn with partial updates in 1-bit (black and white) mode. After ten partial updates the sketch does a full refresh to keep the image quality good.

Example code​

In the Arduino IDE, choose the Inkplate 10 board under Tools, Board, Inkplate Boards before you upload.

Select Board
Inkplate10_RTC_Simple.ino
// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif

#include "Inkplate.h" // Include Inkplate library to the sketch
Inkplate display(INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1-bit mode (BW)

#define REFRESH_DELAY 1000 // Delay between refreshes
unsigned long time1; // Time for measuring refresh in millis

// 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 = 4;
uint8_t day = 11;
uint8_t month = 11;
uint16_t year = 2021;

void setup()
{
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
display.clearDisplay(); // Clear frame buffer of display
display.display(); // Put clear image on display
display.setTextSize(5); // Set text to be 5 times bigger than classic 5x7 px text

display.rtc.setTime(hour, minutes, seconds); // Send time to RTC
display.rtc.setDate(weekday, day, month, year); // Send date to RTC
}

// Variable that keeps count on how much screen has been partially updated
int n = 0;
void loop()
{
if ((unsigned long)(millis() - time1) > REFRESH_DELAY)
{
display.rtc.getRtcData(); // Get the time and date from RTC
seconds = display.rtc.getSecond(); // Store senconds in a variable
minutes = display.rtc.getMinute(); // Store minutes in a variable
hour = display.rtc.getHour(); // Store hours in a variable
day = display.rtc.getDay(); // Store day of month in a variable
weekday = display.rtc.getWeekday(); // Store day of week in a variable
month = display.rtc.getMonth(); // Store month in a variable
year = display.rtc.getYear(); // Store year in a variable

display.clearDisplay(); // Clear content in frame buffer
display.setCursor(100, 300); // Set position of the text
printTime(hour, minutes, seconds, day, weekday, month, year); // Print the time on screen

if (n > 9) // Check if you need to do full refresh or you can do partial update
{
display.display(true); // Do a full refresh
n = 0;
}
else
{
display.partialUpdate(false, true); // Do partial update and keep e-papr power supply on
n++; // Keep track on how many times screen has been partially updated
}

time1 = millis(); // Store current millis
}
}

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);
display.print(':');
print2Digits(_minutes);
display.print(':');
print2Digits(_seconds);

display.print(' ');

display.print(wday[_weekday]);
display.print(", ");
print2Digits(_day);
display.print('/');
print2Digits(_month);
display.print('/');
display.print(_year, DEC);
}

void print2Digits(uint8_t _d)
{
if (_d < 10)
display.print('0');
display.print(_d, DEC);
}

Trimmed from Inkplate10_RTC_Simple.ino: the file header comment is left out. Adapted: the pinned file stores the year as a two-digit number (21), but setDate() expects the full year and subtracts 2000 from it, so this page uses 2021-style values and a uint16_t year. The rest of the code is unchanged.

Functions used​

Inkplate(uint8_t mode)returns None

Creates the Inkplate object and chooses the display mode. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` is grayscale and only supports full updates.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
uint8_tmodeDisplay mode: INKPLATE_1BIT (0) or INKPLATE_3BIT (1).
display.begin()returns void

Initializes the library, the peripherals and the frame buffers. Call it once in `setup()`, later calls are ignored.

ReturnsNothing.

display.clearDisplay()returns void

Clears the frame buffer in memory. It does not clear the panel itself, call `display()` afterwards to do that.

ReturnsNothing.

display.display(bool _leaveOn = 0)returns void

Refreshes the whole panel from the frame buffer (a full update).

ReturnsNothing.

Parameters

TypeNameDescription
bool_leaveOnIf true, the e-paper supply stays on after the update, which saves time on the next update. Defaults to 0.
display.setTextSize(uint8_t s)returns void

Sets the text magnification. Size 1 is the classic 5x7 pixel font, size 5 is five times bigger. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tsText size multiplier.
display.rtc.setTime(uint8_t hour, uint8_t minute, uint8_t sec, bool isPM = false)returns void

Writes the time to the onboard real-time clock (RTC). It also marks the RTC as set, which `isSet()` reports.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_thourHour, in 24-hour form unless the RTC is switched to 12-hour mode.
uint8_tminuteMinute.
uint8_tsecSecond.
boolisPMOnly used in 12-hour mode. Defaults to false.
display.rtc.setDate(uint8_t weekday, uint8_t day, uint8_t month, uint16_t yr)returns void

Writes the date to the RTC. It also marks the RTC as set.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tweekdayDay of the week, 0 to 6. The example sketches print 0 as Sunday and 1 as Monday.
uint8_tdayDay of the month.
uint8_tmonthMonth, 1 to 12.
uint16_tyrFull year, for example 2022. The function subtracts 2000 before writing it to the RTC.
display.rtc.getRtcData()returns void

Reads the time and date from the RTC.

ReturnsNothing.

display.rtc.getSecond()returns uint8_t

Returns the current second from the RTC.

ReturnsSeconds.

display.rtc.getMinute()returns uint8_t

Returns the current minute from the RTC.

ReturnsMinutes.

display.rtc.getHour()returns uint8_t

Returns the current hour from the RTC.

ReturnsHours.

display.rtc.getDay()returns uint8_t

Returns the current day of the month from the RTC.

ReturnsDay of the month.

display.rtc.getWeekday()returns uint8_t

Returns the current day of the week from the RTC.

ReturnsDay of the week, 0 to 6.

display.rtc.getMonth()returns uint8_t

Returns the current month from the RTC.

ReturnsMonth, 1 to 12.

display.rtc.getYear()returns uint16_t

Returns the current year from the RTC.

ReturnsFull year, for example `2022`.

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

Sets where the next printed text starts, in pixels from the top left corner. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
int16_txX coordinate in pixels.
int16_tyY coordinate in pixels.
display.partialUpdate(bool _forced = false, bool leaveOn = false)returns uint32_t

Updates only the parts of the screen that changed, without a full refresh. It works in 1-bit mode only, in 3-bit mode it does nothing and returns 0. A full refresh every 5 to 10 partial updates keeps the image quality good.

ReturnsNumber of pixels that changed from black to white, which are the ones that leave visible blur.

Parameters

TypeNameDescription
bool_forcedFor advanced use with deep sleep: forces a partial update. Defaults to false.
boolleaveOnIf true, the e-paper supply stays on after the update. Defaults to false.
display.print(value, format)returns size_t

Prints text, a character or a number at the cursor position, in the frame buffer. The display object builds on the Adafruit GFX base class, which extends the Arduino `Print` class, so it prints like `Serial` does. Call `display()` or `partialUpdate()` to show the result.

ReturnsNumber of bytes printed.

Parameters

TypeNameDescription
text, char or numbervalueWhat to print.
intformatOptional. Number base for integers, for example DEC.
millis()returns unsigned long

Arduino function that returns the milliseconds since the board started.

ReturnsMilliseconds since start.

What to expect​

The current time and date appear once a second in large text, in the form HH:MM:SS Weekday, DD/MM/YYYY. With the values in the sketch it starts at 12:50:30 Thursday, 11/11/2021. Weekday 0 is Sunday. The sketch does not print to Serial.