Environment setup: Refer to Arduino IDE Getting Started Tutorial to complete the installation of the IDE, and install the corresponding board manager and required libraries according to the development board you are using.
Libraries used:
Hardware used:



#include <M5Unified.h>
void setup() {
// Initializatization
auto cfg = M5.config();
M5.begin(cfg);
// Serial2.begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert)
Serial2.begin(9600, SERIAL_8N1, 26, 0); // (RX: G26, TX: G0)
Serial.begin(115200);
// setup display
M5.Display.setRotation(1);
M5.Display.fillScreen(BLACK);
M5.Display.setTextColor(ORANGE);
M5.Display.setTextSize(2);
M5.Display.setCursor(0, 10);
M5.Display.println("RS485 message:");
}
void loop() {
M5.update();
if (M5.BtnA.wasClicked()) { // Click StickC-Plus2 Button A to send data
Serial.println("RST485 sent: Hello M5Stack!");
Serial2.println("Hello M5Stack!");
}
if (Serial2.available()) { // Received data and display on StickC-Plus2's screen
char ch = Serial2.read();
M5.Display.setTextColor(CYAN);
M5.Display.setTextSize(2);
M5.Display.print(ch);
}
delay(10);
}Enter download mode: Before uploading programs to different Stick devices, you need to install the corresponding driver. The required driver and installation procedure may vary with different controllers. Refer to the bottom section of the Arduino IDE Getting Started Tutorial page for detailed steps based on your specific device.
Select the device port, click the “Verify & Upload” button in the top left corner of Arduino IDE, and wait for the program to compile and upload to the device.
This program will use the TX and RX interfaces of Hat RS485 to send data to each other (press ButtonA on StickC-Plus2 to send data). The transmitted data will be displayed on the screen of StickC-Plus2 and printed on the computer’s serial monitor:
