I created a simple program (C++) in Arduino IDE and tried to run it on M5StickC. | そう備忘録

I created a simple program (C++) in Arduino IDE and tried to run it on M5StickC.

Introduction

I got an M5StickC, so I’ll leave this article as a reminder of how I made a simple program to make it work.

Appearance of the M5StickC

What is M5StickC?

M5StickC is a stick-type product from M5Stack Company, which is based on ESP32-PICO, the same architecture as ESP32.

The features

  • 4MB flash + 520K RAM memory
  • TFT LCD (TFT liquid crystal display)
  • Wi-Fi, Bluetooth
  • Built-in lithium-ion polymer battery (80mAh)
  • Power button + 2 buttons
  • USB Type-C connector
  • HY2.0-4P (Grove compatible connector)
  • LED x 1
  • Infrared transmission
  • Expansion connector
  • 3-axis accelerometer + 3-axis gyroscope (6 axes)
  • Built-in Microphone

Let me explain the features of the M5StickC in more detail.

TFT LCD (Thin Film Transistor Liquid Crystal Display)

The display is a 0.96″ (80 x 160) TFT LCD (Thin Film Transistor Liquid Crystal Display).

The module used was marked as ST7735S.

Wi-Fi, Bluetooth

Since it is based on ESP32, it has built-in Wi-Fi and Bluetooth.

The following photo shows a sample application of BLE (Bluetooth Low Energy) pre-installed at the factory.

BLE Sample Application

Lithium-ion polymer battery

The built-in battery was a lithium-ion polymer battery.

It is also used in drones, and while it is small and powerful, I have heard that it should be used at high temperatures and handled with care when charging, so it should not be handled roughly.

Buttons

There are three buttons: power button, button A, and button B.

Power button and button A

Button A is on the front, and the power button is on the side.

  • Power on: press the power button for 2 seconds
  • Power off: Press the power button for 6 seconds

The power off is designed to be pressed slightly longer than the power on.

Button A,Power

button B

Button B is on the other side of the power button.

Button B

Connectors

The top connector in the picture below is the USB Type-C connector used for power supply and downloading programs created on the PC.

The 4-pin connector below is the GROVE compatible HY2.0-4P.

Connectors

The HY2.0-4P has two signal lines in addition to the power and GND lines.

From left to right in the following picture

  • GND
  • Vout (5.0V)
  • GPIO32 (SDA)
  • GPIO33 (SCL)

You can see them on the sticker on the back.

I haven’t tested it yet, but there was an article about measuring temperature, humidity, and barometric pressure with the BME280 using I2C communication between SDA and SCL.

I’d like to try it eventually.

HY2.0-4P

Extension Connectors

On the other side of the USB connector is a 2.54mm pitch general-purpose pin connector.

From left to right in the photo.

  • 5.0V input
  • 3.3V output *
  • Built-in battery *
  • GPIO0
  • GPIO0 GPIO36
  • GPIO26
  • 5.0V output *
  • GND

* The 3.3V and 5.0V outputs only apply voltage when the power is on.

The built-in battery outputs a voltage of 3.928V (when fully charged) even when the power is off.

Extension Connectors

LED

The red LED is on the upper left of the unit.

The aforementioned photo shows the LED in the lit state.

Infrared transmission

I have not tested it yet, but it seems to be able to send infrared rays.

The following picture shows a sample application for infrared transmission, which is preinstalled at the factory.

Infrared transmission

Accelerometer and gyroscope

It has a 3-axis (X, Y, Z) accelerometer and a 3-axis gyroscope (chip: MPU6886) that can measure the state of the M5StickC when it is tilted, shaken, or rotated.

The accelerometer measures the change in speed per unit of time, so it measures the state when the speed is getting faster (slower).

Acceleration is zero in the case of stationary or constant velocity motion.

The gyro sensor is a sensor that detects changes in angle (angular velocity).

If the object rotates on the spot, the acceleration will be zero with the acceleration sensor because the position is not moving, but it can be detected with the gyro sensor.

The combination of accelerometer and gyroscope can detect the movement of most objects.

The following picture shows a sample application using the accelerometer and gyroscope preinstalled at the factory.

Programs using accelerometers and gyroscopes

Microphone

The microphone is built into the bottom of the M5 button and the chip used is SPM1423.

The picture below shows a sample application using the microphone pre-installed at the factory.

Microphone application example

M5Stack Series

There are a lot of products with similar names, and I didn’t know the difference between them.

If you already know the difference between these products, please skip to the next section, “Program Environment“.

