G21(SDA) , G22(SCL)。
#> 注意 | 本模块只能在同步模式下控制继电器,如果在异步模式下对继电器进行控制将无效,只能控制 LED 指示灯。/*
*******************************************************************************
* Visit for more information: https://docs.m5stack.com/en/unit/4relay
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/4relay
*
* Product: Unit 4RELAY.
* Date: 2022/7/22
*******************************************************************************
Please connect to port A,Control 4 relays and demonstrate the asynchronous
control relay LED 请连接A端口,控制4继电器,并演示异步控制继电器LED
-------------------------------------------------------------------------------
RELAY control reg | 0x10
-----------------------------------------------------------------------------
Relay_ctrl_mode_reg[0] | R/W | System control
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| R | R | R | R | R | R | R | Sync Mode |
| -Sync Mode:0 LED&Relay Async
| -Sync Mode:1 LED&Relay Sync
---------------------------------------------------------------------------------
Relay_ctrl_mode_reg[1] | R/W | Relay & LED control
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| LED1| LED2| LED3| LED4| RLY1| RLY2| RLY3| RLY4|
-------------------------------------------------------------------------------*/
// Note: The relay can only be controlled in synchronous mode, if the relay is
// controlled in asynchronous mode, it will be invalid.
// 注意:只能在同步模式下控制继电器,如果在异步模式下对继电器进行控制将无效,.
#include <M5Unified.h>
#include "Unit_4RELAY.h"
UNIT_4RELAY relay;
char count_i = 0;
bool sync_flag = 0, all_flag = 0;
void show_main_ui() {
M5.Display.fillScreen(WHITE);
M5.Display.setTextColor(BLACK);
M5.Display.setFont(&fonts::FreeMonoBold12pt7b);
M5.Display.println("4-RELAY UNIT");
M5.Display.println("Relay State:");
M5.Display.print("Sync Mode: \n");
M5.Display.print("A:Indep. \nB:Sync/Async \nC:ALL relay \n");
}
void setup() {
M5.begin();
M5.Power.begin();
Serial.begin(115200);
show_main_ui();
relay.begin();
relay.Init(0); // Async mode at start
}
void loop() {
M5.update();
// Press the A key: Independently control a single relay or LED
if (M5.BtnA.wasPressed()) {
M5.Display.fillRect(160, 50, 100, 20, WHITE);
M5.Display.setCursor(160, 50);
if (count_i < 4) {
M5.Display.printf("%d ON", count_i + 1);
if (sync_flag) {
relay.relayWrite(count_i, 1);
} else {
relay.ledWrite(count_i, 1);
}
} else {
M5.Display.printf("%d OFF", (count_i - 3));
if (sync_flag) {
relay.relayWrite((count_i - 4), 0);
} else {
relay.ledWrite((count_i - 4), 0);
}
}
count_i++;
if (count_i >= 8) count_i = 0;
}
// Press the B key: Switch between synchronous and asynchronous modes
if (M5.BtnB.wasPressed()) {
sync_flag = !sync_flag;
relay.switchMode(sync_flag);
M5.Display.fillRect(160, 80, 100, 20, WHITE);
M5.Display.setCursor(160, 80);
if (!sync_flag) {
M5.Display.print("Async");
} else {
M5.Display.print("Sync");
}
}
// Press the C key: All relays /LED switches
if (M5.BtnC.wasPressed()) {
all_flag = !all_flag;
M5.Display.fillRect(160, 50, 100, 20, WHITE);
M5.Display.setCursor(160, 50);
if (all_flag) {
M5.Display.printf("ALL.ON ");
if (sync_flag)
relay.relayAll(1);
else
relay.ledAll(1);
} else {
M5.Display.printf("ALL.OFF");
if (sync_flag)
relay.relayAll(0);
else
relay.ledAll(0);
}
}
}下载模式:不同设备进行程序烧录前需要进入下载模式,不同的主控设备该步骤可能有所不同。详情可参考Arduino IDE上手教程页面底部的设备程序下载教程列表,查看具体的操作方式。
Basic v2.7 长按复位按键 (大约 2 秒) 直到内部绿色 LED 灯亮起,便可松开,此时设备已进入下载模式,等待烧录。(注:gif 为 CoreS3 的示例,Basic v2.7 烧录操作步骤一致)