System

begin()

Functionality:

Initializes the touchscreen; initializes the TF Card; clears the serial buffer, sets the serial baud rate to 115200; reads battery ADC; initializes I2C.

Function Prototype:

void begin(bool touchEnable = true, bool SDEnable = true, bool SerialEnable = true, bool BatteryADCEnable = true, bool I2CEnable = false)

Function Implementation:

void M5EPD::begin(bool touchEnable, bool SDEnable, bool SerialEnable,
                  bool BatteryADCEnable, bool I2CEnable) {
    if (_isInited) {
        return;
    }
    _isInited = true;

    pinMode(M5EPD_MAIN_PWR_PIN, OUTPUT);
    enableMainPower();

    if (SerialEnable == true) {
        Serial.begin(115200);
        Serial.flush();
        delay(50);
        Serial.print("M5EPD initializing...");
    }

    pinMode(M5EPD_EXT_PWR_EN_PIN, OUTPUT);
    pinMode(M5EPD_EPD_PWR_EN_PIN, OUTPUT);
    pinMode(M5EPD_KEY_RIGHT_PIN, INPUT);
    pinMode(M5EPD_KEY_PUSH_PIN, INPUT);
    pinMode(M5EPD_KEY_LEFT_PIN, INPUT);
    delay(100);

    enableEXTPower();
    enableEPDPower();
    delay(1000);

    EPD.begin(M5EPD_SCK_PIN, M5EPD_MOSI_PIN, M5EPD_MISO_PIN, M5EPD_CS_PIN,
              M5EPD_BUSY_PIN);

    if (SDEnable == true) {
        SPI.begin(14, 13, 12, 4);
        SD.begin(4, SPI, 20000000);
    }

    if (touchEnable == true) {
        if (TP.begin(21, 22, 36) != ESP_OK) {
            log_e("Touch pad initialization failed.");
        }
    } else if (I2CEnable == true) {
        Wire.begin(21, 22, (uint32_t)400000U);
    }

    if (BatteryADCEnable == true) {
        BatteryADCBegin();
    }

    if (SerialEnable == true) {
        Serial.println("OK");
    }
}

Usage Example:

#include <M5EPD.h>

void setup() {
  M5.begin(true,true,true,true,true);
}

update()

Functionality:

Checks for button state changes.

Function Prototype:

void update()

Usage Example:

#include <M5EPD.h>
void setup() {
  M5.begin(false,false,true,false,false);
}

void loop() {
  M5.update();
  if(M5.BtnL.wasPressed()) {
    Serial.println("Left button pressed");
  }
  delay(100);
}

Button

Relevant Pin Definitions

Definition Pin Description
M5EPD_KEY_LEFT_PIN 37 Scroll wheel up
M5EPD_KEY_PUSH_PIN 38 Scroll wheel push
M5EPD_KEY_RIGHT_PIN 39 Scroll wheel down
M5EPD_PORTA_W_PIN 32 PORT-A
M5EPD_PORTA_Y_PIN 25 PORT-A
M5EPD_PORTA_W_PIN 32 PORT-A
M5EPD_PORTA_Y_PIN 25 PORT-A
M5EPD_PORTB_W_PIN 33 PORT-B
M5EPD_PORTB_Y_PIN 26 PORT-B
M5EPD_PORTC_W_PIN 19 PORT-C
M5EPD_PORTC_Y_PIN 18 PORT-C
M5EPD_PORTC_Y_PIN 18 PORT-C
M5EPD_MISO_PIN 13 IT8951E/TF-card MISO
M5EPD_MOSI_PIN 12 IT8951E/TF-card MOSI
M5EPD_SCK_PIN 14 IT8951E/TF-card SCK
M5EPD_CS_PIN 15 IT8951E CS
M5EPD_MAIN_PWR_PIN 2 PWR
M5EPD_SCK_PIN 14 IT8951E/TF-card SCK
M5EPD_SCK_PIN 14 IT8951E/TF-card

