Module: j5e/relay

Description

A module for controlling an electrically operated switch. Relays are often used to control high voltage devices such as motors, motors, and even motors. These higher voltages can be dangerous, so excercise caution when using this module.

A relay is an electrically operated switch. It consists of a set of input terminals for a single or multiple control signals, and a set of operating contact terminals. The switch may have any number of contacts in multiple contact forms, such as make contacts, break contacts, or combinations thereof.Read more on Wikipedia

Requires


Class: Relay

The Relay class allows for control of a relay

Constructor

new Relay(io, options, options.type)

Parameters

Name
Type
Description
io
(required)
number, string, or object

Description:
Pin identifier or IO Options (See instantiation)

options
(required)
object

Description:
Device configuration options

options.type
(required)
string

Description:
"NO" (Normally Open) or "NC" (Normally Closed)

Example

// Use a Relay
import Relay from "j5e/relay";

const relay = await new Relay(12);

Properties

Name
Type
Description
value
(read only)
number
A numerical value representing the relay state
isClosed
(read only)
boolean
True if the relay is closed
type
(read only)
string
"NC" if the relay is normally closed, "NO" if the relay is normally open

Methods

Relay.configure

Configure a Relay

relay.configure(options);

Returns: The instance on which the method was called

Parameters

Name
Type
Description
options
(required)
object

Description:
Device configuration options

options.type
number

Description:
"NC" if a relay is normally closed, "NO" if it is normally open
Default: "NO"

Example

import Relay from "j5e/relay";

const relay = await new Relay(14);
relay.configure({
  type: "NC"
});

// With type: "NC", relay.open() sets pin 14 high
relay.open();

Relay.close

Close the relay circuit

relay.close();

Returns:

Example

import Relay from "j5e/relay"

const relay = await new Relay(12);

// Turn it on
relay.close();

// Wait 5 seeconds and turn it off
system.setTimeout(function() {
  relay.open();
}, 5000);

Relay.open

Open the relay circuit

relay.open();

Returns:

Example

import Relay from "j5e/relay"

const relay = await new Relay(12);

// Turn it on
relay.close();

// Wait 5 seeconds and turn it off
system.setTimeout(function() {
  relay.open();
}, 5000);

Relay.toggle

Toggle the relay circuit

relay.toggle();

Returns:

Example

import Relay from "j5e/relay"
import {timer} from "j5e/fn";

const relay = await new Relay(12);

// Turn it on
relay.toggle();

// Wait 5 seeconds and turn it off
timer.setTimeout(function() {
  relay.toggle();
}, 5000);