NodeJS With Arduino

Shubham Verma
3 min readMay 2, 2018

--

Connect arduino with nodejs and do whatever you want through JavaScript code.

Connectivity with nodejs and arduino

Let’s start

Step 1: You need following things:

Step 2: Upload Firmata in your Arduino IDE.

  • Open your arduino kit
  • Goto File > Examples > Firmata > StandardFirmata
  • Click on upload button on your arduino IDE. This library will upload to your arduino kit.

Step 3: Work on nodejs

  • Open your laptop ( Assuming that you have already installed nodejs )
  • Create a directory in any location
  • Navigate to location of above directory and run below command-
 npm init

( The npm init will create a file, package.json which will have all the information about this project )

Now enter the information accordinly, Saw below line for help:

“name”: “arduino_with_nodejs”,
“version”: “1.0.0”,
“description”: “This is the demo to connect nodejs to arduino”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“keywords”: [
“ShubhamArduino”
],
“author”: “Shubham Verma”,
“license”: “ISC”,

If you are getting any error then copy the below written code and replace your whole code which is written in your package.json.

{"name": "arduino_with_nodejs","version": "1.0.0","description": "This is the demo to connect nodejs to arduino","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": ["ShubhamArduino"],"author": "Shubham Verma","license": "ISC","links": {"johnny-five": "https://www.npmjs.com/package/johnny-five"},"dependencies": {"johnny-five": "^0.14.3"}}

Step 4: Install johnny-five module to interact with arduino:

Run command:

npm install johnny-five --save

The above command will create a folder, node_modules and keep all the required library inside this folder.

Step 5: Create a new file on same directory with name :

index.js

Step 6: Now copy and pastey below code into your newly created index.js file:

var five = require('johnny-five');
var board = new five.Board();
board.on('ready', function () {
var led = new five.Led(8);
led.blink(1000);
});

Step 7: Connect your LED object to arduino like in images:

Step 8: Run your node app by using following command:

node index.js

Congratulations… Now your LED will be blinking after every 1 second.

Now you can do whatever to you JavaScript code with this arduino. If you want to stop the node app, press “CTRL+C” and start node app “node index.js”

--

--

Shubham Verma

A full-stack developer, In my free time, I write blogs and play guitar. I am both driven and self-motivated.