SCK | | M5EPD_BUSY_PIN | 27 | / | | M5EPD_EXT_PWR_EN_PIN | 5 | / | | M5EPD_EPD_PWR_EN_PIN | 23 | / | | M5EPD_BAT_VOL_PIN | 35 | / |

lastChange()

Functionality:

Returns the time of the last state change.

Function Prototype:

uint32_t lastChange()

Note:
1. The time returned is counted from the moment M5Paper is initialized, in milliseconds.

Usage Example:

#include <M5EPD.h>

void setup() {
  M5.begin();
  Serial.println("Please press Button L.");
}

void loop() {
  M5.update();
  Serial.printf("The last change at %d ms /n",M5.BtnL.lastChange());    //Print the time of the last state change of key L.
}

isPressed()

Functionality:

Returns the button press status: if the button is pressed, returns true; otherwise, returns false.

Function Prototype:

uint8_t isPressed()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.println("Please press Button L.");
}

void loop() {
    M5.update(); // Need to add M5.update() to read the state of the button.
    if (M5.BtnL.isPressed()) {  // If the button is pressed.
        Serial.println("Button is Pressed.");
    } else {
        Serial.println("Button is Released.");
    }
    delay(20);
}

pressedFor()

Functionality:

Returns the button press status: if the button is pressed for longer than the specified time, returns true; otherwise, returns false.

Function Prototype:

uint8_t pressedFor(uint32_t ms)

Parameter Type Description
ms uint32_t Button press time (milliseconds)

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.println("Please press Button L.");
}

void loop() {
    M5.update();
    if (M5.BtnL.pressedFor(
            2000)) {  // If the button is pressed for more than 2 seconds.
        Serial.println("Button L was pressed for more than 2 seconds.");
        delay(1000);
    }
}

wasPressed()

Functionality:

Returns the button press status: if the button was pressed, it returns true once; otherwise, it returns false.

Function Prototype:

uint8_t wasPressed()

Usage Example:

#include <M5EPD.h>

void setup() {
  M5.begin();
  Serial.println("Please press Button L.");
}

void loop() {
  M5.update();
  if (M5.BtnL.wasPressed()) {   //If the button is pressed.
    Serial.println("Button is pressed.");
  }
  delay(20);
}

Released

isReleased()

Functionality:

Returns the button release status: if the button is released, returns true; otherwise, returns false.

Function Prototype:

uint8_t isPressed()

Usage Example:

#include <M5EPD.h>

void setup() { M5.begin(); }

void loop() {
    M5.update();  // Need to add M5.update() to read the state of the button.
    if (M5.BtnL.isReleased()) {  //If the button is released.
        Serial.println("Button is released.");
    } else {
        Serial.println("Button is Pressed.");
    }
    delay(20);
}

releasedFor()

Functionality:

Returns the button release status: if the button is released for longer than the specified time, returns true; otherwise, returns false.

Function Prototype:

uint8_t pressedFor(uint32_t ms)

Parameter Type Description
ms uint32_t Button release time (milliseconds)

Usage Example:

#include <M5EPD.h>

void setup() { M5.begin(); }

void loop() {
    M5.update();  // Need to add M5.update() to read the state of the button.
    if (M5.BtnL.isReleased()) {  //If the button is released.
        Serial.println("Button is released.");
    } else {
        Serial.println("Button is pressed.");
    }
    delay(20);
}

wasReleased()

Function:

Returns the key release state: if a key is released, it returns true once, otherwise it returns false.

Function Prototype:

uint8_t wasReleased()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.println("Please pressed Button L.");
}

void loop() {
    M5.update();
    if (M5.BtnL.wasReleased()) {  // If the key is released.
        Serial.println("Button is Released.");
    }
    delay(20);
}

wasReleasefor()

Function:

Returns the key release state: if a key is pressed and then released after exceeding a specified time, it returns true once, otherwise it returns false.

Function Prototype:

uint8_t wasReleasefor(uint32_t ms)

Parameter Type Description
ms uint32_t Press time (milliseconds)

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.println("Please pressed Button L.");
}

