Firmata is one of the coolest ways to control an Arduino. When we start off, we’re taught to use the Arduino IDE, programming instructions into our Arduino using the C programming language.

Each change means uploading fresh C code onto the device and resetting the device. With Firmata; we can upload a standardised C library onto the device and then we’re able to send new instructions without further uploads. We can dynamically reprogram our devices in real-time!

In this post, we’re going to look at how to use Firmata to dynamically program an Arduino.

This is not meant to be a post about how to use Node, or to program JavaScript applications. I will assume that you know how to do those things.

You can leave a comment here, or tweet me (@assertchris) if you spot errors or have some tips about these components.

Installing Firmata

Firmata is an NPM module, so we’ll need to add it to package.json:

"dependencies" : {

"firmata" : "^0.3.3"

}

This is from package.json

To install it, we need to run:

$ npm install



> serialport@1.4.2 install ...

> node-pre-gyp install --fallback-to-build

This command will check the dependencies, in package.json, and download them recursively.

Uploading Firmata

Before we can start using JavaScript to program our Arduino, we need to upload the Firmata library on the Arduino. Download the official IDE (for your operating system) at http://arduino.cc/en/Main/Software. Install and launch it.

The version I’ve just downloaded is 1.0.5 (OSX). The instructions that follow will be based on the interface I see, and may vary with your version/operating system. Please let me know if you have a different experience, so that I can adjust the post accordingly.

Go to File → Examples → Firmata → StandardFirmata. Upload this sketch to your Arduino and you should be good to go!

Using Firmata

The first step in programming an Arduino, using Firmata, is connecting to the board:

var firmata = require("firmata");



var port = "/dev/tty.usbmodem1411";



var board = new firmata.Board(port, function () {

console.log("connected to arduino");

});

Obtaining the port can be tricky at first, but you soon learn to recognise the names your computer will assign to your Arduino. You can find the port (e.g. /dev/tty.usbmodem1411) with the following steps:

Disconnect the Arduino $ cd /dev && ls Connect the Arduino $ ls

The new items are probably the Arduino, and they usually start with either tty or cu. The one you want is tty, so use that device as the port.

These instructions are specific to my Macbook. They should also apply (generally) to Linux operating systems, but your mileage may vary. For windows, you’re going to need to use COM ports. I’ll not discuss this here, but you can find more on this at http://arduino.cc/en/guide/windows.

Pin Modes

Then we connect components to the pins on our Arduino, we expect them to perform a certain function. Perhaps we want to light up an LED, or read from a photocell, or turn a servo. In all of these cases; we need to set the mode of the target pins to the function we want to perform. The most common modes are:

INPUT

OUTPUT

ANALOG

PWM

SERVO

If we want to light up that LED, we need to connect it to pin 13 and set the pin mode:

board.pinMode(13, board.MODES.OUTPUT);

Or if we want to turn that servo, we need to connect it to pin 10 and set the pin mode:

board.pinMode(10, board.MODES.SERVO);

You may be wondering why I didn’t show how to set the pin mode for a photocell. Photocells are analog sensors, and you don’t need to set the mode for them. You can just read directly from them…

Reads/Writes

Once the pin mode is set, we can read and write from pins. This could be for reading from a push-button switch:

board.digitalRead(4, function (value) {

console.log("button: " + value);

});

Or for reading from a photocell:

board.analogRead(14, function (value) {

console.log("photocell: " + value);

});

Lighting up LEDs can be done in one of two ways. The first is simple on/off:

board.digitalWrite(13, board.HIGH);



setTimeout(function () {

board.digitalWrite(13, board.LOW);

}, 3000);

This will turn the LED on and (after three seconds) off again. The second way to control the brightness of an LED is using PWM and analog writes:

var intensity = 0;



setInterval(function () {

board.analogWrite(11, intensity);

intensity++;

}, 1000 / 60);

PWM is short for Pulse Width Modulation, which is a technique uses rapid, on/off cycles to simulate incremental values. The longer the gaps between flashes, the dimmer an LED would appear.

Lastly, we can make servos turn by using the following method:

board.analogWrite(5, 0);



setTimeout(function () {

board.analogWrite(5, 180);

}, 3000);