Control Arduino using Node JS

As the title says, this tutorial will teach you how to control your Arduino by Rpi, Computer or Laptop. I am not a big fan of the Windows so I will use Ubuntu (Linux). Basically, Rpi also uses Linux so there will be the same steps for Rpi as well. For windows though, many of the steps are same but the terminal commands are different.

Requirements –

  1. Arduino (I am using Arduino UNO)
  2. NodeJS installed in the system
  3. Arduino IDE

Environment Setup

For setting it up for development follow the following steps.

Setting up Arduino

To make the Arduino work with node js, you have to program it. So for doing so, open Arduino IDE. go to File->Examples->Firmata->StandardFirmata. This will open a program. Compile it and run it. That all. you are all set to control Arduino through nodeJS.

Create a development directory.

For creating a development directory, you have to go with the nodeJS programming ethics. If you are new to nodeJS than don’t worry we will describe easy steps for you.

  • Make sure you have installed nodeJS in your system. You can test that by typing the following command in your terminal.
    $ node -v

    If not installed then you can install it by clicking on this link.

  • Create a repository and package.json file by the following commands.
    $ mkdir <foldername>
    $ cd <foldername>
    $ npm init

    Note: For Windows, just create a folder and open your cmd prompt inside that folder and type “npm init”.

  • Third command npm init will go over with some steps to create a JSON file. When these steps are finished then it will create a package.json file in your repository. This file will hold all your local module information.
  • Now save johnny-five in your node module.
    $ npm install johnny-five --save
  • Type “ls -alt”. You should see something similar like here. Windows user can type “dir” command.
    bhoopesh@jarvase:~/workspace/node-ard$ ls -alt
    total 84
    drwxrwxr-x   3 bhoopesh bhoopesh  4096 Apr 23 20:33 .
    -rw-rw-r--   1 bhoopesh bhoopesh 67335 Apr 23 20:33 package-lock.json
    -rw-rw-r--   1 bhoopesh bhoopesh   252 Apr 23 20:33 package.json
    drwxrwxr-x 168 bhoopesh bhoopesh  4096 Apr 23 20:33 node_modules
    -rw-rw-r--   1 bhoopesh bhoopesh     0 Apr 23 20:32 index.js
    drwxrwxr-x   4 bhoopesh bhoopesh  4096 Apr 23 20:32 ..
    bhoopesh@jarvase:~/workspace/node-ard$

Code to Control Arduino using Nodejs

Create any file with the .js extension (e.g. index.js) using any text editor and paste the following code.

var Arduino = require("johnny-five");                                        //define module you will use
var uno = new Arduino.Board();                                               //define board you will use

//create a listener for ready state of the board 
uno.on("ready", function() {
             var led = new Arduino.Led(13);                                 //define pin of the arduino
             led.blink(1000);                                                           //call blink function
});

So The code is based on very basic programming of nodeJS. Generally, in every node js program, you have to follow these steps:

  1. Require module
  2. Define variable, functions and other parameters of the required module.
  3. Use of those parameters.

So let’s go over with steps in code.

  1. Required node module – “johnny-five”.
  2. Defined board uno in a variable
  3. Now using other function of this variable like ready state, defining led and blinking function.

Execution of the Program

So now you good to go to see results. You can run this code by simply running this command.

$ node <filename>.js

This command will start to blink led in the interval of 1000 second according to the code. If you want to change the interval, change it in led.blink(time_interval) in the code

The terminal will look like this

bhoopesh@jarvase:~/node-ard$ node index.js 
1554967119816 Available /dev/ttyACM0  
1554967119827 Connected /dev/ttyACM0  
1554967123623 Repl Initialized  
>>

Internally, it is communicating Arduino via serial communication. That’s how we can you utilize the power of nodeJS with Arduino.

Conclusion

I guess thats it for this article. If you have any question you can ask directly in the comment section down below. And it will be a pleasure to get your feedback. Thanks for reading. 

1 thought on “Control Arduino using Node JS”

Leave a Comment

Your email address will not be published. Required fields are marked *

Share This