Getting started with hardware hacking can be quite intimidating for some folks. The world of electronics is completely foreign for most developers; additionally, it requires you to write C/C which is efficient but not everyone feels comfortable with. However, the Nodebots movement is a nice way to get started with hardware development using JavaScript. In this article, I want to guide you through some very basic things to get started.

JavaScript && Hardware? How?

There are quite a few different projects aimed at hardware development with JavaScript. Some come with special hardware like the Tessel or Espruino. Both of them are great projects but in this post we will focus on another option.

The option we’ll use is the npm module johnny-five . Johnny-Five isn’t limited to certain hardware but instead supports a wide range of different microcontroller platforms (including the Tessel) using various I/O plug-ins. The only drawback is that some of these platforms (like Arduino) don’t allow you to execute the JS code directly on the microcontroller. Instead you execute it on a “host system”.

The way it works for Arduino is that you execute the JS code on your “host system” (e.g. your computer) using Node.js and the johnny-five module sends the actions via the serialport module to your Arduino that is running the firmata firmware that is designed for remote controlling an Arduino. This way we have full control over the hardware while it’s connected to our computer.

Requirements

For this tutorial you will need a few things. First of all you need the following things installed on your computer:

Node.js and npm

The Arduino IDE to upload the firmata firmware on your microcontroller

firmware on your microcontroller Your favorite code editor

Additionally you will need some hardware components:

An Arduino (or Arduino clone). I’ll be using an Arduino Nano. Check the Johnny-Five page for the different ones that are supported

1x red LED

1x button

1x resistor of 220Ω (has the stripes Red-Red-Brown). Note that for different LED colors you might need different resistance values here

1x resistor of 10kΩ (has the stripes Brown-Black-Orange)

1x breadboard to wire up the setup – something similar to this size should be sufficient

a few jumper or solid wires to connect everything

Installing the firmata firmware

The first thing we need to do is to make sure your Arduino is correctly recognized. Plug it into your computer and click on the Tools menu. In there you want to choose the respective board you are using — in my case I had to choose Arduino Nano.

Also make sure to choose the right port under Tools → Port . It should contain something like usbserial in it. If such a port is not listed make sure you have the necessary drivers for your Arduino installed.



Next we need to install the firmata firmware on our Arduino to be able to communicate with it. Open your Arduino IDE and install the Servo libraries by clicking on Sketch → Include Library → Manage Libraries and searching for the library.

Lastly we need to download StandardFirmataPlus.ino file into our Arduino project directory (in Mac by default under ~/Documents/Arduino ) and upload it to our Arduino. You can find your Arduino directory by going into Preferences and looking up the Sketchbook location.

Download the StandardFirmataPlus.ino file from GitHub and place it in a StandardFirmataPlus directory inside your Arduino directory. Open the StandardFirmataPlus.ino file and click the Upload button. This will compile the bits and upload it to your Arduino. You can close the Arduino IDE once it’s done uploading. You are all set now for some JavaScript!

Note: If it fails uploading the code try to install the “Firmata” library the same way you installed the “Servo” library.

“Hello World” of hardware



The equivalent of Hello World in hardware is probably making LEDs blink. The nice thing is that the Arduinos typically already have an LED on the board that we can use to see if everything works without having to wire up anything. So let’s get started!

Create a new Node.js project anywhere on your computer (like your home directory) by running the following lines in your command line: