
Arduino 上手教程
环境配置: 参考 Arduino IDE 上手教程完成 IDE 安装,并根据实际使用的开发板安装对应的板管理,与需要的驱动库。
使用到的驱动库:
使用到的硬件产品:

G15 (SCL)、G13 (SDA),RGB LED IO 为 P5 (SYS_LEDR)、P6 (SYS_LEDG)、P7 (SYS_LEDB),继电器开关 IO 为 P2 (REL_EN)。实物连接组装如下图所示:
#include "M5StamPLC.h"
M5StamPLC_AC stamplc_ac;
void setup()
{
/* Init M5StamPLC */
M5StamPLC.begin();
M5StamPLC.Display.setFont(&fonts::FreeMonoBold12pt7b);
M5StamPLC.Display.drawCenterString("StamPLC AC", 120, 60);
/* Init M5StamPLC-AC */
while (!stamplc_ac.begin()) {
printf("M5StamPLC-AC init failed, retry in 1s...\n");
delay(1000);
}
}
void loop()
{
static bool relay_state = false;
/* Toggle M5StamPLC-AC relay state */
relay_state = !relay_state;
stamplc_ac.writeRelay(relay_state);
printf("Write M5StamPLC-AC Relay to %s\n", stamplc_ac.readRelay() ? "ON" : "OFF");
if (relay_state)
{
stamplc_ac.setStatusLight(0, 1, 0);
printf("Set M5StamPLC-AC status light to green\n");
}
else
{
stamplc_ac.setStatusLight(1, 0, 0);
printf("Set M5StamPLC-AC status light to red\n");
}
delay(1000);
}
串口监视器反馈如下所示:
Write M5StamPLC-AC Relay to ON
Set M5StamPLC-AC status light to green
Write M5StamPLC-AC Relay to OFF
Set M5StamPLC-AC status light to red
...