Rick Waldron edited this page May 10, 2016 · 6 revisions

The Switch class constructs objects that represent a single Switch attached to the physical board.


  • pin A Number or String address for the pin.

  • options An object of property parameters.

    Property Type Value/Description Default Required
    pin Number, String Any Pin. The Number or String address of the Switch pin yes
    type String "NO" or "NC". Indicate if the switch is "normally open" or "normally closed" "NO" no


Property Name Description Read Only
id A user definable id value. Defaults to a generated uid. No
pin The pin value. No
isClosed Boolean indicating whether the switch is closed. Yes
isOpen Boolean indicating whether the switch is open. Yes

Component Initialization


new five.Switch(8);

// Options object with pin property
new five.Switch({
  pin: 8

Switch SPDT

Switch SPDT


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

board.on("ready", function() {
  var spdt = new five.Switch(8);
  var led = new five.Led(13);

  spdt.on("open", function() {

  spdt.on("close", function() {


  • open The open event will emit when the circuit opens.
  • close The close event will emit when the circuit closes.


Switch supports a Switches collection class, which allows multiple Switch instances to be controlled via a single instance object. Events emitted by instances of the Switch class are forwarded through instances of the Switches class. The handler receives the instance that emitted the event as the first parameter.

new five.Switches([ 2, 3, 4, 5 ]);
new five.Switches([ { pin: 2 }, { pin: 3 }, { pin: 4 }, { pin: 5 } ]);
new five.Switches([ switch1, switch2, switch3 ]);
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.