MojoScale Studio Docs
API Reference

relay

High-level relay control for on/off loads.

Studio Docs Sensors & I/O

import relay

The relay module drives a relay module or solid-state relay from an ESP32 GPIO pin. It hides active-high and active-low wiring differences, so application code can use logical methods like ``on()``, ``off()``, and ``toggle()``.

Quick example

import relay
import system

pump = relay.begin(pin=4, active="low")
pump.on()

system.schedule("pump_off", 5000, 0, 1, lambda: pump.off())

lock = relay.begin(pin=5, active="low", initial="off")
lock.pulse(1000)

if pump.is_on():
    print("Pump running")

importrelay

High-level relay control for on/off loads.

The relay module drives a relay module or solid-state relay from an ESP32 GPIO pin. It hides active-high and active-low wiring differences, so application code can use logical methods like ``on()``, ``off()``, and ``toggle()``.

API 9 available

relay.begin(pin: int, active: any = None, initial: int = None) -> relay_device

Create a relay device.

Parameters
Name Type Pass as Required Description
pin int positional or keyword Yes GPIO output pin connected to the relay control input.
active any positional or keyword No Optional electrical level that activates the relay. Use "high" or "low". Defaults to "high". Many inexpensive relay modules use active="low".
initial int positional or keyword No Optional initial logical state. Use "off" or "on". Defaults to "off" so physical equipment starts safely disabled.
Returns

relay_device relay_device object.

relay.begin().on() -> bool

Turn the relay on using the configured active polarity.

relay.begin().off() -> bool

Turn the relay off using the configured active polarity.

relay.begin().toggle() -> bool

Reverse the currently commanded relay state.

relay.begin().is_on() -> bool

Return True when the relay is currently commanded on.

relay.begin().is_off() -> bool

Return True when the relay is currently commanded off.

relay.begin().set(state) -> bool

Set the relay state. Pass True, False, "on", or "off".

Parameters
Name Type Pass as Required Description
state str positional or keyword Yes Required value.

relay.begin().pulse(duration_ms) -> bool

Turn the relay on, then automatically turn it off after duration_ms milliseconds.

Parameters
Name Type Pass as Required Description
duration_ms int positional or keyword Yes Required value.

relay.begin().Notes

is_on() and is_off() report the commanded software state, not physical feedback from the switched load.