Demo Board M5stack
Demo Board M5stack
Demo Board M5stack
CONTENTS
Contents............................................................ 1
Introduction........................................................ 2
Description .......................................................... 2
M5Core ............................................................... 3
Hardware and Sensor .................................................. 4
Accessories .......................................................... 5
ARDUINO IDE......................................................... 6
Environment Setup............................................... 6
API................................................................ 17
LCD............................................................ 17
Button......................................................... 20
Speaker........................................................ 22
Module............................................................. 24
Relay.......................................................... 24
............................................................... 25
Microphone..................................................... 27
Light Sensor................................................... 29
Joystick....................................................... 31
Step Motor..................................................... 34
DC-Motor....................................................... 39
Servo.......................................................... 42
Keyboard....................................................... 45
Encoder........................................................ 48
DHT12 Temperature and humidity detection....................... 51
BMP280 Air pressure detection.................................. 54
LED MATRIX..................................................... 57
RFID........................................................... 60
DAC............................................................ 63
ADC............................................................ 66
RS-232......................................................... 72
appendix........................................................... 73
Example Github ...................................................... 74
Arduino API ......................................................... 75
Document & Datasheet ................................................ 75
INTRODUCTION
Description
M5Core
Accessories
Software Install
Windows 10 users can click the "Windows Installer, for Windows XP and
up" option, and the page will switch to the donation and download
page. If you need to donate, you can click "CONTRIBUTE & DOWNLOAD",
and only download the installation package file and click "JUST
DOWNLOAD".
After the download is complete, double-click to open the
installation package. According to the installation prompt, choose to
agree to the authorization agreement. For the configuration options
and installation path of the installation, you can follow the default
configuration if there is no special requirement. Click Next
according to the prompts until the installation is complete.
According to the default configuration, after the installation is
complete, an Arduino shortcut icon will be created on the desktop.
Double-click it to start the Arduino IDE.
Basic Introduction
At the top of the program are the tabs and the function menu bar,
which provide a series of operation and configuration options,
including compile, upload, new, open, save, and serial monitor
functions.
Click the Add button on the right side of the "Add Board
Management Address" column, enter the URL address below in the pop-up
window, and click OK to save.
https://dl.espressif.com/dl/package_esp32_index.json
Also, in the "Preferences" setting page, you can set some display
parameters, such as the editor text size, adding line number display,
compiling/burning log prompts, and other auxiliary content.
After saving the settings, select the "Tools" tab, point the mouse to
the "Development Board" option, find the "Development Board Manager"
in its sub-directory, and click Open.
In the pop-up board management window, search for "esp32" and the
content of the "esp32 by Espressif Systems" development board will
appear.
M5Stack Library
At the top of the program are tabs and function menu bars, which
provide a series of operation and configuration options, and in the
middle is the programming area for code editing.
Below the programming area is the log output area. When enabled, it
will show you the current working status of the Arduino IDE, such as
compiling or uploading the program and output the log information
during the compilation and upload process, which is convenient for
program debugging and Troubleshooting.
Connect the M5Stack device to the computer through the Type-C data
cable, select the corresponding port in the "Port" option in the
"Tools" tab, and confirm that the development board and other
configuration information are correct.
Click the "Upload" button on the function menu bar, and the program
will automatically start to compile. After the compilation is passed,
the program will be uploaded automatically, and the log will output
the percentage of the progress of the program upload at this time. At
this time, please keep the computer and the M5Stack device connected
until the program upload is completed. When the status bar prompts
"Done uploading.", it means the program has been uploaded.
Check the M5Stack device at this time, and you will find that the
upper left corner of the LCD screen displays the words "Hello World".
The screen is as expected by our program, and it is successfully
displayed.
API
LCD
Description
In the program, we can not only modify the displayed text content but
also set the display position of the content and the font size. By
setting the text cursor, you can specify the position of a character
in the text content. By setting the font-size value, the size of the
font display can be adjusted.
parameter:
return: none
eg:M5.lcd.setCursor(40, 60);
parameter:
return: none
eg:M5.lcd.setTextSize(3);
Print Text:
int M5.lcd.print(val);
int M5.lcd.print(val,format);
int M5.lcd.println(val);
int M5.lcd.println(val,format);
parameter:
eg:
M5.lcd.print("M");// Print M
M5.lcd.print(78,HEX);// Print 4E
Example
void setup(){
M5.begin();
M5.Lcd.setTextSize(3);
}
void loop() {
M5.Lcd.setCursor(40, 60);
M5.Lcd.print("Hello World");
delay(1000);
M5.Lcd.clear(BLACK);
M5.lcd.setCursor(40, 100);
M5.Lcd.print("Hello M5Stack");
delay(1000);
M5.Lcd.clear(BLACK);
}
Button
Description
Button click:
uint8_t M5.BtnA.wasPressed();
parameter:none
return:none
eg:
if (M5.BtnA.wasPressed()) {
M5.Lcd.printf("Button A was pressed.");
delay(1000);
}
Long press:
uint8_t M5.BtnA.pressedFor(int32_t ms);
parameter:
int32_t ms: Set long press time
return:none
eg:
if (M5.BtnA.pressedFor(2000)) {
M5.Lcd.printf("Button A was pressed for more than 2 second
s.");
delay(1000);
}
Status update:
void M5.update();
return:none
eg:
M5.update();
Speaker
Description
eg:
M5.Speaker.tone(440, 1000);
Example
void setup() {
M5.begin();
M5.Lcd.setTextSize(3);
M5.lcd.setCursor(40, 100);
void loop() {
if(M5.BtnA.wasPressed()) {
M5.Speaker.tone(262, 1000);
}
if(M5.BtnB.wasPressed())
{
M5.Speaker.tone(294, 1000);
}
if(M5.BtnB.wasPressed())
{
M5.Speaker.tone(330, 1000);
}
M5.update();
}
Warnning:
When using M5Core for programming and development, you need to pay
attention to the pin occupancy of the built-in hardware and avoid
using the same pin to drive different hardware, resulting in failure
to work properly. The following pins represent their application in
M5Core, ( For example, GPIO32 is the pin occupied by the LCD screen)
MODULE
Relay
Description
Relay is a relay control module. When the coil inside the relay
is energized, it will generate a magnetic force to absorb the switch
action, and then realize the switch control. Such a small current
control high current electrical circuit scheme can control DC/3A-30V
or AC/3A -220V level line is on and off.
Each relay provides three control circuit pins, which are the
common terminal COM, the normally closed terminal NC, and the
normally open terminal NO. By default, NC and COM are connected, and
NO and COM are disconnected by default. When the relay coil is
energized, The switch relationship between the two and the common
terminal COM will be reversed.
Hardware connection
When using the Relay relay control module, you only need to connect
the control pin of the controller to the relay coil and turn on the
independent power switch of the module to power it. In this way, when
the controller pin outputs a high level, the relay coil will be
energized and generated The magnetic field force absorbs the action
switch and makes the corresponding action.
Example
#include <M5Stack.h>
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(In_0, LOW);
delay(1000);
digitalWrite(In_0, HIGH);
delay(1000);
}
Microphone
Description
Hardware connection
Example
#include <M5Stack.h>
}
uint16_t a_data;
uint16_t d_data;
void loop() {
// put your main code here, to run repeatedly:
a_data = analogRead(Analog);
d_data = digitalRead(Digtal);
Serial.printf("Analog:%0d Digtal:%0d\n", a_data, d_data);
}
Light Sensor
Description
Hardware connection
The Light module supports the reading of both digital and analog
signal values. When making hardware connections, it should be noted
that not all GPIO interfaces of M5Core support AD conversion, so when
using the analog reading function, you need to connect the interface
Connect to pins that support ADC (such as 34/35/36) and use general
GPIO for digital reading.
Example
#include <M5Stack.h>
M5.Lcd.setCursor(100, 0, 4);
M5.Lcd.print("LUMINOSITY");
}
uint16_t a_data;
uint16_t d_data;
void loop() {
// put your main code here, to run repeatedly:
a_data = analogRead(Analog);
d_data = digitalRead(Digtal);
delay(200);
}
Joystick
Description
The joystick is a joystick input module. The module has built-in two
sets of sliding rheostats and a key switch. When the joystick is
operated, its internal resistance changes accordingly. Connect the
X_ADC and Y_ADC interfaces of the module to the M5Core support On the
ADC converted pin, read the digital information of the joystick
offset through the program.
When the BUTTON is pressed down on the joystick, its internal key
switch will act accordingly to change the output signal to a low
level.
Hardware connection
Connect the X_ADC and Y_ADC of the Joystick module to the pins
that support the ADC function on the M5Core (in this example, PIN
35/36 will be used), and connect the BUTTON to PIN 2.
Example
#include <M5Stack.h>
/*
note:Reading the adc value requires writing the pin 25 of th
e adc to 0.
dacWrite(25, 0);
*/
void setup() {
// put your setup code here, to run once:
M5.begin();
pinMode(2, INPUT_PULLUP);
dacWrite(25, 0);
M5.Lcd.setCursor(100, 0, 4);
M5.Lcd.print("JOYSTICK");
}
uint16_t x_data;
uint16_t y_data;
uint8_t button_data;
void loop() {
// put your main code here, to run repeatedly:
x_data = analogRead(35);
y_data = analogRead(36);
button_data = digitalRead(2);
delay(200);
}
Step Motor
Description
Hardware connection
Example-1
#include <M5Stack.h>
Example-2
#include <M5Stack.h>
int count;
void setup() {
M5.begin();
pinMode(pinMotor[0], OUTPUT);
pinMode(pinMotor[1], OUTPUT);
pinMode(pinMotor[2], OUTPUT);
pinMode(pinMotor[3], OUTPUT);
}
void loop() {
for (int i = 0; i < 2048 ; i++) {
driveMotor();
delay(2);
}
}
void driveMotor() {
count++;
int step = count % 4;
digitalWrite(pinMotor[0] , logic[step][0]);
digitalWrite(pinMotor[1] , logic[step][1]);
digitalWrite(pinMotor[2] , logic[step][2]);
digitalWrite(pinMotor[3] , logic[step][3]);
}
Example-3
We can further subdivide the step, only need to modify the array of
power-on logic, and the drive function.
void driveMotor() {
count++;
int step = count % 8;
digitalWrite(pinMotor[0] , logic[step][0]);
digitalWrite(pinMotor[1] , logic[step][1]);
digitalWrite(pinMotor[2] , logic[step][2]);
digitalWrite(pinMotor[3] , logic[step][3]);
}
DC-Motor
Description
Connect the control pins PIN 21, 22 to the M+ and M- ports of the DC
motor module respectively, and turn on the independent power supply.
Example
#include <M5Stack.h>
void setup() {
pinMode(21, OUTPUT);
pinMode(22, OUTPUT);
}
void loop() {
M5.update();
if (M5.BtnA.wasReleased()) {
M5.Lcd.print('A');
digitalWrite(22, LOW);
digitalWrite(21, HIGH);
} else if (M5.BtnB.wasReleased()) {
M5.Lcd.print('B');
digitalWrite(21, LOW);
digitalWrite(22, LOW);
} else if (M5.BtnC.wasReleased()) {
M5.Lcd.print('C');
digitalWrite(21, LOW);
digitalWrite(22, HIGH);
}
}
Servo
Description
Connect the control pin PIN 15 to the SIGNAL interface of the servo
and turn on the independent power supply,
Example
#include <M5Stack.h>
#include "driver/ledc.h"
void setup() {
M5.begin();
M5.Lcd.setCursor(120, 110, 4);
M5.Lcd.println("SERVO");
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(servo_pin, ledChannel);
}
void loop() {
ledcWrite(ledChannel, 6);//0°
delay(1000);
ledcWrite(ledChannel, 18);//90°
delay(1000);
ledcWrite(ledChannel, 30);//180°
delay(1000);
}
Keyboard
Description
Connect the control pins to each pin of the row and column of the
button matrix, so that the program can scan the action of the button.
Example
In this case, the library <Keypad.h> is used, you can search and
install it in the library management of Arduino.
#include<M5Stack.h>
#include <Keypad.h>
/***
* note:You should first install the keyboard library.
* https://github.com/Chris--A/Keypad
***/
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
byte rowPins[ROWS] = {17,16,21,22};
byte colPins[COLS] = {5, 26, 13, 15};
char keys[ROWS][COLS] = {
{'a','b','c','d'},
{'e','f','g','h'},
{'i','j','k','l'},
{'m','n','o','p'}
};
void setup() {
// put your setup code here, to run once:
M5.begin();
M5.Lcd.setCursor(100, 0, 4);
M5.Lcd.println("KEYBOARD");
}
void loop() {
//put your main code here, to run repeatedly:
char key = keypad.getKey();
if(key){
Serial.println(key);
M5.Lcd.fillRect(150, 150, 80, 50, BLACK);
M5.Lcd.setCursor(150, 140, 4);
M5.Lcd.printf("%c",'A');
}
}
Encoder
Description
Hardware connection
Example
#include <M5Stack.h>
void encoderEvent() {
code = GET_CODE();
if(code != code_old) {
if(code == 0x00) {
count_last = count;
if(code_old == 0x10) {
count--;
count_change == -65536 ? count_change : count_change-
-;
} else {
count_change == 65536 ? count_change : count_change++;
}
}
code_old = code;
}
}
void setup() {
// put your setup code here, to run once:
M5.begin();
pinMode(phaseA, INPUT_PULLUP);
pinMode(phaseB, INPUT_PULLUP);
pinMode(Button, INPUT_PULLUP);
dacWrite(25, 0);
M5.Lcd.setCursor(100, 0, 4);
M5.Lcd.print("ENCODER");
code = GET_CODE();
code_old = code;
void loop() {
// put your main code here, to run repeatedly:
uint8_t value = digitalRead(Button);
encoderEvent();
Serial.printf("code = %d,button = %d\r\n ",count_change,valu
e);
}
DHT12 Temperature and humidity
detection
Description
Hardware connection
Example
The following code is only the main program and does not include its
dependent library files such as <DHT12.h>. For the complete code,
please visit the Github address below to get it.
https://github.com/m5stack/DEMO-BOARD/tree/master/SENSOR/TEMP_HUM
Read the temperature and humidity values measured by the DHT12 sensor
and display them on the screen.
#include <M5Stack.h>
#include "DHT12.h"
#include <Wire.h> //The DHT12 uses I2C comunication.
void setup() {
M5.begin();
Wire.begin();
M5.Lcd.setCursor(80, 0, 4);
M5.Lcd.print("TEMPERATURE");
}
void loop() {
delay(100);
}
BMP280 Air pressure detection
Description
Hardware connection
Example
Read the atmospheric pressure value measured by the BMP280 sensor and
display it on the screen.
#include <M5Stack.h>
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include <Adafruit_BMP280.h>
/*
note: need add library Adafruit_BMP280 from library manage
*/
Adafruit_BMP280 bme;
void setup() {
M5.begin();
Wire.begin();
M5.Lcd.setCursor(70, 0, 4);
M5.Lcd.print("AIR_PRESSURE");
if (!bme.begin(0x76)){
Serial.println("Could not find a valid BMP280 sensor, ch
eck wiring!");
while (1);
}
void loop() {
float pressure = bme.readPressure();
M5.Lcd.setCursor(50, 100, 4);
M5.Lcd.printf("Pressure:%2.0fPa\r\n",pressure);
delay(100);
}
LED MATRIX
Description
Connect the control pin to the SIGNAL interface of the matrix and
turn on the independent power switch.
Example
The library <FastLED.h> is used in this case, you can search and
install it in the library management of Arduino.
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LED
S).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
for(int i = 0; i < 64; i++){
leds[i] = CRGB::White;
FastLED.show();
}
delay(500);
// Now turn the LED off, then pause
for(int i = 0; i < 64; i++){
leds[i] = CRGB::Black;
FastLED.show();
}
delay(500);
}
RFID
Description
Hardware connection
Example
The following code is only the main program and does not include its
dependent library files such as <MFRC522_I2C.h>. For the complete
code, please visit the Github address below to obtain it.,
https://github.com/m5stack/DEMO-BOARD/tree/master/RFID
#include <Wire.h>
#include "MFRC522_I2C.h"
#include <M5Stack.h>
// 0x28 is i2c address on SDA. Check your address with i2cscan
ner if not match.
MFRC522 mfrc522(0x28); // Create MFRC522 instance.
void setup() {
M5.begin();
Wire.begin();
M5.Lcd.setCursor(140, 0, 4);
M5.Lcd.println("RFID");
mfrc522.PCD_Init(); // Init MFRC522
ShowReaderDetails(); // Show details of PCD - MFR
C522 Card Reader details
Serial.println(F("Scan PICC to see UID, type, and data block
s..."));
M5.Lcd.setCursor(0,30,2);
M5.Lcd.println("Scan PICC to see UID, type, and data blocks.
..");
}
void loop() {
// Look for new cards, and select one if present
if (!mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadC
ardSerial()) {
delay(50);
return;
}
// Dump UID
Serial.print(F("Card UID:"));
M5.Lcd.println(" ");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
M5.Lcd.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
}
void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably fai
led
if((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the M
FRC522 properly connected?"));
}
}
DAC
Description
Sensor devices that use the I2C protocol for communication can be
connected to M5Core's default I2C protocol pins PIN21 (SDA), PIN22
(SCL).
Example
Wire.beginTransmission(DAC_ADDR);
Wire.write(0x10|(ch<<1));
void setup() {
// put your setup code here, to run once:
M5.begin();
Wire.begin(21, 22);
dacWrite(25, 0);
M5.Lcd.setCursor(140, 0, 4);
M5.Lcd.print("DAC");
outVoltage(0,256); //1.25v
outVoltage(1,512); //2.50v
outVoltage(2,768); //3.75v
outVoltage(3,1023); //5.00v
void loop() {
delay(200);
}
ADC
Description
Hardware connection
Sensor devices that use the I2C protocol for communication can be
connected to M5Core's default I2C protocol pins PIN21 (SDA), PIN22
(SCL)
Example
Read the analog signals input by the 4 ADC conversion interfaces, and
convert them into digital signals and display them on the screen.
#include <M5Stack.h>
#define ADC_ADDR 0x48
uint16_t InVoltage(uint8_t ch){
uint8_t data_L = 0;
uint8_t data_H = 0;
uint16_t data_adc = 0;
Wire.beginTransmission(ADC_ADDR);
Wire.write(0X01);
Wire.write(0XC0 | (ch << 4));
Wire.write(0X83);
Wire.endTransmission();
Wire.beginTransmission(ADC_ADDR);
Wire.write(0x00);
Wire.endTransmission();
delay(50);
Wire.requestFrom(ADC_ADDR, 2);
while(Wire.available()){
data_H = Wire.read();
data_L = Wire.read();
}
Hardware connection
void setup() {
M5.begin();
M5.Power.begin();
Serial.begin(115200);
void loop() {
if(Serial.available()) {
int ch = Serial.read();
Serial2.write(ch);
}
if(Serial2.available()) {
int ch = Serial2.read();
Serial.write(ch);
}
}
RS-232
Description
The RS-232 conversion module can convert TTL level signals to RS232
level signals, realize protocol conversion, and then control
corresponding types of equipment.
Hardware connection
#include <M5Stack.h>
void setup() {
M5.begin();
M5.Power.begin();
Serial.begin(115200);
void loop() {
if(Serial.available()) {
int ch = Serial.read();
Serial2.write(ch);
}
if(Serial2.available()) {
int ch = Serial2.read();
Serial.write(ch);
}
}
APPENDIX
Example Github
https://github.com/m5stack/DEMO-BOARD
Arduino API
https://docs.m5stack.com/#/en/arduino/arduino_home_page
https://docs.m5stack.com/#/en/app/demo-board