Arduino M5Stack Tutorial

In this tutorial, you will learn how to setup your computer for Arduino/M5Stack development. This tutorial assumes you are using a Windows computer. If you are using Mac or Linux, the steps are generally the same, but be aware that there might be differences that you will need to figure out yourself.

Hardware Requirements

This tutorial requires an M5Stack and a USB-C cable. Make sure you have them before starting this tutorial. As mentioned in the project briefing, you will be lent 2 sets of M5Stack and USB-C cables from the course coordinator. They are available for collection from AM418 every Tuesday and Friday after the lecture (from 10:00 to 11:00).

Step 1: M5Stack Driver Installation

You will first need to install the appropriate driver. Before doing this step, ensure that your M5Stack is NOT connected to your computer!

Download the appropriate driver from the following table:
Operating System Chip Link
Windows CP2104 https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/drivers/CP210x_VCP_Windows.zip
Windows CH9102 https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/drivers/CH9102_VCP_SER_Windows.exe
MacOS CP2104 https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/drivers/CP210x_VCP_MacOS.zip
MacOS CH9102 https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/drivers/CH9102_VCP_MacOS_v1.7.zip
Linux
(driver not needed)
CP2104 https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/drivers/CP210x_VCP_Linux.zip
Most of the M5Stack that we have uses the CP2104 chipset, so try to install that version first. Note that the ZIP files may contain several installation program files (e.g. x86 for 32-bit/16-bit computers (who uses these computers now?) and x64 for 64-bit computers). Use the program file that corresponds to your computer.

Step 2: Install Arduino IDE

If you already have Arduino IDE, try to upgrade it to the latest version if possible.

If you do not have Arduino IDE, you can download it from https://www.arduino.cc/en/software .

Step 3: Run Arduino IDE

Open Arduino IDE. You should see a window that looks like this:

arduino1.png

An Arduino code is called a sketch and consists of two functions that you need to implement. The setup() function is used to initialize your program as it is called once after M5Stack boots up. The loop() function should contain the main loop of your program. Arduino uses C++, so you can write C/C++ code within the functions. You are also free to create additional functions to make your code more organized. Visit https://www.arduino.cc/reference/en/ for more information about Arduino programming.

Step 4: Add M5Stack to Arduino IDE Board Manager

You will need to add M5Stack to the Arduino IDE Board Manager. To do this, select File -> Preferences.

arduino2.png

The Preferences window should appear. Click the Settings tab.

arduino3.png

Click on the Additional Boards Manager URLs to add the M5Stack Board Manager. Copy the link below
https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/arduino/package_m5stack_index.json

and paste it into the textbox.

arduino4.png

Click OK on both the Additional Boards Manager URLs and Preferences windows.

Select Tools -> Board: "<board name>" -> Board Manager... (If you are running Arduino IDE for the first time, the board name is most likely Arduino Uno, otherwise it could be something else that you have picked before.)

arduino5.png

You should see the Board Manager window which looks like this:

arduino6.png

Search for M5Stack by typing M5Stack in the textbox. This should show a result, as shown in the figure below:

arduino7.png

Click Install to install M5Stack package. Note that this will install around 140 Megabytes, so this could take awhile. This would be a good opportunity to have a break...

Click Close after the completion of the installation.

Step 5: Install M5Stack Libraries

You will need to install the necessary M5Stack libraries to compile your code. To do this, select Tools -> Manage Libraries... The following window should appear:

arduino5b.png

Search for M5Stack. This will hit several results. Scroll down and look for M5Stack, as shown in the figure below, then click Install. When prompted, click Install M5Stack Only.

arduino5c.png

Step 6: Connect M5Stack to Your Computer

You can now connect the M5Stack to your computer using the USB-C cable. To test if the connection is successful, on the Arduino IDE, click Tools -> Port -> COMx (where x is some integer which changes everytime you plug the M5Stack). See image below for an example. Note that you will need to do this step everytime you connect/plug the M5Stack to your computer.

arduino8.png

Change the board to M5Stack-Core-ESP32. You can do this by selecting Tools -> Board: "<board name>" -> M5Stack Arduino -> M5Stack-Core-ESP32, as shown in the figure below:

arduino9.png

Finally, change the Partition Scheme from Default to No OTA. You can do this by selecting Tools -> Partition Scheme: "Default" -> No OTA (Large App), as shown in the figure below:

arduino10.png

Important: Everytime you unplug and plug the M5Stack to your computer, you will have to change to the correct Port, Board, and Partition Scheme.

Step 7: Hello World Test

As customary in any programming course, you will test your setup using the Hello world program. Copy the following to Arduino IDE (overwrite the existing sketch code):
#include <M5Stack.h>

void setup(){
    M5.begin(); // Initialize M5Core
    M5.Power.begin(); // Initialize the power module
    M5.Lcd.print("Hello world"); // Do our business
}

void loop() {
}

Save and click Upload (see figure below for the location of the Upload button):

arduino11.png

This should compile and flash the code to your M5Stack. On success, you should see the Hello world message displayed on the M5Stack screen, as shown below:

m5stackhw.jpg

Example Codes For M5Stack

There is a lot of example codes for M5Stack that was downloaded to your Arduino IDE. You can find them here:
  • File -> Examples -> ESP32 BLE Arduino
  • File -> Examples -> M5Stack
Take some time to look at the example codes to familiarize yourself with M5Stack programming.

As mentioned in the lecture, you can use the BLE server code (from ESP32 BLE Arduino) as a possible starting point for your Sensor node code.