M5Stack is headquartered in Shenzhen, China, and manufactures ESP32-based M5Stack and M5Stick series products.

M5Stack

The M5Stack series mainly consists of the following products.

  1. M5Stack Basic
  2. M5Stack Gray (with 9-axis IMU)
  3. M5Stack Faces (with 9-axis IMU and various covers)
  4. M5Stack FIRE
  5. M5GO IoT Starter Kit (with various IoT sensors)
  6. M5Stack Core2

1. M5Stack Basic

Basic M5Stack.

M5Stack Basic

On-board chip

ESP32 240MHz dual core

Memory

520KB SRAM

Flash Memory

4M FLASH

Display

320 x 240 color TFT LCD(2.0 inch)

Network

Wi-Fi、dual mode Bluetooth

Input volt.

 5.0V – 5.5V / 500mA(USB Type-C)

Interface

  • USB Type-C x 1
  • SPI x 1
  • GROVE I2C(HY2.0-4) x 1
  • UART x 2
  • I2S(SCLK,WS,MCLK,IN,OUT) x 1
  • microSD slot x 1

Built-in speaker

1W

Built-in battery

3.7V / 150mAh

2. M5Stack Gray

The M5Stack Basic model is equipped with a 6-axis sensor (MPU6886) that detects 3 acceleration axes and 3 gyroscopic axes, and a sensor (BMM150) that can measure 3-axis geomagnetism.

M5Stack Gray

On-board chip

ESP32 240MHz dual core

Memory

520KB SRAM

Flash Memory

16M FLASH

Display

320 x 240 color TFT LCD (2.0 inch)

Network

Wi-Fi, dual mode Bluetooth

Input

 5.0V – 5.5V / 500mA (USB Type-C)

Interface

  • USB Type-C x 1
  • SPI x 1
  • GROVE I2C(HY2.0-4) x 1
  • UART x 2
  • I2S(SCLK, WS, MCLK, IN, OUT) x 1
  • microSD slot x 1

Inertial measurement sensor

  • MPU6886: 6 axes(3 axes of acceleration and 3 axes of gyro (angular velocity))
  • BMM150:Geomagnetic 3-axis

Early model was MPU9250, but changed to MPU6886 + BMM150 combination around 2019.

Built-in speaker

1W

Built-in battery

3.7V / 150mAh

3. M5Stack Faces

M5Stack Gray with 9-axis sensor, keyboard, numeric keypad, and gaming panel.

M5Stack Faces

On-board chip

ESP32 240MHz dual core

Memory

520KB SRAM

Flash Memory

16M FLASH

Display

320 x 240 color TFT LCD (2.0 inch)

Network

Wi-Fi, dual mode Bluetooth

Input

 5.0V – 5.5V / 500mA (USB Type-C)

Interface

  • USB Type-C x 1
  • SPI x 1
  • GROVE I2C(HY2.0-4) x 1
  • UART x 2
  • I2S(SCLK, WS, MCLK, IN, OUT) x 1
  • microSD slot x 1

Inertial measurement sensor

  • MPU6886:Six axes (three acceleration axes and three gyro (angular rate) axes)
  • BMM150:Geomagnetic 3-axis

Early model was MPU9250, but changed to MPU6886 + BMM150 combination around 2019.

input device

  • Keyboard
  • Numeric keypad
  • Game console-style interface panel

Built-in speaker

1W

Built-in battery

3.7V / 150mAh

4. M5Stack FIRE

This is the equivalent of M5Stack Gray with 9-axis sensor, enhanced memory to 4M PSRAM, LED BAR (RGB LED x 10) and Charge Base for charging.

The interface is different from the M5Stack Gray, and there is a hole on the back side of the main body that can be jointed with Lego.

M5Stack Fire

On-board chip

ESP32 240MHz dual core

Memory

520KB SRAM, 4M PSRAM

Flash Memory

16M FLASH

Display

320 x 240 color TFT LCD(2.0 inch)

Network

Wi-Fi, dual mode Bluetooth

Input

 5.0V / 500mA(USB Type-C)

Interface

  • USB Type-C x 1
  • GROVE x 3(I2C, I/O,  ART)
  • POGO PIN x 1(Connecting to Charge Base)
  • microSD Slot X 1

Inertial measurement sensor

  • MPU6886:Six axes(three acceleration axes and three gyro (angular rate) axes)
  • BMM150:Geomagnetic 3-axis

Early model was MPU6050+MAG3110, but it was changed to MPU9250, and the latest model is MPU6886+BMM150 combination.

LED

