
Arduino Quick Start
The RS-232 converter module converts TTL-level signals to RS-232 levels, enabling protocol conversion and control of compatible devices.
Devices that communicate over a serial port can be connected to the M5Core default serial pins, PIN17 (TXD) and PIN16 (RXD).
This program implements a transparent bidirectional serial bridge: Serial2 (RX16/TX17, connected to RS-232, 115200/8N1, enabled by GPIO5) and Serial (UART0, connected to the PC through the onboard USB-to-serial converter) pass bytes through in both directions. The screen shows the baud rate, cumulative TX/RX byte counts, and the most recently received character in real time for field debugging.
#include <M5Unified.h>
constexpr int RX_PIN = 16;
constexpr int TX_PIN = 17;
constexpr int ENABLE_PIN = 5;
constexpr uint32_t BAUD = 115200;
uint32_t txCount = 0;
uint32_t rxCount = 0;
int lastByte = -1;
void show()
{
M5.Display.fillScreen(TFT_BLACK);
M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Display.setTextSize(3);
M5.Display.setCursor(80, 15);
M5.Display.print("RS232");
M5.Display.setTextSize(2);
M5.Display.setCursor(10, 70);
M5.Display.printf("Baud: %lu\nTX: %lu\nRX: %lu\n",
(unsigned long)BAUD,
(unsigned long)txCount,
(unsigned long)rxCount);
M5.Display.setCursor(10, 185);
if (lastByte < 0) {
M5.Display.print("Last: ---");
} else if (lastByte >= 32 && lastByte <= 126) {
M5.Display.printf("Last: %c 0x%02X", (char)lastByte, lastByte);
} else {
M5.Display.printf("Last: 0x%02X", lastByte);
}
}
void setup()
{
auto cfg = M5.config();
M5.begin(cfg);
Serial.begin(115200);
Serial2.begin(BAUD, SERIAL_8N1, RX_PIN, TX_PIN);
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, HIGH);
M5.Display.setBrightness(100);
show();
Serial.println("RS232 ready");
}
void loop()
{
M5.update();
bool changed = false;
while (Serial.available()) {
Serial2.write(Serial.read());
txCount++;
changed = true;
}
while (Serial2.available()) {
lastByte = Serial2.read();
Serial.write(lastByte);
rxCount++;
changed = true;
}
if (changed) show();
}