pdf-icon

Arduino Quick Start

StamPLC Input & Output

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

Relay Example Program

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
*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

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
*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