Welcome to the third tutorial in our series geared towards Arduino programming on Node.js, using the Johnny-Five framework. Get caught up here.

Johnny-Five is a Firmata based Arduino programming framework. See: JavaScript: Arduino Programming on Node.js.


In the first tutorial you created a program to control a servo via REPL; then, in the second tutorial you added a slider to control the servo. In this tutorial, we’re going to set the servo and slider aside and build a simple “on/off” switch with a bumper and an led:

New Parts list:

Tutorial Requirements:

  • One (1) Arduino Microcontroller Board
  • One (1) 9V wall wart adapter plug or 9v battery harness
  • Seven (7) Jumper cables: red (3), black (2), yellow (2)
  • One (1) Any color Led
  • One (1) Bumper Switch/Button
  • One (1) 10k OHM

First, set up the hardware using this schematic as your guide:

Connect the following…

  1. Bumper: Black > GND to Breadboard to 10k OHM Resistor
  2. Bumper: Red > 5v to Breadboard
  3. Bumper: Red > Breadboard to Bumper
  4. Bumper: Yellow > Signal to Breadboard on grounded resistor
  5. Bumper: Yellow > Breadboard to Pin 7
  6. Led: Cathode > GND
  7. Led: Anode > Pin 13
  8. Microcontroller > USB
  9. 9V Power

Assuming you’re using the actual repo, open up eg/button-bumper.js, or follow along here…

button-bumper.js

var five = require("../lib/johnny-five.js"),
    bumper, led;

five.Board().on("ready", function() {

  bumper = new five.Button(7);
  led = new five.Led(13);

  bumper.on("hit", function() {

    led.on();

  }).on("release", function() {

    led.off();

  });
});

And that’s it!

Run node eg/button-bumper.js1 and try tapping and/or holding the bumper down — the led should switch on and off as you press and release.

Remember to watch this space for more tutorials!


Note: There is a known issue in where the Firmata protocol layer has issues freeing itself on the serial line which results in the program hanging in the ready state. For now, ^C to kill the hanging program and simply run it again.