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()``.
relay.begin(pin: int, active: any = None, initial: int = None) -> relay_device
relay.begin(pin: int, active: any = None, initial: int = None) -> relay_deviceCreate a relay device.
| 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. |
relay_device relay_device object.
relay.begin().on() -> bool
relay.begin().on() -> boolTurn the relay on using the configured active polarity.
relay.begin().off() -> bool
relay.begin().off() -> boolTurn the relay off using the configured active polarity.
relay.begin().toggle() -> bool
relay.begin().toggle() -> boolReverse the currently commanded relay state.
relay.begin().is_on() -> bool
relay.begin().is_on() -> boolReturn True when the relay is currently commanded on.
relay.begin().is_off() -> bool
relay.begin().is_off() -> boolReturn True when the relay is currently commanded off.
relay.begin().set(state) -> bool
relay.begin().set(state) -> boolSet the relay state. Pass True, False, "on", or "off".
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
state |
str |
positional or keyword | Yes | Required value. |
relay.begin().pulse(duration_ms) -> bool
relay.begin().pulse(duration_ms) -> boolTurn the relay on, then automatically turn it off after duration_ms milliseconds.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
duration_ms |
int |
positional or keyword | Yes | Required value. |
relay.begin().Notes
relay.begin().Notesis_on() and is_off() report the commanded software state, not physical feedback from the switched load.