NederlandsKlik deze knop voor de Nederlandstalige website

Using the M5Stack RGB Unit with the M5StickC

The M5StickC in itself is a very complete ESP32 development platform, but more LEDs are of course always better! Fortunately, there are numerous add-ons available that you can connect to the Grove port. One such extension is the “M5Stack RGB Unit (SK6812)”: a Grove module with 3 RGB LEDs. If you want more than 3 LEDs, you can link several of these modules together.

M5StickC and the Arduino IDE

In this example, we will program the M5StickC with the Arduino library. You can read how to install it and make it suitable for programming the M5StickC in the previous article “Programming the M5StickC with the Arduino IDE“.

M5StickC ESP32 PICO Color LCD Mini IoT Development Board
Tested and recommended by OneGuyOneBlog.com:

M5StickC ESP32 PICO Color LCD Mini IoT Development Board
Banggood.com

Adafruit Neopixel Library

In addition to the M5StickC libraries, you also need the Neopixel Library from Adafruit. You can download it from GitHub or install it directly in the Arduino IDE with the Library Manager.

M5Stack RGB Unit (SK6812) Sample Sketch

The example below is a simple “Hello World”-like sketch. This sketch is derived from the example sketch for the M5Stack module.

The main points of attention:

  • Make sure the correct libraries are loaded:
    #include <Adafruit_NeoPixel.h>
    #include <M5StickC.h>
  • Select the correct pin (number 32) for the LEDs:
    #define PIN 32
  • Choose the number of LEDs (pixels). Each RGB Unit module has 3 LEDs. So for 1 module you choose “NUMPIXELS 3”, for 2 modules you choose “NUMPIXELS 6”, etc.
    #define NUMPIXELS 3

The rest of the sketch is self-explanatory. For more information on using the Neopixel library, see Adafruit’s GitHub.

/*
    RGB Unit on M5StickC basic example
    For details see https://oneguyoneblog.com
    Please install library before compiling:
    AdaFruit NeoPixel library: https://github.com/adafruit/Adafruit_NeoPixel
*/
#include <Adafruit_NeoPixel.h>
#include <M5StickC.h>

// Grove pin on the M5StickC
#define PIN            32
// Number of LEDs (a.k.a. "Neopixels")
#define NUMPIXELS      3

// Setup the NeoPixel library
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 1500; // delay for half a second

void setup() {
  M5.begin();
  M5.Lcd.print("LED test");

  pixels.begin(); // This initializes the NeoPixel library.
  pixels.show(); // All pixels off
}

void loop() {
  pixels.setPixelColor(0, pixels.Color(128, 0, 0)); // Moderately bright red color.
  pixels.setPixelColor(1, pixels.Color(0, 128, 0)); // Moderately bright green color.
  pixels.setPixelColor(2, pixels.Color(0, 0, 128)); // Moderately bright blue color.
  pixels.show(); // This sends the updated pixel color to the hardware.
  delay(delayval); // Delay for a period of time (in milliseconds)

  pixels.setPixelColor(0, pixels.Color(128, 128, 128));
  pixels.setPixelColor(1, pixels.Color(128, 128, 128));
  pixels.setPixelColor(2, pixels.Color(128, 128, 128));
  pixels.show();
  delay(delayval);
}

M5Stack RGB Unit in projects

At a later time, this LED module will be used together with, for example, the ESP32 Tally lights, the Grove Beginner Kit for Arduino from Seeed Studio or the project Grove for Arduino: Thermometer and Hygrometer. Do you have any nice ideas or already made projects with the M5Stack RGB Unit? Let us know in the comment section at the bottom of this page!

M5StickC ESP32 PICO Color LCD Mini IoT Development Board
Tested and recommended by OneGuyOneBlog.com:

M5StickC ESP32 PICO Color LCD Mini IoT Development Board
Banggood.com