Deep sleep with the wake button
What this example shows
Wake the Inkplate 10 from deep sleep with the WakeUp button, or let a 30 second timer do it. Every time the board wakes, it shows how many times it has started and what woke it up.
The sketch enables two wake-up sources before sleeping: a timer, and an external signal (called EXT0) on GPIO_NUM_36, which the sketch comments name as the wake button. The wake-up level is LOW. The boot counter is kept in RTC memory with RTC_DATA_ATTR, so it survives deep sleep.
Example code
In the Arduino IDE, choose the Inkplate 10 board under Tools, Board, Inkplate Boards before you upload.
// 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>
// Conversion factor for micro seconds to seconds
#define uS_TO_S_FACTOR 1000000ULL
// Time ESP32 will go to sleep (in seconds)
#define TIME_TO_SLEEP 30
// Initiate Inkplate object
Inkplate display(INKPLATE_1BIT);
// Store int in rtc data, to remain persistent during deep sleep
RTC_DATA_ATTR int bootCount = 0;
void setup()
{
display.begin();
++bootCount;
// Our function declared below
displayInfo();
// Go to sleep for TIME_TO_SLEEP seconds
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// Enable wakeup from deep sleep on gpio 36 (wake button)
esp_sleep_enable_ext0_wakeup(GPIO_NUM_36, LOW);
// Go to sleep
esp_deep_sleep_start();
}
void loop()
{
// Never here! If you use deep sleep, the whole program should be in setup() because the board restarts each
// time. loop() must be empty!
}
// Function that will write number of boots and boot reason to screen
void displayInfo()
{
// First, lets delete everything from frame buffer
display.clearDisplay();
// Set text cursor and size
display.setCursor(10, 280);
display.setTextSize(2);
display.print(F("Boot count: "));
display.println(bootCount, DEC); // Print the number
// Set next line cursor position
display.setCursor(10, 320);
// Display wake up reason
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0:
display.println("Wakeup caused by WakeUp button");
break;
case ESP_SLEEP_WAKEUP_TIMER:
display.println("Wakeup caused by timer");
break;
default:
display.println("Wakeup was not caused by deep sleep");
break;
}
display.display();
}
Trimmed from Inkplate10_Wake_Up_Button.ino: the file header comment is left out. The code is otherwise unchanged.
Functions used
Inkplate(uint8_t mode)returns NoneCreates 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
| Type | Name | Description |
|---|---|---|
uint8_t | mode | Display mode: INKPLATE_1BIT (0) or INKPLATE_3BIT (1). |
display.begin()returns voidInitializes the library, the peripherals and the frame buffers. Call it once in `setup()`, later calls are ignored.
ReturnsNothing.
display.clearDisplay()returns voidClears the frame buffer in memory. It does not clear the panel itself, call `display()` afterwards to do that.
ReturnsNothing.
display.setCursor(int16_t x, int16_t y)returns voidSets 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
| Type | Name | Description |
|---|---|---|
int16_t | x | X coordinate in pixels. |
int16_t | y | Y coordinate in pixels. |
display.setTextSize(uint8_t s)returns voidSets 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
| Type | Name | Description |
|---|---|---|
uint8_t | s | Text size multiplier. |
display.print(value, format)returns size_tPrints 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
| Type | Name | Description |
|---|---|---|
text, char or number | value | What to print. |
int | format | Optional. Number base for integers, for example DEC. |
display.println(value, format)returns size_tSame as `print()`, then moves the cursor to the start of the next line.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text, char or number | value | What to print. Optional: with no value it only moves to the next line. |
int | format | Optional. Number base for integers, for example DEC. |
display.display(bool _leaveOn = 0)returns voidRefreshes the whole panel from the frame buffer (a full update).
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
bool | _leaveOn | If true, the e-paper supply stays on after the update, which saves time on the next update. Defaults to 0. |
esp_sleep_get_wakeup_cause()returns esp_sleep_wakeup_cause_tReturns what woke the ESP32 up. The sketch checks for `ESP_SLEEP_WAKEUP_EXT0` (the pin) and `ESP_SLEEP_WAKEUP_TIMER` (the timer). It comes from the ESP32 Arduino core, not from the Inkplate library.
ReturnsThe wake-up cause.
esp_sleep_enable_timer_wakeup(uint64_t time_in_us)returns esp_err_tSets a timer that wakes the ESP32 from deep sleep. It comes from the ESP32 Arduino core, not from the Inkplate library.
ReturnsError code, not checked by the sketch.
Parameters
| Type | Name | Description |
|---|---|---|
uint64_t | time_in_us | Time until wake-up, in microseconds. The sketch multiplies seconds by uS_TO_S_FACTOR (1000000). |
esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)returns esp_err_tMakes a change on one pin wake the ESP32 from deep sleep. It comes from the ESP32 Arduino core, not from the Inkplate library.
ReturnsError code, not checked by the sketch.
Parameters
| Type | Name | Description |
|---|---|---|
gpio_num_t | gpio_num | Pin that wakes the board, for example GPIO_NUM_36. |
int | level | Pin level that causes the wake-up: LOW (0) or HIGH (1). |
esp_deep_sleep_start()returns voidPuts the ESP32 into deep sleep. The program stops here, and on wake-up it starts again from the beginning of `setup()`. It comes from the ESP32 Arduino core, not from the Inkplate library.
ReturnsDoes not return.
What to expect
After upload, the screen shows Boot count: 1 and Wakeup was not caused by deep sleep, then the board goes to sleep. Press the WakeUp button, or wait 30 seconds. The count goes up by one and the second line reads either Wakeup caused by WakeUp button or Wakeup caused by timer. The sketch does not use Serial.