Getting Started With Arduino Using Node.js

Arduino is one single board on which you can do programing and control various devices, you can give output to any device using Arduino and also take some input from the device (sensor).

Node.js is a powerful JavaScript-based framework/platform built on Google Chrome's JavaScript Engine. Here we will use it with Arduino for IOT based projects.

For getting started with Node JS and Arduino follow the below steps.

  1. Download Node JS from here. And Install it.
  2. Download Git from here. And install it.
  3. Download Arduino IDE from here. And install it.
  4. Now we have requirement of following component,

    • Arduino Uno Board with USB cable
    • Bread Board
    • Jumper cable
    • LED

  5. Now we check if Node js is installed successfully or not.

    So we make one folder in “D:\” drive, give it the name “Arduino” and right click in this folder and go to “Git Bash here” as shown in below image.

    Arduino

  6. It will open one window as shown in below image,

    window

  7. Now type “node” in open windows as shown in below image,

    node

  8. Now type the below command and hit enter key.

    console.log("Hello World!");


    command

  9. Here you can show the above image. It will give output as “Hello World!” Now you can say that your Node Js is installed successfully.

  10. Now for Arduino communication with other devices we use Node JS's “johnny-five” library. So first download it in node JS. So type “npm install johnny-five” command in GIT windows.

    command

  11. On successful installation of “johnny-five” it displays output as shown in the below image.

    output

  12. Now we use the Pin 13 of Arduino for our LED blink program. So connect Minus (-) terminal of Led to ground of Arduino and plus (+) terminal of LED to Pin 13 of Arduino. As show in below image.

    Pin 13
    Pin 13

  13. Now connect the Arduino board to the computer using USB cable so it gives power to it.

  14. Now open the Arduino IDE. as shown in the below image,

    Arduion IDE

  15. Now go to the File>>Examples>>FIRData>>StandardFirData. It will open a new window.

    window

  16. Now upload this program to Arduino by clicking on “upload button” as shown in the figure. If it uploads successfully without any error then go to the next step.

    next

  17. Now make a new JS file give the name “LEDBlink.js”, save it to “D:\Arduino” and write the following code to it. And save it.
    1. var Jfive = require('johnny-five');  
    2. var nboard = new five.Board();  
    3. nboard.on('ready'function() {  
    4.     var led = new jfive.Led(13); // pin 13  
    5.     led.blink(1000); // 1 second interval  
    6. });  
    Herethe above code says that it will connect pin 13 of Arduino to Led and blink it every 1 second gap.

    code

  18. Now we run this “LEDBlink.js” file from our GIT window. So run “node LEDBlink.js” as shown in the below image.

    window

    window

  19. Now it will start Blinking LED every 1 second as shown in the below image.

    output

Thanks for reading my article if you have any query regarding this you can ask it in the comment section.

Up Next
    Ebook Download
    View all
    Learn
    View all