pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Unit PoE-P4 Button

APIs and sample sketch for button input on Unit PoE-P4.

Note
When using the button API you must call M5.update() in the main loop to refresh the state, and avoid long blocking delays so that events are detected promptly.

Example

Compilation Requirements

  • M5Stack Board Manager version >= 3.2.6
  • Development board option = M5UnitPoEP4
  • M5Unified library version >= 0.2.13
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
#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");
    }
}

This sketch monitors the front button and prints messages to the serial monitor when it is pressed, released, clicked (single/double) or held. Example output:

BtnA was pressed
BtnA was released
BtnA was single clicked
BtnA was pressed
BtnA was held
BtnA was released

API

The Unit PoE-P4 button uses Button_Class from the M5Unified library. See the documentation for more details:

On This Page