JavaScript and Arduino programming with Johnny-Five

kumar kundan
Sep 4, 2018 · 3 min read
“brown circuit board on laptop” by Louis Reed on Unsplash

Johnny-Five is the JavaScript Robotics & IoT Platform.
Released by Bocoup in 2012. Johnny-Five is open source and has growing developer communities

So let’s get started with Johnny-Five to program the Arduino Uno!

1. Setting Up Arduino

Hardware and software you need:

1. Arduino (Genuino) Uno
2. Arduino IDE (download the Arduino IDE and install it on your computer)
3. Node.js

First connect your Arduino Uno to your computer with a USB cable. You will need the Arduino IDE only for the initial setup.

On Arduino IDE, go to Tools > Port and make sure the right board, Arduino Uno, is connected to the right port (tty.usbmodem… for Mac, cu.usbmodem…for Windows).

Johnny-Five communicates with Arduino using the Firmata protocol, so you need to install StandardFirmata:

On IDE, open File > Examples > Firmata > StandardFirmata.
Click the upload button (arrow button).
Wait until the IDE message window says “Done uploading”.
Close the IDE. You don’t need the IDE anymore unless you want to keep using it for coding.

2. Hello World

Make sure Node.js is installed on your machine. Create an appropriate directory like js-hello, and then cd into the directory and install Johnny-five using the npm package manager.

$ npm install johnny-five

Now, let’s write your first project Hello World with Johnny-Five. Since you’ve got some hardware, you’re going to create the “Hello world” of hardware, which is a blinking LED light!

Hardware You Need

1 Arduino Uno
1 LED
1 breadboard
2 male/male jumper wire (1 red, 1 black)
1 resistor(200–330Ω)

Know Your LEDs

LEDs, short for light-emitting diodes, are polarized: the positive (+) side is called the anode, and the negative side is called the cathode. Usually, each LED has two legs, and the longer leg is an anode pin. This is important when you build a circuit.

Assembling a Circuit
Let’s use a color convention to avoid confusion:

Black wires for ground
Red wires for voltage
Your wires do not have to be red and black, but do use two different colors so you don’t confuse yourself. The best practice is to use red to connect to the positive end (in this practice, use pin 13), and block to ground (GND pin).

You can learn more about breadboards, resistors, and all the basics of the parts at Adafruit!

Blinking an LED with Johnny-Five
Now, you are going to work on the software side.

Create a file, blink.js, and paste the code below:

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

board.on(‘ready’, function() {
var led = new five.Led(13); // pin 13
led.blink(500); // 500ms interval
});

Run:

$ sudo node blink.js

The LED should blink at a 500ms interval, otherwise check both the circuit and code.

Thanks for reading!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade