M5Stack Beginner Project: implement a remote control function
18 Feb 2025
0 Comments
M5StickC Plus2 is a small development board based on an ESP32 chip with many powerful functions, including Bluetooth, Wi-Fi, OLED display, touch screen, and so on. Through it, we can easily realize the remote control function to control other devices or home appliances. This article will show you how to make a simple remote control using M5StickC Plus2 and introduce the structure and code implementation of the project.
LED Functions Explained
The M5StickC Plus2 is equipped with a programmable power LED that can be coded for simple on/off control or blinking effects. The LED can be used to indicate power status and, in conjunction with other sensor modules, as an indication of device status. The programmability of this LED provides a wide range of application scenarios for smart reminders or simple alarm systems.
Example Analysis
StickCP2.Power.setLed(1) is used to turn on the power LED of the M5StickC Plus2.
Accordingly, StickCP2.Power.setLed(0) is used to turn off the LED. This function is often used for status indication, such as whether the power supply is on or off, whether the device is functioning properly, or to make a simple signaling indication.

void setup() { // Retrieves device configuration. auto cfg = M5.config(); // Initializes the M5StickC Plus2. StickCP2.begin(cfg); // Rotates the display StickCP2.Display.setRotation(1); // sets text color to green StickCP2.Display.setTextColor(GREEN); // centers the text StickCP2.Display.setTextDatum(middle_center); // uses the 'rbitron_Light_24' font StickCP2.Display.setTextFont(&fonts::Orbitron_Light_24); StickCP2.Display.setTextSize(1); // Displays the message "Power LED" at the screen’s center. StickCP2.Display.drawString("Power LED", StickCP2.Display.width() / 2, StickCP2.Display.height() / 2); } void loop() { // inside power red led control //Turns on the power LED. StickCP2.Power.setLed(1); // Waits 1 second. delay(1000); // Turns off the LED. StickCP2.Power.setLed(0); //Waits 1 second. delay(1000); }
✔ Copied!
For an explanation of the infrared function see: M5Stack Beginner Guide: PLUS2 Infrared Capabilities
Remote control function realization
Step 1:Remote control infrared signal recognition
In this method, the M5Stack Plus2 acts as a remote control, sending infrared signals to other devices (e.g., TVs, air conditioners, etc.) via an infrared emitting module (IR LED).
-
Hardware required:
-
Hardware connections:
Arduino UNO | ---> | IR Receiver |
5V | ---> | VCC |
GPIO 11 | ---> | IN |
GND | ---> | GND |
-
Install the library
-
You can go ahead and install the IRremote library in the Arduino IDE. This library will help you generate standard-compliant IR signals.
-
Open Arduino IDE, select Tools -> Library Manager, search for IRremote, and install it.
-
Write receive infrared code
#include // Defines the pins to which the IR receiver sensor is connected const int RECV_PIN = 11; //Creating IR Receiving Objects IRrecv irrecv(RECV_PIN); // Create the decoding result object decode_results results; void setup() { // Initialize serial communications Serial.begin(115200); // Start IR reception irrecv.enableIRIn(); Serial.println("IR Receiver Ready"); } void loop() { if (irrecv.decode(&results)) { // IR signal detected Serial.print("IR Code Received: "); // Print the received IR signal Serial.println(results.value, HEX); // Receive the next signal irrecv.resume(); } delay(100); }
✔ Copied!
-
Click Tools -> Port to burn.
-
Click Tools -> Serial Monitor to monitor the IR signal.
Step 2: LED Control Project Explanation
After understanding the code functions and following the button tutorial, we can now combine button operations with LED control. The logic is straightforward:
-
Press Button A (StickCP2.BtnA.wasPressed()): Turn the LED on using
StickCP2.Power.setLed(1);
. -
Press Button B (StickCP2.BtnB.wasPressed()): Turn the LED off using
StickCP2.Power.setLed(0);
.
We implement this using
if
conditions to detect when Button A or Button B is pressed. This provides a responsive way to toggle the LED using simple button interactions, making it an easy project to get started with.M5Stack Plus2 implements remote control functions
The sample code emits an infrared signal through the IR LED when the button of M5Stack Plus2 is pressed, simulating the operation of a remote control. You can change the signal number in
IrSender.sendNEC()
to control different devices as needed.
#define DISABLE_CODE_FOR_RECEIVER #define SEND_PWM_BY_TIMER #define IR_TX_PIN 19 #include "M5StickCPlus2.h" #include void setup() { auto cfg = M5.config(); //To understand the underlying logic of the initialization with begin(), you can refer to the Dependent Library. StickCP2.begin(cfg); //Display rotation directions StickCP2.Display.setRotation(1); // The color of the text displayed on the screen. StickCP2.Display.setTextColor(GREEN); //Text alignment middle_center means aligning the center of the text to the specified coordinate position. StickCP2.Display.setTextDatum(middle_center); //Font Styles StickCP2.Display.setTextFont(&fonts::Orbitron_Light_24); //Font size StickCP2.Display.setTextSize(1); IrSender.begin(DISABLE_LED_FEEDBACK); // Start with IR_SEND_PIN as send pin IrSender.setSendPin(IR_TX_PIN);//Infrared Signal Transmit Pin Settings } void loop() { if (StickCP2.BtnA.wasPressed()) { // Send IR code for power button IrSender.sendNEC(0x25AE7EE3, 32); // Example NEC code for TV (need to replace with actual code) StickCP2.Display.clear(); StickCP2.Display.drawString("Power Signal Sent", StickCP2.Display.width() / 2, StickCP2.Display.height() / 2 - 40); delay(5000); // Sent every 5 seconds } if (StickCP2.BtnB.wasPressed()) { // Send IR code for volume up button //IrSender.sendNEC(25AE7EE3, 32); // Send volume up signal StickCP2.Display.clear(); StickCP2.Display.drawString("Volume Up Sent", StickCP2.Display.width() / 2, StickCP2.Display.height() / 2 - 40); delay(5000); // Sent every 5 seconds } StickCP2.update(); // Update button status }
✔ Copied!
Tags:
Leave a comment
All blog comments are checked prior to publishing