void loop() {
    M5.update();
    if (M5.BtnL.wasReleasefor(3000)) {  // If button A is pressed for 3s and then released.
        Serial.println("OK");
    }
}

Power

enableEXTPower()

Function:

Enables external port power.

Function Prototype:

void enableEXTPower()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.enableEXTPower();  // Enable external port power.
    Serial.println("Expansion port power is on");
}

void loop() {}

disableEXTPower()

Function:

Disables external port power.

Function Prototype:

void disableEXTPower()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.disableEXTPower();  // Disable external port power.
    Serial.println("Expansion port power is off");
}

void loop() {}

enableEPDPower()

Function:

Enables E-Ink display power.

Function Prototype:

void enableEPDPower()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.enableEPDPower();  // Enable E-Ink display power.
    Serial.println("Ink screen power is activated");
}

void loop() {}

disableEPDPower()

Function:

Disables E-Ink display power.

Function Prototype:

void disableEPDPower()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.disableEPDPower();  // Disable E-Ink display power.
    Serial.println("Ink screen power off");
}

void loop() {}

enableMainPower()

Function:

Enables main power.

Function Prototype:

void enableMainPower()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.enableMainPower();  // Start main power.
    Serial.println("Main power is on");
}

void loop() {}

disableMainPower()

Function:

Disables main power.

Function Prototype:

void disableMainPower()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.disableMainPower();  // Turn off the main power.
    Serial.println("Main power is off");
}

void loop() {}

BatteryADCBegin()

Function:

Initializes battery ADC detection.

Function Prototype:

void BatteryADCBegin()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    M5.BatteryADCBegin();  // Initialize battery ADC detection.
}

void loop() {}

getBatteryRaw()

Function:

Reads the battery voltage native ADC value.

Function Prototype:

uint32_t getBatteryRaw()

Usage Example:

#include <M5EPD.h>

void setup() {
    M5.begin();
    Serial.printf("ADC RAW:%d",
                  M5.getBatteryRaw());  // Read battery voltage native ADC values.
}

void loop() {}

getBatteryVoltage()

Function:

Reads the battery voltage.

Function Prototype:

uint32_t getBatteryVoltage()

Usage Example:

#include <M5EPD.h

>

void setup() {
    M5.begin();
    Serial.printf("Battery Voltage:%d",
                  M5.getBatteryVoltage());  // Read battery voltage.
}

void loop() {}

shutdown()

Function Overload 1:

Turns off the power; to restart, the PWR button must be pressed to wake up.

void shutdown()

Function Overload 2:

Turns off the power; according to the delay seconds passed in, the device is woken up by the RTC after the delay ends.

int shutdown(int seconds)

Function Overload 3:

Turns off the power; a specified RTC time structure is passed in, and the device is woken up by the RTC when the hour, minute, and second match.

int shutdown(const rtc_time_t &RTC_TimeStruct)

Function Overload 4:

Turns off the power; a specified RTC time structure is passed in, and the device is woken up by the RTC when the week, day, and time simultaneously match.

int shutdown(const rtc_date_t &RTC_DateStruct, const rtc_time_t &RTC_TimeStruct)

Usage Example:

#include <M5EPD.h>

M5EPD_Canvas canvas(&M5.EPD);

void setup(){
  M5.begin();
  M5.EPD.SetRotation(90);
  M5.TP.SetRotation(90);
  M5.EPD.Clear(true);
  M5.RTC.begin();
  canvas.createCanvas(540, 960);
  canvas.setTextSize(3);
  canvas.drawString("Press PWR Btn for sleep!", 45, 350);
  canvas.drawString("after 5 sec wakeup!", 70, 450);
  canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
}

void loop(){
  if(M5.BtnP.wasPressed()){
    canvas.drawString("I'm going to sleep.zzzZZZ~", 45, 550);
    canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
    delay(1000);
    M5.shutdown(5);
  }
  M5.update();
  delay(100);
}
On This Page