LED BAR(RGB LED x 10)

Built-in speaker

1W

Built-in battery

3.7V / 550mAh

5. M5GO IoT Starter Kit

This is a set equivalent to M5stack Gray with various sensors such as temperature/humidity measurement and infrared.

Parts for compatibility with LEGO are also included, so you can combine it with LEGO MINDSTORMS EV3.

M5GO IoT Starter Kit

On-board chip

ESP32 240MHz dual core

Memory

520KB SRAM

Flash Memory

16M FLASH

Display

320 x 240 color TFT LCD(2.0 inch)

Network

Wi-Fi, dual mode Bluetooth

Input

 5.0V / 500mA(USB Type-C)

Interface

  • USB Type-C x 1
  • GROVE x 3(I2C, I/O, UART)
  • POGO PIN x 1
  • microSD Slot x 1

Inertial measurement sensor

MPU9250:9-axis sensor(3-axis acceleration, 3-axis gyro (angular velocity), and 3-axis geomagnetic)

LED

LED BAR(RGB LED x 10)

Built-in speaker

1W

Microphone

Microphone

Built-in battery

3.7V / 550mAh

Attached sensor

  • ENV Sensor(Temperature, humidity, air pressure)
  • IR Romote(Infrared rays)
  • RGB LED(LED lights up)
  • Motion Sensor(Motion sensor)
  • ANGLE Sensor(The position of the knob changes the numerical value and the amount of light emitted by the side LEDs.)
  • Hub

6. M5Stack Core2

This is an enhanced version of the original generation Core released in September 2020.

Compared to M5Stack Gray, it has 8M PSRAM, touch screen, no magnetometer (only 6-axis accelerometer and gyroscope), vibration motor vibration, microphone, real-time clock, etc.

CORE2

On-board chip

ESP32 D0WD-V3 240MHz dual core

Memory

520KB SRAM, 8M PSRAM

Flash Memory

16M FLASH

Display

320×240 Color TFT LCD(2.0 inch)

Touch Screen(T6336U)

Network

Wi-Fi, dual mode Bluetooth

Input

 5.0V / 500mA(USB Type-C)

Interface

  • USB Type-C x 1
  • GROVE x 3(I2C, I/O, UART)
  • microSD Slot X 1

Inertial measurement sensor

MPU6886:Six axes(three acceleration axes and three gyro (angular rate) axes)

Other

  • Vibration function by vibration motor
  • Microphone (SPM1423)
  • Real-time clock (RTC BM8563)

LED

Power indicator light (green)

Built-in speaker

1W

I2S Power Amplifier (NS4168)

Built-in battery

3.7V / 390mAh(Lithium battery)

7. M5Stack UnitV2

An AI recognition module for Edge computing with a built-in 1080P camera.
The OS is Linux.

M5Stack UnitV2

On-board chip

SigmaStar SSD202D (Dual Cortex-A7 1.2Ghz Processor)

Memory

128MB DDR3

Flash Memory

512MB NAND Flash

Camera

GC2145 1080P Colored Sensor

Power Input

5V @ 500mA

Peripherals

USB Type-C x 1, UART x 1, TFCard x 1, Button x 1

Network

Wi-F (150Mbps 2.4GHz 802.11 b/g/n)

Microphone

Microphone

M5Stick Series

The M5Stick series are mainly sold in the following products.

  1. M5StickC
  2. M5stickC Plus

1. M5StickC

It is a smaller version of the M5Stack with slightly lower specifications, but it includes an IMU (inertial measurement unit) and is recommended as a first IoT device.

M5StickC

On-board chip

ESP32-PICO 240MHz dual core

Memory

520KB SRAM

Flash Memory

4M FLASH

Display

80×160 Color TFT LCD(0.96 inch)

Network

Wi-Fi, dual mode Bluetooth

Input

5.0V@500mA(USB Type-C)

Interface

  • USB Type-C x 1
  • HY2.0-4P(Grove compatible connector) x 1
  • Extension Connectors

Inertial measurement sensor

MPU6886:Six axes(three acceleration axes and three gyro (angular rate) axes)

Initially it was SH200Q, but switched to MPU6886 around 2019.

Other

Infrared transmission

LED

Red LED x 1

Microphone

Built-in Microphone

Built-in battery

80mAh Lithium-ion polymer battery

2. M5StickC Plus

Compared to the M5stickC, this version has a larger screen, more battery capacity, and an added buzzer.

M5StickC Plus

On-board chip

ESP32-PICO-D4 240MHz dual core

Memory

520KB SRAM

Flash Memory

