Skip to main content

Deep sleep and the wake-up button

What this example shows​

Put the ESP32-S3 into deep sleep and wake it either after 120 seconds or when you press the wake-up button (IO18). On every wake-up the board starts again from setup(), counts the boot and shows why it woke up. The boot counter lives in RTC memory, which keeps its value through deep sleep.

This example also uses functions of the ESP32 board package (the ESP-IDF sleep modes), which are not part of the Inkplate library.

Example code​

Select Board
Inkplate7SPECTRA_Wake_Up_Button.ino
#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 120

// Initiate Inkplate object
Inkplate inkplate;

// Store int in rtc data, to remain persistent during deep sleep
RTC_DATA_ATTR int bootCount=0;

void setup() {
inkplate.begin();
inkplate.setTextColor(INKPLATE_BLACK);
++bootCount;

// Our function delcared 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 18 (wake button)
esp_sleep_enable_ext0_wakeup(GPIO_NUM_18, 0);
// Start deep sleep (this function does not return). Program stops here.
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!

}

void displayInfo(){
// First, lets delete everything from frame buffer
inkplate.clearDisplay();

// Set text cursor and size
inkplate.setCursor(10,180);
inkplate.setTextSize(1);

inkplate.print(F("Boot count: "));
inkplate.println(bootCount, DEC); // Print the number

// Set next line cursor position
inkplate.setCursor(10,220);

// 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:
inkplate.println("Wakeup caused by WakeUp button");
break;
case ESP_SLEEP_WAKEUP_TIMER:
inkplate.println("Wakeup caused by timer");
break;
default:
inkplate.println("Wakeuo was not caused by deep sleep");
break;
}
inkplate.display();

}

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. After deep sleep the board restarts, so setup() calls it again on every wake-up.

ReturnsNothing.

inkplate.setTextColor(uint16_t c)returns void

Sets the text color, here INKPLATE_BLACK.

ReturnsNothing.

inkplate.clearDisplay()returns void

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

ReturnsNothing.

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

Sets where the next printed text starts, in pixels from the top-left corner.

ReturnsNothing.

inkplate.setTextSize(uint8_t s)returns void

Sets the text magnification; 1 is the base 5×7 pixel font.

ReturnsNothing.

inkplate.print(value)returns size_t

Prints text at the cursor into the frame buffer, like Serial.print().

ReturnsNumber of bytes printed.

inkplate.println(value, format)returns size_t

Like print(), then moves the cursor to the next line. With DEC, a number is printed in decimal.

ReturnsNumber of bytes printed.

inkplate.display(bool _leaveOn = 0)returns void

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

ReturnsNothing.

esp_sleep_enable_timer_wakeup(uint64_t time_in_us)returns esp_err_t

ESP32 board package: wakes the chip from deep sleep after the given time in microseconds, here 120 × 1,000,000.

ReturnsESP_OK on success.

esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)returns esp_err_t

ESP32 board package: wakes the chip when the pin reaches the given level. The sketch uses GPIO_NUM_18, the wake-up button, and level 0 (low).

ReturnsESP_OK on success.

esp_deep_sleep_start()returns void

ESP32 board package: enters deep sleep. It does not return; the next wake-up starts the sketch from the beginning.

ReturnsDoes not return.

esp_sleep_get_wakeup_cause()returns esp_sleep_wakeup_cause_t

ESP32 board package: reports what woke the chip, for example ESP_SLEEP_WAKEUP_EXT0 (the button) or ESP_SLEEP_WAKEUP_TIMER.

ReturnsThe wake-up cause.

What to expect​

After upload the screen shows Boot count: 1 and Wakeuo was not caused by deep sleep (the spelling is the example's). The board then sleeps. Press the wake-up button and the count goes up with Wakeup caused by WakeUp button; leave it for 120 seconds and it shows Wakeup caused by timer. The sketch prints nothing to the Serial Monitor.

🔗Open the complete example

examples/Inkplate7SPECTRA/Advanced/DeepSleep/Inkplate7SPECTRA_Wake_Up_Button

→