🛠 Mastering GPIO I/O on M5Paper

M5Stack M5Paper · intermediaire · Apprenez à lire les boutons et à contrôler les périphériques via les broches GPIO du M5Paper.

Objectives

This tutorial aims to understand low-level interaction with the M5Paper. You will learn to identify available GPIO pins, configure a physical button as a digital input, and control an output (or simulate one) based on the button state. We will also explore e-Ink screen specifics to avoid unnecessary refreshes.

Required Material

Steps

  1. Environment Setup: Open Arduino IDE, go to *Preferences* and add the M5Stack board manager URL. Install the "M5Paper" board via the *Board Manager*.
  1. Pin Identification: The M5Paper has several accessible GPIOs. Physical buttons (BTN_A, BTN_B, BTN_C) are typically on GPIO 39, 38, and 37. Check the official schematic to confirm mappings for your revision.
  1. Code Writing: Create a new sketch. Include the M5Paper.h library. In setup(), initialize the board with M5.begin() and set the button pin to INPUT_PULLUP.
#include <M5Paper.h>

const int btnPin = 39; // Example for BTN_A

void setup() {
  M5.begin();
  pinMode(btnPin, INPUT_PULLUP);
  M5.Display.print("Press the button");
}
  1. Reading Logic: In loop(), read the button state with digitalRead(). Note that with INPUT_PULLUP, the state is LOW when pressed. Add a small delay (delay(50)) to avoid mechanical bouncing.
void loop() {
  if (digitalRead(btnPin) == LOW) {
    M5.Display.print("Button pressed!");
    delay(500);
  }
}
  1. E-Ink Management: The IT8951 screen is slow. Do not call M5.Display.update() in every loop. Use partial mode if available or limit full refreshes to preserve battery life.

Verification

Upload the code to the M5Paper. Press the corresponding physical button. The text on the screen should change. If nothing happens, check wiring or change the GPIO pin. Use the serial monitor to debug digitalRead values.

Going Further

Explore hardware interrupts (attachInterrupt) to react instantly to presses without polling the pin continuously, optimizing power consumption, which is crucial for battery-powered IoT applications.

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials