Skip to main content


Latest Version: 1.0.0

This library implements an interface for a latching relay. It requires two imp pins: one to toggle the relay’s on state, and another to toggle the off state.

You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.

To add this library to your project, add #require "LatchingRelay.class.nut:1.0.0" to the top of your device code

Class Usage

Constructor: LatchingRelay(onPin, offPin, [defaultState])

To instantiate a new LatchingRelay object, you must pass it two imp pins which will both be configured by the library as DIGITAL_OUT. An optional third, integer parameter can be passed to set the initial state (0 = off, 1 = on); the default is 0.

#require "LatchingRelay.class.nut:1.0.0"

// Latching relay on an imp002 connected to pins 9 and A
relay <- LatchingRelay(hardware.pin9, hardware.pinA, 1);

Class Methods


The write() method sets the relay to on (state = 1), or off (state = 0). Invalid states are ignored.

// Turn the relay on:


The toggle() method sets the relay to the opposite state, ie. if it is on, turn it off, and vice versa.

// Toggle the relay


The getState() method returns the current state of the relay.

// Send the current state of the relay to the agent
agent.on("getState", function(data) {
    agent.send("getStateResponse", relay.getState());

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release


The LatchingRelay library is licensed under the MIT License.