LED blink with NodeJS and Arduino

LED blink with NodeJS and Arduino

In this tutorial, I will show you how to blink led using arduino and NodeJS. This post will be part of the tutorial series on Javascript in Embedded electronics. You can check up Sound Sensor with Arduino Mega, LED including Buzzer.

With the tremedous rise of javascript and nodejs, it wasn’t a surprise seeing it heading into the world of electronics and IOT.

Lets make sure you have got all we need to accomplish this task, you can  get them from our store:

  • Arduino board (I am making use of Arduino Mega but Uno will still serve)
  • Arduino IDE (You can download it here if you have not)
  • LED (I am making use of the LED connected to the Arduino’s pin 13)
  • NodeJS (You can download yours from here)
  • Johnny-five (a JavaScript Robotics programming framework. Johnny-Five is open source and has growing developer communities. It can be install using the npm package manager)

 

Setting up the Arduino Board

  • Open your Arduino IDE, click on file ->  examples -> firmata -> StandardFirmata, then compile and upload.
  • Close the IDE except if you are using it for another thing. The board is ready.

 

Setting up the JavaScript file

  • From the folder of your choice, create a new folder and cd into the folder and install johnny-five framework:
npm install johnny-five
  • create a javascript file named blink_led.js (you can named yours what you want). Paste this code inside the file:
var fiveLib = require("johnny-five");
var megaboard = new fiveLib.Board();

//set delay to 1000ms
var delay = 500;

//led pin number
var pinNumber = 13;

megaboard.on('ready', function() {
    var led = new fiveLib.Led(this.pinNumber);

    //blink led for 1000ms
    led.blink(this.delay);
});

 

Run the code with this command (i.e. from your project folder):

node blink_led.js

 

You should see your LED on pin 13 blinking and this on your console:

 

Feel free to add your comments above this post and stay in-touch for the next post in this series.

Leave a Reply

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