<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Core2 with 4 Relay UNIT, example code crashes]]></title><description><![CDATA[<p dir="auto">Hi there,</p>
<p dir="auto">Using the Arduino example sketch for the 4 Relay UNIT (not the MODULE) my Core2 constantly crashes / reboots on an endless loop. It does appear to get through most of the setup code before rebooting. ie the LCD will display until I'm guessing it hits the 'relay.Init(0);' line and this brings everything to a halt.<br />
The stranger thing is though that this happens IF I'm lucky enough to get the compiled code to the Core2 - if I compile/upload again there's a 50/50 chance the compiler will error on:</p>
<pre><code>...\AppData\Local\Temp\.arduinoIDE-unsaved20231020-21796-14ea59g.a188\Unit_4RELAY_M5Core2\Unit_4RELAY_M5Core2.ino: In function 'void loop()':
...\AppData\Local\Temp\.arduinoIDE-unsaved20231020-21796-14ea59g.a188\Unit_4RELAY_M5Core2\Unit_4RELAY_M5Core2.ino:100:23: error: 'class UNIT_4RELAY' has no member named 'LED_ALL'
                 relay.LED_ALL(1);  // Turn on all the lights. 打开所有的灯
                       ^~~~~~~
...\AppData\Local\Temp\.arduinoIDE-unsaved20231020-21796-14ea59g.a188\Unit_4RELAY_M5Core2\Unit_4RELAY_M5Core2.ino:106:23: error: 'class UNIT_4RELAY' has no member named 'LED_ALL'
                 relay.LED_ALL(0);  // Turn off all the lights.  关闭所有的灯
Compilation error: 'class UNIT_4RELAY' has no member named 'LED_ALL'
</code></pre>
<p dir="auto">If I continue to compile after a time it will finish &amp; upload, but then crash as above.</p>
<p dir="auto">I'm only here after running into problems with the same 4 Relay UNIT with the Cardputer - the same happened there so I thought hang on, I'll try with my Core2 lying around and see how that works, and bring it back to the Cardputer from there... no luck!</p>
<pre><code>/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
*                  Equipped with M5Core2 sample source code
*                          配套  M5Core2 示例源代码
* 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&amp;Relay Async
                              | -Sync Mode:1 LED&amp;Relay Sync
  ---------------------------------------------------------------------------------
  Relay_ctrl_mode_reg[1]      | R/W | Relay &amp; 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 &lt;M5Core2.h&gt;
#include "Unit_4RELAY.h"

UNIT_4RELAY relay;

void setup() {
    M5.begin(true, false, true, true);  // Init M5Core2 And the I2C port(21,22).
                                        // 初始化 M5Core2 和I2C(21,22)端口
    M5.Lcd.setCursor(
        80, 0, 4);  // Set the cursor position to (80,0). 将光标位置设置为(80,0)
    M5.Lcd.print("4-RELAY UNIT\n\n");
    M5.Lcd.print("   Relay State: \n   Sync Mode: ");
    M5.Lcd.setCursor(0, 220, 2);
    M5.Lcd.print("Independent switch  Sync/Async    ALL relay");
    relay.Init(0);  // Set the lamp and relay to asynchronous mode(Async =
                    // 0,Sync = 1).  将灯和继电器设为非同步模式
}

char count_i   = 0;
bool sync_flag = 0, all_flag = 0;

void loop() {
    M5.update();  // Check button down state.  检测按键按下状态
    if (M5.BtnA.wasPressed()) {  // If button A is pressed.  如果按键A按下
        M5.Lcd.fillRect(160, 50, 100, 20, BLACK);
        M5.Lcd.setCursor(160, 50, 4);
        if (count_i &lt; 4) {
            M5.Lcd.printf("%d ON", count_i + 1);
            if (sync_flag) {
                relay.relayWrite(
                    count_i,
                    1);  // Open the relay at Count_i.  打开count_i处的继电器
            } else
                relay.ledWrite(count_i,
                               1);  // Turn on count_I to get led lights.
                                    // 打开count_i出得led灯
        } else {
            M5.Lcd.printf("%d OFF", (count_i - 3));
            if (sync_flag) {
                relay.relayWrite(
                    (count_i - 4),
                    0);  // Close the relay at Count_i.  关闭count_i处的继电器
            } else
                relay.ledWrite(
                    (count_i - 4),
                    0);  // Turn off the COUNt_I leds.  关闭count_i出得led灯
        }
        count_i++;
        if (count_i &gt;= 8) count_i = 0;
    } else if (M5.BtnB.wasPressed()) {
        sync_flag = !sync_flag;
        relay.switchMode(sync_flag);
        M5.Lcd.fillRect(160, 80, 100, 20, BLACK);
        M5.Lcd.setCursor(160, 80, 4);
        if (!sync_flag) {
            M5.Lcd.print("Async");
        } else {
            M5.Lcd.print("Sync");
        }
    }
    if (M5.BtnC.wasPressed()) {
        all_flag = !all_flag;
        M5.Lcd.fillRect(160, 50, 100, 20, BLACK);
        M5.Lcd.setCursor(160, 50, 4);
        if (all_flag) {
            M5.Lcd.printf("ALL.ON ");
            if (sync_flag)
                relay.relayAll(1);  // Open all the relays.  打开所有的继电器
            else
                relay.LED_ALL(1);  // Turn on all the lights. 打开所有的灯
        } else {
            M5.Lcd.printf("ALL.OFF");
            if (sync_flag)
                relay.relayAll(0);  // Close all relays.  关闭所有的继电器
            else
                relay.LED_ALL(0);  // Turn off all the lights.  关闭所有的灯
        }
    }
}
</code></pre>
<p dir="auto">Any ideas would be much appreciated.</p>
]]></description><link>https://community.m5stack.com/topic/5832/core2-with-4-relay-unit-example-code-crashes</link><generator>RSS for Node</generator><lastBuildDate>Sun, 03 May 2026 17:25:35 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/5832.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 20 Nov 2023 12:45:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Core2 with 4 Relay UNIT, example code crashes on Mon, 20 Nov 2023 17:30:36 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/user/pugmartin" aria-label="Profile: Pugmartin">@<bdi>Pugmartin</bdi></a></p>
<p dir="auto">you are correct, the example is broken in at least two ways. Firstly it doesn't compile and secondly a statement is missing.</p>
<p dir="auto">I had success compiling using the example <a href="https://github.com/m5stack/M5Unit-RELAY/blob/master/examples/Unit_4RELAY_M5Core2/Unit_4RELAY_M5Core2.ino" target="_blank" rel="noopener noreferrer nofollow ugc">here</a>.</p>
<p dir="auto">To fix the crash it needs a small fix in <code>setup()</code> - add this line before <code>relay.init(0)</code>:</p>
<pre><code>relay.begin();  // new line
relay.init(0);  // existing line
</code></pre>
<p dir="auto">Thanks<br />
Felix</p>
]]></description><link>https://community.m5stack.com/post/22837</link><guid isPermaLink="true">https://community.m5stack.com/post/22837</guid><dc:creator><![CDATA[felmue]]></dc:creator><pubDate>Mon, 20 Nov 2023 17:30:36 GMT</pubDate></item></channel></rss>