🛠 Controlling GPIOs with M5Stamp Timer Power 2
Objectives
This tutorial aims to understand basic interaction with the Input/Output (GPIO) pins of the M5Stamp Timer Power 2. We will configure one pin as an output to control an LED and another as an input to detect a button press. This module, based on the PY32L020 microcontroller, is optimized for low power, but its GPIOs remain accessible for simple logic control. It is essential to understand that power management (via the M5PM1 chip) influences pin states, especially during sleep modes. This course provides the basics to physically interact with the module without relying solely on I2C communication.
Required Hardware
- One M5Stamp Timer Power 2 module (SKU S021)
- A SWD-compatible programmer or a host board capable of I2C communication (address 0x6E)
- One red LED and a 220Ω resistor
- One tactile push button
- Connecting wires (Dupont)
- Breadboard
- USB-C cable for power supply
Steps
- Pin Identification: The module has 5 programmable IOs. Consult the datasheet to identify the physical pins corresponding to the desired GPIO functions. Note the format is a 28-pin castellated-hole package.
- LED Wiring: Connect the LED cathode (short leg) to Ground (GND) via the 220Ω resistor. Connect the anode (long leg) to a GPIO pin configured as output. Ensure the output voltage is compatible (3.3V via PY_3V3 or 3V3_LDO_OUT).
- Button Wiring: Connect one leg of the button to a GPIO pin configured as input (with internal pull-up resistor enabled if possible) and the other leg to Ground.
- Software Initialization: In your code (C/C++ or MicroPython), initialize the I2C bus to configure M5PM1 registers if needed to activate specific GPIOs. Set the LED pin to
OUTPUTmode and the button pin toINPUTmode. - Main Loop: Write a loop that reads the button state. If the button is pressed (low state due to pull-up), set the LED pin to HIGH to turn it on. Otherwise, set it to LOW.
Verification
Open the serial monitor or use a multimeter. Press the button: the LED should light up immediately. Release the button: the LED should turn off. If the LED does not light up, check the LED polarity and ensure the selected pin is powered (check if the 3V3 output is active via M5PM1 configuration).
Going Further
Explore the use of external interrupts (IRQ) to wake the module from deep sleep (6.72uA consumption) when a button is pressed, rather than polling the CPU with a loop.