English
English
简体中文
日本語
pdf-icon

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Atom-Lite/Atom-Matrix Button

APIs and an example sketch for the front button on Atom-Lite / Atom-Matrix.

Note: Call M5.update() regularly in your main loop to refresh button state. Minimize blocking delays to ensure button events are captured promptly.

Example

Compilation Requirements

  • M5Stack Board Manager version >= 3.2.6
  • Development board option = M5Atom
  • 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");
    }
}

The sketch detects front-button events (press, release, single click, double click, hold) and prints example messages to the serial monitor, for example:

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

API

The button API is provided by the Button_Class in M5Unified. See:

On This Page