Arduino IDE environment - M5Dial

Arduino-IDE

Click here to visit Arduino official website , select the installation package corresponding to your own operating system to download.

M5Stack's board management

1.Open the Arduino IDE, select File->Preferences->Settings

2.Copy the M5Stack board management URL below to the Additional Development Board Manager:

https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/arduino/package_m5stack_index.json

3.Select Tools->Development Board:->Development Board Manager...

4.In the new pop-up dialog box, enter and search for M5Stack, click Install (If the search fails, you can try to restart the Arduino program)

Note: Compiling M5Dial requires at least version 2.0.6 and above

5.Select Tools->Development Board:->M5Stack Arduino, the main control used by M5Dial is M5StampS3, so select (STAMP-S3) in this option.

Different hardware devices have different sample program libraries. Please choose to download according to the device you are using. Open the Arduino IDE, and then select Project->Load Library->Library Management...

M5Dial library

M5Dial depends on the M5Unified and M5GFX libraries. When using the Arduino library to manage downloads, please follow the prompts to download the dependent libraries, or manually pull them locally through the Github link below.

基础使用

Display

The M5Dial library calls the M5Unified API on some built-in peripheral drivers, so the operation and use are similar.

#include "M5Dial.h"

void setup() {

   //Initialize most of the built-in peripherals through M5Dial.begin().
    M5Dial.begin();
    //Set font color
    M5Dial.Display.setTextColor(GREEN);
    //Set font alignment
    M5Dial.Display.setTextDatum(middle_center);
    //Set font
    M5Dial.Display.setTextFont(&fonts::Orbitron_Light_32);
    //Set font size
    M5Dial.Display.setTextSize(1);
    //Print text
    M5Dial.Display.drawString("Hello World", M5Dial.Display.width() / 2,
                              M5Dial.Display.height() / 2);
}

void loop() {

}

Button

The Button class calls the interface in M5Unified and can obtain the status of button press, release, long press, etc. For more APIs, please refer to Button_Class . Note: M5Dial.update(); needs to be added to the main loop to refresh the key status.

#include "M5Dial.h"

void setup() {
    auto cfg = M5.config();
    M5Dial.begin(cfg, true, false);
}

void loop() {
    M5Dial.update();
    if (M5Dial.BtnA.wasPressed()) {

    }
    if (M5Dial.BtnA.wasReleased()) {

    }
}

Encoder

#include "M5Dial.h"

void setup() {
    auto cfg = M5.config();
    //Set the enableEncoder flag to true to enable the encoder function.
    M5Dial.begin(cfg, true, false);
}

//Set a variable to store the last encoding value
long oldPosition = -999;

void loop() {
    //Read coded value
    long newPosition = M5Dial.Encoder.read();
    if (newPosition != oldPosition) {
        oldPosition = newPosition;
        Serial.println(newPosition);
    }
}

RFID

#include "M5Dial.h"

void setup() {
    auto cfg = M5.config();
    //Set the enableRFID flag to true to enable the RFID function。
    M5Dial.begin(cfg, false, true);
}

void loop() {
    if (M5Dial.Rfid.PICC_IsNewCardPresent() ||
        M5Dial.Rfid.PICC_ReadCardSerial()) {
        String uid = "";
        // Print card UID
        for (byte i = 0; i < M5Dial.Rfid.uid.size;
             i++) {  
            Serial.printf("%02X ", M5Dial.Rfid.uid.uidByte[i]);
            uid += String(M5Dial.Rfid.uid.uidByte[i], HEX);
        }
        Serial.println();
    }
}

Touch

The current touch coordinates and touch event type can be obtained through getDetail. Note: M5Dial.update(); needs to be added to the main loop to refresh the touch state.


#include <M5Dial.h>

void setup(void) {
    auto cfg = M5.config();
    M5Dial.begin(cfg, false, false);
}

int prev_x = -1;
int prev_y = -1;

static m5::touch_state_t prev_state;

void loop(void) {
    M5Dial.update();
    auto t = M5Dial.Touch.getDetail();
    if (prev_state != t.state) {
        prev_state                                  = t.state;
        static constexpr const char* state_name[16] = {
            "none", "touch", "touch_end", "touch_begin",
            "___",  "hold",  "hold_end",  "hold_begin",
            "___",  "flick", "flick_end", "flick_begin",
            "___",  "drag",  "drag_end",  "drag_begin"};
        Serial.println(state_name[t.state]);
    }
    if (prev_x != t.x || prev_y != t.y) {
        Serial.println("X:" + String(t.x) + " / " + "Y:" + String(t.y));
        prev_x = t.x;
        prev_y = t.y;
    }
}

Power

Low-power power supply design

M5Dial adopts a low-power power supply design, which can reach uA level power consumption in sleep state when connected to a battery.

Usage and precautions:

  • Wake-up method:
    • Manual triggering by pressing the button
    • RTC clock IRQ signal
    • After triggering the wake-up signal, the Hold pin (G46) needs to be set to high level during program initialization to maintain power supply, otherwise the device will re-enter the sleep state. (This step is implemented in M5Dial.begin();, M5.begin();, so you only need to execute this API at the beginning of the program for initialization)
    • When powered by battery, set the Hold pin (G46) to low level to achieve shutdown. You can call M5Dial.Power.powerOff(); to shut down the system.
  • USB power supply:
    • When powered by USB, the device will always maintain power and remain powered on.
#include <M5Dial.h>

void setup(void) {
    M5Dial.begin();
}

void loop(void) {
    M5Dial.update();
    if (M5Dial.BtnA.wasPressed()) {
        //Under battery power supply, after executing the timerSleep program, the device will shut down immediately, and RTC will generate an IRQ signal after the specified time to turn on the device.
        M5Dial.Power.timerSleep(5);
        // M5Dial.Power.timerSleep(const rtc_time_t& time);
        // M5Dial.Power.timerSleep(const rtc_date_t& date, const rtc_time_t&
        // time);
        // M5Dial.Power.powerOff(); shutdown
    }
}
On This Page