🛠 Advanced I/O Control and Power Management on M5Stamp Timer Power 2
Objectives
Learn to configure the 5 programmable IOs (GPIO, ADC, PWM) of the M5Stamp Timer Power 2 to interface with sensors or displays. Understand the interaction between power outputs (3V3_DCDC_OUT, 3V3_LDO_OUT) and the PY32L020 microcontroller logic. Optimize power consumption by leveraging deep sleep modes (6.72uA off, 12.27uA standby) with interrupt-based wake-up.
Materials Needed
- M5Stamp Timer Power 2 Module (SKU S021)
- USB-C programming cable
- Analog sensor (e.g., thermistor) or digital (e.g., push button)
- RGB LED or small OLED screen
- DC 5V power supply or 3.7V LiPo battery
- Arduino IDE or M5Burner software
Steps
- Environment Setup: Install PY32L020 support in Arduino IDE. Select the « M5Stamp Timer Power 2 » board and the correct serial port.
- Power Output Initialization: Before using IOs, enable voltage rails if needed via internal registers or dedicated pins. Note that
3V3_DCDC_OUTprovides 0.6A while3V3_LDO_OUToffers 0.4A. Ensure total load does not exceed these limits. - GPIO Configuration: The 5 IOs are multiplexed. For a button, configure a pin as
INPUT_PULLUP. For an LED, useOUTPUT. For an analog sensor, select the appropriate ADC channel.
// Example GPIO configuration
pinMode(IO_PIN_BTN, INPUT_PULLUP);
pinMode(IO_PIN_LED, OUTPUT);
analogReadResolution(12); // If supported
- PWM Implementation: Use a PWM-compatible pin to control LED brightness or motor speed. Check the default timer frequency of the PY32L020.
analogWrite(IO_PIN_PWM, 128); // 50% duty cycle
- Interrupt and Sleep Management: Configure a pin for falling-edge interrupt mode. Before entering sleep, save IO states and disable unnecessary clocks.
attachInterrupt(digitalPinToInterrupt(IO_PIN_BTN), wakeCallback, FALLING);
// Code to enter Deep Sleep mode
py32l020_enter_sleep();
- Data Reading: Read ADC values or GPIO states after wake-up. Wake-up can be scheduled (timer) or external (IRQ). Standby consumption is then 12.27uA at 4.2V.
Verification
- Measure voltage on
EXT_5V_INOUTto confirm power supply. - Verify that the LED turns on/off according to PWM commands.
- Use a multimeter to measure current in off mode (expected ~6.72uA).
- Press the button to verify module wake-up.
To Go Further
Explore I2C communication (address 0x6E) to read M5PM1 chip parameters. Adjust charging current (650mA or 180mA) via dedicated pins to optimize battery life. Test stability of PY_3V3 outputs under variable load to avoid voltage drops.