JavaScript: Relay Control with Johnny-Five on Node.js

The Johnny-Five Tutorial Series is geared towards Arduino programming on Node.js, using the Johnny-Five framework. Get caught up here.

Do not attempt this tutorial with a voltage source greater than 24V

A Relay is a type of actuator, or electrically controlled switch, that allows controlling one circuit with another circuit. The controller circuit is completely independent of the larger voltage power supply circuit (most applications will fall in the low-voltage range, which is defined by the IEC as 120-1500V DC). The Relay used in this tutorial will activate on an input of 5V and deactivate at 0V.

Relays have two operational modes:

  • NO, Normally Open. When provided with a voltage supply, the relay switch will close, completing the circuit.

  • NC, Normally Closed. When provided with any voltage supply, the relay switch will open, breaking the circuit.

These modes correspond to a constructor options property in Johnny-Five’s Relay class. The common case “NO” is the default, however a program could explicitly set the type to “NC” and maintain the expected electro-mechanical behavior:

// Create a Normally Closed relay instance
var relay = new five.Relay({
  type: "NC",
  pin: 10

// If NC wasn't specified above, this would
// actually turn OFF the relay. Since it has been
// specified, the relay will turn ON.


Connect the following:

  • Signal to D10
  • Power to 5V
  • Ground to GND

With the Extension Cord unplugged, carefully cut away a section of the exterior rubber sheathing and cut the hot line:

Then, connect one side of the hot line to the NO terminal and the other side to COM terminal:

Plug a lamp into the extension cord’s socket, then plug the extension cord into the wall outlet.


Save the following as relay.js:

var five = require("johnny-five"),
  board = new five.Board();

board.on("ready", function() {
  var relay = new five.Relay(10);

  // From the REPL, toggle the lamp on and off by calling:
  //   relay.toggle()
    relay: relay

Run node relay.js to see the program in action!


Based on the simple program above, how might it be extended to trigger the relay based on some sensory input? What kind of high-voltage devices could be controlled with this Relay? Post links to your own solutions in the comments!

Special thanks to my fellow Bocouper Trevor Sayre for his brilliantly crafted Fritzing Components.

** This tutorial was inspired by the Relay tutorial for TinkerKit

EDIT Thanks to Andrew Krespanis for indentifying the misuse of the term “high voltage”. Corrections have been made to correspond to the IEC 60038’s voltage range definitions.


We moved off of Disqus for data privacy and consent concerns, and are currently searching for a new commenting tool.

  1. \”NO, Normally Open. When provided with any voltage supply, the output is on. The default mode is LOW or \u201coff\u201d, requiring a HIGH signal to turn the relay off. This is the more commonly used connection.\”

    Should read: The default mode is LOW or \”ON\” requiring a HIGH signal to turn the relay OFF. No?

  2. Hi! I know this has been dead for a while but I cannot find anything on setting up a board with multiple relays, like an 8 or even 16 channel sainsmart relay. My button mashing attempts have failed. How do I correctly address multiple relays so I can access them all in the repl?

Contact Us

We'd love to hear from you. Get in touch!


P.O. Box 961436
Boston, MA 02196