Build it step by step
0 of 7 doneInstall the board package
M5Stack in Boards Manager.
Install the libraries
M5Unified and PubSubClient.
Draw the screen
Test with the display only.
Add the touch button
Check it responds.
Fill in Wi-Fi and MQTT
Your own values.
Watch the topic
With an MQTT client.
Add a Grove sensor
If you like.
How do you build an M5Stack Core2 IoT panel?
A ready-made touch device shows sensor values and switches things on, with no wiring. You need M5Stack Core2 and Grove cable 4-pin. It takes 7 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 2
Parts list
- Development BoardsBuyM5Stack Core2 × 1
- Connectors
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Core2 USB-C | Computer or charger | Programming and power |
| Grove cable | Core2 Port A (I2C) to an optional Grove sensor | Add a sensor later |
| Core2 | Built-in screen, touch, accelerometer and battery | Nothing to wire |
Code
The Core2 has the screen, touch panel, accelerometer, speaker, battery and Wi-Fi in one case, so this is a quick way to learn how a touch interface and MQTT fit together without any wiring. The sketch draws the battery level and tilt, and a big touch button that sends ON or OFF to the MQTT topic home/panel/light, which a Home Assistant or Node-RED flow can use to switch a lamp. Fill in the placeholder values for Wi-Fi and your broker, and do not post them anywhere public. The plain MQTT here is for a home network you control, not the internet. To add a sensor, plug a Grove I2C unit into Port A and read it with its own library, then show the value on the screen. The built-in battery is a small LiPo, so charge it with the supplied USB-C cable, keep it away from heat, and stop using the device if the case swells. Install the M5Stack boards package, choose M5Core2, and use M5Unified for the display and touch.
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// M5Stack Core2 panel: shows battery and tilt, and a touch button that publishes ON or OFF to MQTT.
// Libraries: M5Unified, PubSubClient. Board: M5Stack Core2 (M5Stack boards package). Fill in the placeholders.
#include <M5Unified.h>
#include <WiFi.h>
#include <PubSubClient.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const char* MQTT_HOST = "YOUR_BROKER_ADDRESS";
const char* MQTT_USER = "YOUR_MQTT_USER";
const char* MQTT_PASS = "YOUR_MQTT_PASSWORD";
WiFiClient net;
PubSubClient mqtt(net);
bool lightOn = false;
unsigned long lastDraw = 0;
void draw() {
M5.Display.fillScreen(BLACK);
M5.Display.setTextColor(WHITE);
M5.Display.setTextSize(2);
M5.Display.setCursor(10, 10);
M5.Display.printf("Battery %d%%", M5.Power.getBatteryLevel());
float ax, ay, az;
M5.Imu.getAccel(&ax, &ay, &az);
M5.Display.setCursor(10, 40);
M5.Display.printf("Tilt x %.2f y %.2f", ax, ay);
M5.Display.fillRoundRect(60, 100, 200, 90, 12, lightOn ? GREEN : DARKGREY);
M5.Display.setTextColor(BLACK);
M5.Display.setCursor(110, 135);
M5.Display.print(lightOn ? "LIGHT ON" : "LIGHT OFF");
}
void setup() {
auto cfg = M5.config();
M5.begin(cfg);
WiFi.begin(WIFI_SSID, WIFI_PASS);
mqtt.setServer(MQTT_HOST, 1883);
draw();
}
void loop() {
M5.update();
if (WiFi.status() == WL_CONNECTED && !mqtt.connected()) mqtt.connect("core2-panel", MQTT_USER, MQTT_PASS);
mqtt.loop();
auto t = M5.Touch.getDetail();
if (t.wasPressed() && t.x > 60 && t.x < 260 && t.y > 100 && t.y < 190) {
lightOn = !lightOn;
if (mqtt.connected()) mqtt.publish("home/panel/light", lightOn ? "ON" : "OFF");
draw();
}
if (millis() - lastDraw > 2000) { lastDraw = millis(); draw(); }
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Power on | Battery and tilt shown |
| Tilt the device | Numbers change |
| Touch the button | Colour changes and MQTT message sent |
| Two seconds later | Screen refreshes |
If it does not work
Touch does not respond
Use M5Unified and check the button area.
No MQTT message
Check Wi-Fi, broker address and credentials.
Wrong board error
Select M5Core2.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the M5Stack Core2 IoT panel?
M5Stack Core2 and Grove cable 4-pin.
How long does the M5Stack Core2 IoT panel take to build?
About 90 minutes for a beginner maker, in 7 steps.
How do I know the M5Stack Core2 IoT panel works?
Power on: Battery and tilt shown. Tilt the device: Numbers change. Touch the button: Colour changes and MQTT message sent.
Touch does not respond: what should I check?
Use M5Unified and check the button area.
No MQTT message: what should I check?
Check Wi-Fi, broker address and credentials.
Wrong board error: what should I check?
Select M5Core2.
