pdf-icon

Arduino Guide

Input & Output

StamPLC input signal and relay control related APIs and example programs.

Relay Example Program

/*
 *SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 *
 *SPDX-License-Identifier: MIT
 */
#include <Arduino.h>
#include <M5StamPLC.h>

void setup()
{
    /* Init M5StamPLC */
    M5StamPLC.begin();
}

void loop()
{
    static bool relay_state = false;

    /* Toggle relay state */
    relay_state = !relay_state;
    for (int i = 0; i < 4; i++) {
        M5StamPLC.writePlcRelay(i, relay_state);
        printf("Write Relay %d to %s\n", i, relay_state ? "ON" : "OFF");
        delay(500);
    }

    delay(1000);
}

Relay API

writePlcRelay

Function Prototype:

void writePlcRelay(const uint8_t& channel, const bool& state);

Function Description:

  • Controls writing the relay state for the specified channel

Parameters:

  • const uint8_t& channel:

    • 0-3
  • const bool& state:

    • true: ON
    • false: OFF

Return Value:

  • null

writePlcAllRelay

Function Prototype:

void writePlcAllRelay(const uint8_t& relayState);

Function Description:

  • Controls writing the relay state for all channels

Parameters:

  • const uint8_t& relayState:
    • bit[0:3] -> channel[0:3]
    • bit == 1: ON
    • bit == 0: OFF

Return Value:

  • null

readPlcRelay

Function Prototype:

bool readPlcRelay(const uint8_t& channel);

Function Description:

  • Reads the relay state for the specified channel

Parameters:

  • const uint8_t& channel:
    • 0-3

Return Value:

  • bool:
    • true: ON
    • false: OFF

Input Example Program

/*
 *SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 *
 *SPDX-License-Identifier: MIT
 */
#include <Arduino.h>
#include <M5StamPLC.h>

void setup()
{
    /* Init M5StamPLC */
    M5StamPLC.begin();
}

void loop()
{
    static std::array<bool, 8> input_list;

    // Read inputs
    for (int i = 0; i < 8; i++) {
        input_list[i] = M5StamPLC.readPlcInput(i);
    }

    // Print input reading result
    printf("Input: %d, %d, %d, %d, %d, %d, %d, %d\n", input_list[0], input_list[1], input_list[2], input_list[3],
           input_list[4], input_list[5], input_list[6], input_list[7]);

    delay(1000);
}

Input API

readPlcInput

Function Prototype:

bool readPlcInput(const uint8_t& channel);

Function Description:

  • Reads the state of the specified input signal channel

Parameters:

  • const uint8_t& channel:
    • channel 0-7

Return Value:

  • bool:
    • true: HIGH
    • false: LOW
On This Page