Zum Hauptinhalt springen

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.

Select Board
Inkplate10_Wake_Up_Button.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>

// 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 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.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.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.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.
display.println(value, format)returns size_t

Same as `print()`, then moves the cursor to the start of the next line.

ReturnsNumber of bytes printed.

Parameters

TypeNameDescription
text, char or numbervalueWhat to print. Optional: with no value it only moves to the next line.
intformatOptional. Number base for integers, for example DEC.
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.
esp_sleep_get_wakeup_cause()returns esp_sleep_wakeup_cause_t

Returns 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_t

Sets 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

TypeNameDescription
uint64_ttime_in_usTime 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_t

Makes 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

TypeNameDescription
gpio_num_tgpio_numPin that wakes the board, for example GPIO_NUM_36.
intlevelPin level that causes the wake-up: LOW (0) or HIGH (1).
esp_deep_sleep_start()returns void

Puts 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.