pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

PowerHub Button

API and example programs related to PowerHub Button.

Example Program

Compile Requirements

  • M5Stack board manager version >= 3.2.3
  • Board selection = M5PowerHub
  • M5Unified library version >= 0.2.11
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "M5Unified.h"

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

void loop() {
  M5.update();

  if (M5.BtnA.wasPressed()) {
    Serial.println("BtnA was pressed");
  }
  if (M5.BtnA.wasReleased()) {
    Serial.println("BtnA was released");
  }
  if (M5.BtnA.wasSingleClicked()) {
    Serial.println("BtnA was single clicked");
  }
  if (M5.BtnA.wasDoubleClicked()) {
    Serial.println("BtnA was double clicked");
  }
  if (M5.BtnA.wasHold()) {
    Serial.println("BtnA was held");
  }
  if (M5.BtnA.wasReleaseFor(5000)) {  // ms
    Serial.println("BtnA was released after being held for 5000 ms");
  }

  if (M5.BtnB.wasPressed()) {
    Serial.println("BtnB was pressed");
  }
  if (M5.BtnB.wasReleased()) {
    Serial.println("BtnB was released");
  }
  if (M5.BtnB.wasSingleClicked()) {
    Serial.println("BtnB was single clicked");
  }
  if (M5.BtnB.wasDoubleClicked()) {
    Serial.println("BtnB was double clicked");
  }
  if (M5.BtnB.wasHold()) {
    Serial.println("BtnB was held");
  }
  if (M5.BtnB.wasReleaseFor(5000)) {  // ms
    Serial.println("BtnB was released after being held for 5000 ms");
  }

  delay(10);
}

This program detects the status of two buttons on the device (the yellow round button as BtnA and the rectangular translucent button as BtnB), including states such as pressed, released, single short press, double short press, long press, and release after holding for 5000 milliseconds. The messages are printed in the Serial Monitor:

API

The PowerHub Button driver uses the Button_Class from the M5Unified library. For more related APIs, refer to the following documentation:

On This Page