Introduction: JavaScript for IoT: Blinking LED on Raspberry Pi With Node.js

About: Software engineer and open source enthusiast

In this tutorial you will learn the exact steps how to control a LED connected to a GPIO pin of Raspberry Pi using JavaScript and Node.js. This is actually the easiest thing to do with a Raspberry Pi and a LED. The provided example is open source and it is available at GitHub under the MIT licence. You can use the algorithm to create alarms on do it yourself (DIY) devices with Raspberry Pi.

This tutorial is appropriate for developers, makers and hobbyists. No knowledge of JavaScript is needed to run the example. No prior course of algorithms is required.

JavaScript appeared in 1995. For long time it was a programming language used only by front-end web developers. In 2009 Node.js, an amazing new technology, appeared and converted JavaScript into a general-purpose programming language. Nowadays we can use JavaScript even on embedded devices. Is JavaScript the best language for controlling GPIO pins? In general, definitely not. If you are looking for high performance C should be the language of your choice. But JavaScript is easy to use, easy to learn and a lot of developers already know it. It is popular among web developers so it a good option for Internet of Things.

Have a look at the video version of this Instructable. If the embedded video does not appear on your mobile device, follow the alternative link. If you like this tutorial please follow me here in Instructables and subscribe to my YouTube channel.

Step 1: Getting Ready

For this tutorial you need the following hardware components:

  • Raspberry Pi (any model or version)
  • Breadboard
  • LED
  • Resistor
  • Male to female jumper wires (x2)
  • microSD card with Raspian GNU/Linux distribution
  • Power supply

Step 2: Wiring

One of the jumper wires should be connected to ground which is on pin 6 of Raspberry Pi. The other wire goes to pin 7. The resistor should be on the breadboard, between the second wire and the LED (as shown in the photos).

Step 3: Setting Up Development Environment

Make sure that Raspbian GNU/Linux distribution is running on your Raspberry Pi. Log in though SSH or just open a terminal application on the Pi itself. After that execute the following command to install Node.js, npm (Node Package Manager) and git:

sudo apt-get install -y nodejs npm git

Step 4: Installing the Example

Get the open source example and install its dependencies using npm:

git clone https://github.com/leon-anavi/rpi-nodejs-examples.git
cd rpi-nodejs-examples
npm install

Step 5: Blinking LED

Execute the following command with root permission and the LED will start blinking on every second:

sudo nodejs led/led.js

Step 6: How Does It Work?

The source code is available at GitHub and in the attached zip archive. JavaScript file led.js is executed by Node.js and its dependencies are installed by npm depending on the configuration from package.json. This examples relies on package wiring-pi which provide JavaScript binding to the popular open source C library wiringpi.

Our JavaScript sets a timer which changes a pin from high to low on every second. The tricky part is the pin numbering scheme. Have a look at the documentation of wiringpi C library for details.