M5Unified is a library to handle ESP32-based M5Stack products with ArduinoFramework.
Until now, M5Stack/M5Core2/M5StickC/M5StickCPlus/M5Atom had separate libraries for each product, and their usage (APIs) were also different. With M5Unified, all M5Stack series can be handled with the same usage (API), which is very convenient.
Also, once you get used to M5Unified, you can run different M5Stack series (e.g., M5Stack with screen and M5Atom without screen) with a single program code.
M5Unified can be used not only with Arduino framework but also with ESP-IDF framework.
Devices other than those listed below may also be supported. Please check M5Unified repository for the latest information.
M5Stack Basic / Gray / Go / Fire
M5Stack Core2 / Core2 for AWSIoT EduKit / M5Tough
M5StickC / M5StickCPlus
M5Stack CoreInk
M5Paper
M5Atom Lite / Matrix / Echo / PSRAM / U
M5Stamp Pico / C3 /C3U
M5Station
Unit LCD
Unit OLED
Atom Display / Lite (Connect to M5Atom Lite / Matrix / PSRAM)
SPK HAT (connects to M5StickC / M5StickCPlus / M5Stack CoreInk)
Atomic SPK (connects to M5Atom Lite / PSRAM)
Install ArduinoIDE
Start ArduinoIDE and follow the steps below to install M5Unified.
Install M5Unified library.
3.
Enter the program code for HelloWorld or blinking RGB LEDs in the ArduinoIDE editor.
The display and serial monitor will show "HelloWorld! on the display and serial monitor, and counts the numbers every second.
#include <M5Unified.h> // Make the M5Unified library available to your program.
// global variables (define variables to be used throughout the program)
uint32_t count;
// setup function is executed only once at startup.
// This function mainly describes the initialization process.
void setup() {
auto cfg = M5.config(); // Assign a structure for initializing M5Stack
// If config is to be set, set it here
// Example.
// cfg.external_spk = true;
M5.begin(cfg); // initialize M5 device
M5.Display.setTextSize(3); // change text size
M5.Display.print("Hello World!!!") ; // display Hello World! and one line is displayed on the screen
Serial.println("Hello World!!!") ; // display Hello World! and one line on the serial monitor
count = 0; // initialize count
}
// loop function is executed repeatedly for as long as it is running.
// loop function acquires values from sensors, rewrites the screen, etc.
void loop() {
M5.Display.setCursor(0, 20); // set character drawing coordinates (cursor position)
M5.Display.printf("COUNT: %d\n", count); // display count on screen
Serial.printf("COUNT: %d\n", count); // display count serially
count++; // increase count by 1
delay(1000); // wait 1 second(1,000msec)
}
M5Unifed v0.0.7 does not support RGB LEDs and requires an external library such as FastLED.
Refer to
Install Library
and install the library "FastLED".
The serial monitor will display "Hello World! on the serial monitor and blinks the RGB LED green every second.
#include <M5Unified.h> // enable the M5Unified library.
#include <FastLED.h> // enable FastLED (RGB LED) library.
// Specify the number of RGB LEDs (25 for M5Atom Matrix).
#define NUM_LEDS 1
// Specify DATA PIN of RGB LEDs.
#define LED_DATA_PIN 27
// global variables (define variables to be used throughout the program)
uint32_t count;
CRGB leds[NUM_LEDS];
// setup function is executed only once at startup.
// This function mainly describes the initialization process.
void setup() {
auto cfg = M5.config(); // assign a structure for initializing M5Stack
// If config is to be set, set it here
// Example.
// cfg.external_spk = true;
M5.begin(cfg); // initialize M5 device, Display is also initialized
FastLED.addLeds<WS2811, LED_DATA_PIN, GRB>(leds, NUM_LEDS); // initialize RGB LEDs
FastLED.setBrightness(20); // set the brightness (more than 20 may break due to heat.)
Serial.println("Hello World!!!") ; // display Hello World!!! on serial monitor and one line is displayed on the serial monitor
count = 0; // initialize count
}
// loop function is executed repeatedly for as long as it is running.
// loop function acquires values from sensors, rewrites the screen, etc.
void loop() {
leds[0] = CRGB::Red; // set LED[0] to red
FastLED.show(); // display LEDs
delay(500); // wait 0.5 seconds
leds[0] = CRGB::Black; // set LED to black
FastLED.show(); // show LEDs (lights off because they are black)
delay(500); // wait 0.5 seconds
Serial.printf("COUNT: %d\n", count); // display count value on serial
count++; // increase count by 1.
}
Connect the device to which the program is to be written to the PC.
Check and select the port to which the device is connected. (The image shows the COM? port on Windows.)
? > The name may vary depending on the chip in the device and the OS version.
OS | Port name (CH2104) | Port name (CH9102) | remarks |
---|---|---|---|
Windows | COM? | COM? | ?? is a number (COM1 and COM3 are not available in Windows 11 because they are for system use.) |
MacOS | /dev/tty.SLAB_USBtoUART /dev/cu.usbserial-XXX |
/dev/tty.SLAB_USBtoUART /dev/cu.usbserial-XXX |
|
Linux | /dev/ttyUSB? | /dev/ttyACM? | ? is a number. |
Execute Sketch
-> Write
from the menu to write the program to the device after compiling. If an error occurs, it will be displayed in orange, so check the message to find out.
Creating
I plan to introduce concrete examples of how to get sensor values, control motors, etc.
Many of the information available on the Internet uses product-specific libraries and cannot be used as is. In such cases, please refer to the following document for porting to M5Unified.