4M FLASH

Display

135x 240 color TFT LCD(1.14 inch)

Network

Wi-Fi, dual mode Bluetooth

Input

5.0V@500mA(USB Type-C)

Interface

  • USB Type-C x 1
  • HY2.0-4P(Grove compatible connector) x 1
  • Extension Connectors

Inertial measurement sensor

MPU6886:Six axes(three acceleration axes and three gyro (angular rate) axes)

Initially it was SH200Q, but switched to MPU6886 around 2019.

Other

  • Infrared transmission
  • buzzer

LED

Red LED x 1

Microphone

Built-in Microphone

Built-in battery

120mAh@3.7V

programming environment

Back to programming with the M5StickC.

In creating a simple test program, I looked into the programming environment.

Environment

My environment is as follows

OS

Windows10 Home Version 1903

PC

DELL G7 15 7588

CPU

CoreTM i7-8750H

Selecting a development environment and programming language

The most common development environment and language is C/C++ with Arduino IDE.

It seems that MicroPython can be used as well, so I did some research on the net, but there were not that many sample sources in Python related to the M5Stack.

I don’t know much about C, so I would have preferred to use Python, which is a little better, but I feel that this kind of choice is often easier if you have a lot of reference material and sample code, even if you are not good at the language.

I think I can do C/C++ while I’m at it, so I’m going to install the Arduino IDE.

Download the Arduino IDE

Go to this page and click on “Windows Win7 and newer” in the “DOWNLOAD OPTIONS” section on the right.

Download the Arduino IDE

If you want to donate to the download, select the amount and click the “CONTRIBUTE & DOWNLOAD” button to make a PayPal donation.

I clicked on the free “JUST DOWNLOAD” button.

Just Download

Save the “arduino-1.8.13-windows.exe” file to your local disk.

Launch the installer

Run the downloaded arduino-1.8.13-windows.exe and click “I Agree” when the GNU license is displayed.

I Agree

When the installation options are displayed, confirm them and click the “Next” button.

select Install options

Confirm the destination folder and click the “Install” button.

Install

Install additional software

You will be asked to install the Adafruit Industries LLC port device software, click the “Install” button.

*Screen shots are for Windows in a Japanese environment.

Adafruit Device Software install

You will be asked to install the USB driver for Arduino srl.

Arduino USB Driver

Next, you will be asked to install the Ardiuno LLC USB driver, so click the “Install” button.

I don’t know why there are two USB drivers, one from Arduino srl (Italy) and the other from Ardiuno LLC (USA), but it may be a remnant of a previous split (settled in 2016).

Arduino USB Driver Install

When the installation is finished, click the “Close” button.

インストール終了

Preparing the operating environment

After installing the Arduino IDE, install the libraries necessary for programming the M5StickC.

Add a board manager

Start the Arduino IDE and select “File” -> “Preferences”.

Preferences

Enter the following in the “Additional Boards Manager URLs”

https://dl.espressif.com/dl/package_esp32_index.json

Click the “OK” button, and ESP32 will be recognized.

If you want to specify more than one board manager, separate them with “,” (comma).

Additional Boards Manager URLs

Next, select “Tools” -> “Board” -> “Boards Manager”.

Boards Manager

Enter “esp32″ in the search field to narrow down the results, and click the “Install” button.

When the installation is finished, click the “Close” button to close it.

esp32 Installed

Installing the library

Next, install the necessary libraries.

Select “Sketch” -> “Include Library” -> “Manage Libraries” from the menu.

Manage Libraries

Search for “M5StickC” in the search field and click the “Install” button to install the M5StickC library.

M5StickC Library

In the same way, narrow down the search field to “FastLED” and click the “Install” button to install the FastLED library.

This library is used in the sample application at the factory, so you need to install it.

When the installation is finished, click the “Close” button to close the application.

fastLED Library Installed

Checking the COM port

When the M5StickC is connected to the PC via USB, the COM port will be recognized and the number should be checked.

Right-click on the Windows Start button and select Device Manager.

Device Manager

When you connect the M5StickC to your computer via USB, you will see COM8 (may change depending on your environment), so you know it is COM8.

Ports COM8

Selecting a board

Next, select “Tools” -> “Board” -> “ESP32 Arduino” -> “M5Stick-C” from the Arduino IDE menu.

select M5StickC

The Upload Speed will change to 1500000 (if it hasn’t changed, change it manually), then select “Tools” > “Partition Scheme” > “No OTA (Large APP)”.

No OTA

OTA (Over the Air) is a function to upgrade programs over the network.

