Johnny Five Tutorial

Learn how to talk to electronic devices using JavaScript

Johnny Five is a super cool library that allows us to interface with electronic devices using JavaScript.

Devices like Arduino are usually programmed in the Arduino Language, which is a particular framework for C/C++.

Due to the limited capabilities of those electronic devices, with low memory and processor speed, other languages can’t natively be used to write programs for them.

But there is a special protocol, called Firmata, that allows languages to interface with Arduino.

Johnny Five is the great library that allows us to do so using JavaScript, and in particular Node.js.

Setting up your Arduino to work with Johnny Five

Download the Arduino IDE from http://arduino.cc/en/main/software.

Open it, and you’ll see something like this:

Connect the Arduino board to your USB port.

Go to Tools -> Port and make sure the port selected is the one the Arduino is connected to (in my case /dev/cu.usbmodem14101). You should have a few options, and Arduino IDE should already pre-detect it for you.

Go to Tools -> Board and make sure the device you have is correctly selected.

In my case, the device is an Arduino Uno compatible board.

Then go to File -> Examples -> Firmata and choose StandardFirmataPlus:

This will load a new window:

Click the right arrow icon on the toolbar to compile and load the program on the Arduino board:

Great! Now you’re all set, on the hardware side, to use Johnny Five.

The Arduino device must remain connected

One thing that you need to note about Johnny Five and this approach of writing electronics applications using JavaScript/Node.js is that we can’t detach the device from the computer.

Usually when you program an Arduino using the Arduino Language, which is C/C++, once a program is loaded on the device, you can move it anywhere, and as soon as Arduino is booted because it’s powered on, the program starts running.

The simplicity of Arduino is so that there’s no operating system, no runtime, nothing is executed on the device other than the program loaded in memory.

The program loaded in memory now is the StandardFirmataPlus program, that provides Johnny Five a set of primitives, an API implemented through the Firmata protocol, that we can call programmatically through the USB connection.

As soon as we disconnect the Arduino, the Johnny Five program stops its execution.

One way we can overcome this problem, if we want to deploy our device somewhere for example, is to use a Raspberry PI, connect the Arduino to it, and run the Node.js app from there, maybe from your computer using a VLC or SSH connection.

This is out of the scope of this lesson, but check out How to connect to a Raspberry Pi using a Mac and How to make sure the Raspberry Pi has always the same IP address if you’re interested in doing so.

You can also overcome this problem in other ways, like using an additional WiFi module.

For the sake of understanding how we can program electronics with JavaScript, however, it will be enough to have the device connected to our computer.

An overview of the functionality offered by Johnny Five

Johnny Five offers access to several APIs we can use to access commonly used electronic components:

and much more.

All is available as part of the johnny-five npm package:

npm install johnny-five

This is how you can initialize a board and wait until it’s available:

const { Board } = require('johnny-five')
const board = new Board()

board.on('ready', () => {
  //ready!
})

I will not cover the entire API, which can be consulted at http://johnny-five.io/api, but I’ll give you an example of how to work with an LED.

Get the Led class from the library and initialize a new Led object using new Led(), passing the pin number as parameter:

const { Led } = require('johnny-five')
//...
const led = new Led(13)

Once you have the led object, you can call its methods, which include:

  • led.on() to turn it on
  • led.off() to turn it off
  • led.toggle() to toggle its current state
  • led.blink() to toggle it indefinitely, by default every 100ms
  • led.stop() to stop the blinking

This is the first in a series of Johnny Five tutorials. In the next Johnny Five tutorial I’ll show you more about how to use it!

Download my free JavaScript Beginner's Handbook and check out my JavaScript Masterclass!

⭐️ install javascript into your brain with the JavaScript Masterclass. -26 days ⭐️