Control arduino with javascript (getting started with johnny-five)

In this article i’ll show you how to get started with johnny-five and write your first script for arduino, in our case we will be controlling an LED blinking and pulsing.

Before we get started we have to make sure our environment is setup.

Installing Node.js - go to this link and install the proper version of node for your operating system.

Install Arduino Software (IDE) - head over to arduino download page download Arduino IDE and install it.

After the installation of both Node.js and Arduino IDE open up Arduino IDE, here we need to upload the StandardFirmataPlus sketch to our arduino board. Before that make sure in Tools -> Ports
the port for your arduino board is selected. Open up File -> Examples -> Firmata -> StandardFirmataPlus
After the sketch is loaded in the IDE click the upload button so you can upload the sketch to the Arduino board.

Now we are ready to start fiddling around with the arduino. Make a folder, cd in to it and install johnny-five module

1
$ npm install johnny-five

Create a file controller.js. Here we will write our first script, for this one we will make the LED blink.

1
2
3
4
5
6
7
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
var led = new five.Led(13);
led.blink(1000);
});

Let’s explain what’s going on. First we requre johny-five library var five = require(“johnny-five”);
Next we create an instance of var board = new five.Board();
And when the board is ready on that event we start blinking the LED.

1
2
3
4
5
6
board.on("ready", function() {
// Create LED component instance and assign pin 13 to it
var led = new five.Led(13);
// start blinking the LED every second
led.blink(1000);
});

On your arduino put the LED on. The shorter leg in the grounding(GND) and the longer in pin number 13.

Let’s run the script

1
$ node controller.js

Voilà it’s working, LED is blinking.

Now let’s try to pulse the LED.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
// Create LED component instance and assign pin 11 to it (pin 11 is a PWM style pin usually denoted by ~)
var led = new five.Led(11);
// start LED pulsing
led.pulse();
// stop LED pulsing after 5 seconds
this.wait(5000, function() {
// stop() terminates the interval
// off() shuts the led off
led.stop().off();
});
});

Here instead of blinking we are pulsing the LED. And after 5 seconds we turn the LED off and stop the interval. Here we moved the longer leg of the LED to pin 11 which is a PWM “Pulse Width Modulation” pin basically it’s controlling analog devices with a digital output.

Let’s run this script

1
$ node controller.js

At this case we can observe that the LED will fade in and out for 5 seconds and than turn off.

Well that’s about it, we learned how to setup the environment for johny-five and covered some simple examples i also recommend taking a look at johnny-five documentation to find out more.
In the future we will be taking a look at more advanced lessons so to be updated head over to my youtube and subscribe or follow me on facebook or twitter.

If you have any questions please feel free to ask below.
Cya in the next one.



Share