Channels ▼

Embedded Systems

Building an IoT Project with Intel Galileo and Node.js

Writing JavaScript Code

Let's walk through a few steps to create a new project in the IDE, start writing JavaScript code, and use the Node.js modules to interact with the board features:

  • Click on the XDK PROJECTS tab, located at the upper-left corner of the window, and then click on START A NEW PROJECT.
  • Click Start with a Sample or Template below Internet of Things (IoT) with Node.js Projects.
  • Click Blank Template and then Use This Template.
  • Select the folder in which you want to save the project and enter the name (BatteryVoltage). Finally, click Create. The IDE will display a dialog box offering you documentation about it. After you click No, Thanks, the IDE will display the files for the project and the editor for the main.js file.

The following lines show the new code for the main.js file that uses the open source MRAA library to control a GPIO pin and an analog IO port.

// Require the MRAA library
var mraa = require('mraa');
// Print the MRAA library version to the IDE console
console.log('MRAA Library Version: ' + mraa.getVersion());
// The Galileo Gen 2 onboard led is mapped to pin #13
var onboardLed = new mraa.Gpio(13);
// Set the GPIO direction to output (I want to turn on and off the LED)
// Hold the LED state
var ledState = false
// Analog input pin #1 (A1)
var analogPin1 = new mraa.Aio(1);
// Call the periodict activity function

function periodicActivity()
    // Invert the LED state
    ledState = !ledState;
    // Turn on the LED by writing a '1' (high) or
    // Turn off the LED by writing a '0' based on ledState
    // Read the value from the analog pin (up to 4096)
    var analogValue =;
    // Convert the retrieved value to the appropriate voltage value
    var measuredVoltage = analogValue / 4096 * 5;
    // Write the measured voltage to the IDE console
    console.log("Measured voltage (in Volts): " + measuredVoltage);
    // Call periodicActivity again after 1000 milliseconds (1 second)

Notice that the IDE displays some JSHint problems that are wrong. For example, JSHint doesn't recognize require. I suggest just paying attention to problems related to simple syntax errors, such as missing semicolons.

The code is easy to understand. The first line requires the MRAA library and the entry point is the mraa variable.

var mraa = require('mraa');

Then, the code uses console.log to print the MRAA library version installed on the board. The IDE provides additional debugging features, but the ability to easily write to the console window is a very useful feature.

The mraa.Gpio function receives the pin number that you want to access for general purpose I/O. In this case, I just want to turn on and turn off the onboard LED labeled L located at the right-hand side of the USB connector. This LED is mapped to pin 13 and the general purpose I/O direction is set to output because I want to write high (1) and low (0) values to turn on and turn off the LED. The code will be able to use onboardLed to change the state of the LED.

var onboardLed = new mraa.Gpio(13);

The mraa.Aio function receives the pin number that you want to access to analog input. I want to take advantage of the analog-to-digital converter to measure the voltage of a rechargeable AAA battery. The code will be able to use analogPin1 to read a voltage.

var analogPin1 = new mraa.Aio(1);

Finally, the code calls the periodicActivity function for the first time.


The periodicActivity function inverts the LED state each time it is called. It calls onboardLed.write with the new desired value as an argument. As a result, the onboard LED will turn on or off and you will be able to notice that the code is running on the board by checking the onboard LED blinking.

ledState = !ledState;

Next, the call to returns the value read from the analog input pin A1. The analog-to-digital converter provides 12 bits of resolution, that is, 212 = 4096 different values. By default, the analog input measures from ground to 5 Volts, which is equal to 4096 units or 0.00122 (1.22 mV) per unit. Thus, the code has to convert the retrieved value to the appropriate voltage value before displaying the measured voltage on the console.

var analogValue =;
var measuredVoltage = analogValue / 4096 * 5;
console.log("Measured voltage: " + measuredVoltage);

Finally, the code calls the setTimeout function to call the periodActivity function again after 1000 milliseconds (1 second).

You can connect an AA/AAA rechargeable battery that has a proper voltage of 1.25 V to the analog input and the code will measure the voltage. However, you must be extremely careful because incorrect wiring can damage the board. Connect a black cable from the battery's flat side (-) to the GND pin located at the lower-right corner of the board (oriented so that you can read the MAC address and the labels included in the board as in Figure 1). Connect a red cable from the battery's nipple (+) to the A1 pin located at the lower right corner of the board.

After you enter the code in the IDE editor, select File | Save and then click the Stop button to stop any project that might be running on the board. Then, click Install/Build and the IDE will fetch the Node.js modules specified in the package.json file and install them on the board. The previously specified settings make the IDE clean the Node.js modules before building and running npm install directly on the board for the dependencies.

You will see the following lines in the IDE console window:

Intel XDK - Message Received: install
| Intel (R) IoT - NPM Install - (may take several minutes)
npm WARN package.json [email protected] No description
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No README data

The Node.js code that will run in the board is located in the /node_app_slot directory. After the IDE installs the project on the board, you will have main.js and package.json in /node_app_slot.

Click on Run and the IDE will make the board run the project and you will see the strings that the board sends to the console using the console.log method in the IDE console window (see Figure 3). The following lines show an example of the console output:

Intel XDK - Message Received: run
=> Stopping App <=
MRAA Library Version: v0.4.5
Measured voltage (in Volts): 1.220703125

IoT IDE message
Figure 3: The IDE control located at the bottom of the main window.

In the first run I made with a rechargeable battery, the value read is 1000 and 1000 / 4096 * 5 = 1.220703125 Volts. With just a few lines of code, it is possible to easily measure a rechargeable battery voltage with precision.

In this case, I just used the MRAA library and I didn't take advantage of Node.js and many of its modules that allow other applications to interface with the board. There are myriad libraries that you can use in your code to interact with the board features. To name a few:

  • I2c: Uses sysfs and simplifies working with the I2C bus.
  • Iot-io: An Arduino like API to perform IO operations.
  • Johnny-five: An I/O library.
  • Onoff: An I/O library that uses sysfs.
  • Upm: A high-level library for sensors and actuators that works with MRAA.


This example should provide you a baseline to use for working with Intel XDK IoT Edition, JavaScript, Node.js, and an Intel Galileo Gen 2 board. In this article, I focused on the initial setup and configuration of both the board and the IDE. In the next article, I'll explain different options that take advantage of Node.js to easily generate interfaces that allow the board to communicate with other applications. In fact, the Internet of Things is all about providing interfaces to any device, and Node.js is extremely useful for generating just such interfaces with only a few lines of nonblocking code.

Gastón Hillar is a senior contributing editor at Dr. Dobb's.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.