pdf-icon

Arduino Guide

Unit Relay Arduino Tutorial

1. Preparation

2. Example Program

Example Explanation
Unit Relay is a mechanical relay that provides three interfaces: NC, NO, and COM. It can help us control the ON/OFF of AC-220V@3A / DC-30V@3A circuits. To use it, simply configure the IO pin to HIGH/LOW to control the switch. When the control pin outputs LOW, NO and COM are disconnected, and NC and COM are connected. When the control pin outputs HIGH, NO and COM are connected, and NC and COM are disconnected.
#include <M5Unified.h>

#define RELAY_PIN 9

bool relay_no_status = false;

void setup()
{
    M5.begin();
    M5.Display.setFont(&fonts::FreeMonoBold12pt7b);
    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, LOW);
    M5.Display.setCursor(10, 40);
    M5.Display.println("Relay NO & COM Open");
}

void loop(void)
{
    M5.update();
    auto t = M5.Touch.getDetail();

    if (t.wasClicked() || M5.BtnA.wasClicked()) {
        relay_no_status = !relay_no_status;
        if (relay_no_status) {
            M5.Display.clear();
            M5.Display.setCursor(10, 40);
            M5.Display.println("Relay NO & COM Closed");
            digitalWrite(RELAY_PIN, HIGH);
        } else {
            M5.Display.clear();
            M5.Display.setCursor(10, 40);
            M5.Display.println("Relay NO & COM Open");
            digitalWrite(RELAY_PIN, LOW);
        }
    }
}

3. Compile and Upload

    1. Download Mode: Different devices may require entering download mode before programming. The steps for this may vary depending on the main control device. For more details, refer to the Arduino IDE Getting Started Guide , which includes specific download tutorial lists for each device.
  • For CoreS3, press and hold the reset button (for about 2 seconds) until the internal green LED lights up, and then release it. This indicates the device is in download mode and ready for programming.

    1. Select the device port and click the compile and upload button in the top left corner of Arduino IDE, then wait for the program to compile and upload to the device.

4. Switch Control

The Unit Relay switch can be controlled through the touchscreen, enabling load power control.

On This Page