py32l020-i2c-rgb-led-pwm-5.ino

M5Stamp Timer Power 2 (PY32L020) · File : py32l020-i2c-rgb-led-pwm-5.ino · Language : C · Use case : Contrôler les LED RVB adressables internes ou externes via PWM pour afficher un état système avec faible consommation.

Drives an RGB LED via three PWM pins to create dynamic visual indicators (breathing, blinking).

The M5Stamp Timer Power 2 has 5 programmable IOs. Identify which ones support PWM (TIMER). The code uses analogWrite. For minimal consumption, turn off the LEDs (0) during deep sleep phases.

py32l020-i2c-rgb-led-pwm-5.ino

/*
 * M5Stamp Timer Power 2 (PY32L020) - RGB LED Control via PWM
 * Platform: Arduino (PY32L020 Core)
 * Note: On some M5Stamp variants, RGB LED is driven via dedicated pins or specific IO.
 * This example assumes standard RGB LEDs on GPIOs supporting PWM (e.g., PA0, PA1, PA2)
 */

#include <Arduino.h>

// Define RGB LED Pins (Check M5Stamp Timer Power 2 Pinout: IOs 1-5 are programmable)
// Example mapping to IO1, IO2, IO3 which support PWM
#define LED_R_PIN PA0
#define LED_G_PIN PA1
#define LED_B_PIN PA2

void setRGB(int r, int g, int b) {
  // Invert if Active Low (common on some modules)
  // analogWrite expects 0-255
  analogWrite(LED_R_PIN, 255 - r);
  analogWrite(LED_G_PIN, 255 - g);
  analogWrite(LED_B_PIN, 255 - b);
}

void setup() {
  pinMode(LED_R_PIN, OUTPUT);
  pinMode(LED_G_PIN, OUTPUT);
  pinMode(LED_B_PIN, OUTPUT);
  
  // Initialize Timer/PWM hardware if required by core (usually automatic with analogWrite)
  setRGB(0, 0, 0);
  delay(1000);
}

void loop() {
  // Breathing effect Red (Low Power friendly if duty cycle is low)
  for (int i = 0; i <= 255; i += 5) {
    setRGB(i, 0, 0);
    delay(10);
  }
  for (int i = 255; i >= 0; i -= 5) {
    setRGB(i, 0, 0);
    delay(10);
  }
  
  // Blink Green when System is 'Active'
  setRGB(0, 0, 0);
  delay(2000);
}

← Back to the board