Since the old and new programs are temporarily loaded into the memory, it requires twice as much free memory as the program size.

There is no problem with the size of the program we are going to test.

When I compile the program to revert to the factory-installed program, the following message appears if OTA is enabled.

The program size (1338029 bytes) is greater than maximum allowed (1310720 bytes)

The above message appears and I get an error.

There is a setting to expand the program area in memory to more than 1.3 MByte, but I decided to disable OTA to deal with it this time.

Selecting a port

Set the COM port (COM8) that you have just checked.

Select “Tools” -> “Port” > “COM8″ from the menu.

select COM Port

This completes the preliminary setup.

test program

Create a new program by selecting “File” > “New” from the menu.

Specifications

The specifications of the program for testing are as follows.

  • Use button A (M5 button) to turn the LED on and off (toggle).
  • When the LED is on, the display will show the word “LED ON”, and when it is off, the display will show the word “LED OFF”.
  • Button B toggles the display between “white text with black background” and “red text with white background”.

LED off(white text)

LED off(white text)

LED on(white text)

LED on(white text)

LED off(red text)

LED off(red text)

LED on(red text)

LED on(red text)

source-code

The source code for the program is as follows.

#include <M5StickC.h>

#define LED_PIN   10

#define LED_ON  LOW // LED on
#define LED_OFF HIGH // LED off

int led_flg = 0; // false
int color_flg = 0; // false

void setup() {
  M5.begin();
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LED_OFF); // LED off
  led_flg = 0;
  M5.Lcd.setRotation(1); // Change the direction of the LCD
  M5.Lcd.setTextSize(2); // Double the font size.
  M5.Lcd.setTextColor(WHITE, BLACK); // White for text, black for background
  color_flg = 0;
  M5.Lcd.setCursor(10,10);
  M5.Lcd.print("LED OFF"); // display LED OFF
}

void loop() {
  M5.update();

  if(M5.BtnA.wasPressed()){ // When button A is pressed
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setCursor(10,10);
    if (led_flg){ // If LED on
      digitalWrite(LED_PIN, LED_OFF); // LED off
      M5.Lcd.print("LED OFF"); // display LED OFF
      led_flg = 0;
    } else { // 消灯なら
      digitalWrite(LED_PIN, LED_ON); // LED on 
      M5.Lcd.print("LED ON"); // display LED ON
      led_flg = 1;
    }
    delay(500);
  }

  if(M5.BtnA.wasReleased()){ // When button A is released
    ;  // none 
  }

  if(M5.BtnB.wasPressed()){ // When button B is pressed
    if (color_flg){ // If text is red and background is white
      M5.Lcd.setTextColor(WHITE, BLACK); // White for text, black for background
      color_flg = 0;
    } else { // White for text, black for background
      M5.Lcd.setTextColor(RED, WHITE); // If text is white and background is black
      color_flg = 1;
    }
    delay(500);
  }

  if(M5.BtnB.wasReleased()){ // When button B is released
     ; // none
  }

}

I think there is a smarter way to do this than using flags to determine the state.

Compiling and Writing

Then, select “Sketch” -> “Upload” from the menu.

Compile and write the executable module to the M5StickC.

Upload

When the writing was finished, the program ran, and I confirmed that it did what I wanted by pressing buttons A and B.

Restore the original program

When you write a new program, the original program will be lost, so if you want to return to the factory default program, follow the steps below.

From the menu, select “File” > “Open” to open the file.

file open

Open “FactoryTest.ino” in the following folder under the folder where you installed the Arduino IDE library (“Documents” by default in Windows 10).

C:¥Users¥Your user name¥Documents¥Arduino¥libraries¥M5StickC¥examples¥Basics¥FactoryTest¥FactoryTest.ino
Open FactoryTest.ino

After opening the file, select “Sketch” -> “Upload” from the menu and write the program to the M5StickC to return it to the factory default state.

This concludes this article.

Finally.

I hope this article will be useful to someone somewhere.

souichirou kikuchi

I'm Japanese. A reminder to remember what I've done. I'm blogging in the hope that it will be helpful to others who want to do similar things. I mainly write blogs about LEGO, AWS (Amazon Web Services), WordPress, Deep Learning and Raspberry Pi. At work, I'm working on installing collaborative robots and IoT in factories. I passed the JDLA (Japan Deep Learning Association) Deep Learning for GENERAL in July 2019. If you have any questions, please leave them in the comments at the bottom of the article.

You may also like...

comment

Name, Email, and Website are optional.
and, your Email address will not be published.