pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Cardputer Button

APIs and example programs for Cardputer button input, applicable to both Cardputer and Cardputer-Adv.

Buttons vs. Keyboard
On Cardputer and Cardputer-Adv, only the G0 button located on the top side supports the APIs described on this page. For the front keyboard, please refer to Keyboard.

Example Program

Compilation Requirements

  • M5Stack Board Manager version >= 3.2.2
  • Board option = M5Cardputer
  • M5Cardputer library version >= 1.1.0
  • M5Unified library version >= 0.2.8
  • M5GFX library version >= 0.2.10
Notes
When using this API, you need to include the M5Cardputer.update() function in the main loop to read state updates. Try to minimize blocking operations; otherwise, button state changes may not be detected in time.
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "M5Cardputer.h"

void setup() {
  auto cfg = M5.config();
  M5Cardputer.begin(cfg);
  Serial.begin(115200);
}

void loop() {
  M5Cardputer.update();

  if (M5Cardputer.BtnA.wasPressed()) {
    Serial.println("Button A Pressed");
  }
  if (M5Cardputer.BtnA.wasReleased()) {
    Serial.println("Button A Released");
  }
}

API

The M5Cardputer library is built on top of the M5Unified library. The Button function uses the Button_Class from the M5Unified library. For more related APIs, please refer to the following documentation:

On This Page