We have seen varied samples of Blinking AN light-emitting diode mistreatment. But they are totally different Microcontrollers and different programming languages. However, today, we’ll use an extremely widespread JavaScript framework. Node.js to regulate the light-emitting diode connected to Arduino Uno.
We’ll conjointly use the Node.js interface to regulate the brightness of the light-emitting diode from an Arduino. By few tweaks and mistreatment ESP8266.

What is Node.js?

Node JS is a runtime environment or a platform that can read write files and do certain operations on a computer. The programming language used in Node is JavaScript but Node JS as a whole is more than a language. Because it’s an all-around tool.

It is one common language that can be used as a backend and frontend. and can be used almost anywhere except in the browser. It doesn’t exist in the browser but for good reason… because it’s a JavaScript runtime, which already exists in browsers.

During this tutorial, we’ll follow a similar approach. The light-emitting diode is controlled mistreatment 2 strategies such as:

  • Easy light-emitting diode blink by writing a JavaScript code within the Node.js framework.
  • light-emitting diode Brightness management mistreatment Node.js framework and an internet interface from any browser.

Components needed

Hardware:
  1. Arduino UNO Board (arduino uno pins)
  2. Led
  3. Resistor
Software:
  1. Arduino IDE:
    The Arduino Integrated Development Environment is a cross-platform application that is written in functions from C and C++. It is used to write and upload programs to Arduino compatible boards, but also, with the help of 3rd party cores, other vendor development boards.  For uploading sketch to Arduino UNO Board. You can program an Arduino using python. But programming an Arduino via. the Arduino IDE is way simpler than programming the same by python.
  2. Firmata:
    It’s a protocol for human action with completely different micro-controllers. Firmata Library comes with Arduino IDE. Therefore no have to be compelled to transfer from anyplace. The Firmata protocol could theoretically be implemented for any microcontroller platform. Currently however, the most complete implementation is for Arduino. If your proposal uses any of the available non-SysEx midi messages. The number of bytes in the message must correspond to the number of bytes in the midi message. The meaning however does not need to be the same. However if the midi message uses channels (such as Note Off (0x80)) then the Firmata message must also use channels since a midi parser may expect this. 
  3. Johnny-Five:
    Johnny-Five is that the JavaScript-based mostly AI and IoT platform. wont to write codes in JavaScript and wont to build a bridge. It is tested with a range of Arduino-compatible Boards like Arduino UNO, NANO, Pro mini. The installation guide is explained later-on during this tutorial.
Circuit Diagram
Node.js with Arduino: How to Controlling Brightness of LED through Web Interface | arduino uno pins

Setting Up Node.js Framework

Before beginning the cryptography and development and you must download the Node.js.
For downloading the Node.js atmosphere simply follow easy steps.

1:- Transfer the .exe file of Node.js from its official website.

2:- Run the .exe and follow the given directions within the installer.

Setting_Up_Node.js_Framework

3:- Restart your laptop because it is usually recommended within the Node.js.
Document and conjointly to use all the options of Node.js

4:- To envision if the node has been put in, open electronic communication and sort Node –v

To_heck_Nodejs_is_nsatalled

5:- The Node.js version are displayed indicating the Node.js is put in.

Installing Johnny-Five Library

Now when downloading the Node.js, produce a brand new Folder named “LED_Control” in Documents to stay all the project files in one place. Each project file is unbroken within the “LED_Control” folder. Currently, transfer the Johnny-Five Library within the “LED_Control” (arduino uno pins) folder. To transfer, follow the steps below:

1:- Open electronic communication

2:- Move to “LED_Control” Folder by following ‘cd’ command.

3:- Run the Command ‘npm install johnny-five’

4:- This may install all dependencies of Johnny-Five.

5:- Conjointly install necessary libraries employed in this tutorial for the dominant brightness of light-emitting diode. There area unit 3 libraries used here:

express: communications protocol server wrapper
socket.io: WebSockets library
serialport: interface wrapper

6:- Run the below commands one by one to put in these 3 libraries.

type npm install()
express
socket.io
serialport
Installing_Johnny_Five-Express_Library
Installing_Johnny_Five_SocketIO_Library
Installing_Johnny_Five_Serial_Port_Library

As told earlier the project is split in 2 parts:

Firstly, Arduino Blinking light-emitting diode with Node.js
Secondly, dominant light-emitting diode Brightness from internet interface mistreatment Arduino and Node.js.
Blinking light-emitting diode with Arduino and Node.js

To blink light-emitting diode, the Arduino must come upon to speak with the laptop.

Setting up Arduino UNO for Blinking LED:

In order to form Arduino UNO settle for a command from the portable computer to regulate light-emitting diode. To load the Firmata computer code
So simply follow these easy steps:

  • Connect the Arduino UNO mistreatment USB cable
  • Open Arduino IDE choose|and choose} Arduino UNO board from Tools.
  • Choose the various COM port of connected Arduino UNO
  • Find the Firmata SketchUsing Menu -> File -> Examples -> Firmata -> StandardFirmata.
  • Transfer the “StandardFirmata” sketch by about to File -> transfer.

This will load the Firmata Sketch onto Arduino UNO and currently because the Arduino UNO is prepared to simply accept any command from the laptop.

Writing Node.js program for Blinking LED:

To write a Node.js program open any text editor (Notepad, Notepad++, etc.) and paste the ‘blink_led’ code. It is hooked up at the top of this tutorial and reserve it with an extension of ‘.js’ i.e. (blink_led.js). within the “LED_Control” folder created before. We’ll discuss the necessary steps in a blink_led.js code file.

Initially outline the Pin of microcontroller wherever the light-emitting diode is connected. During this example, the light-emitting diode is connected to Pin five of Arduino UNO. The ‘var’ in Node.js represents a variable declaration.

var led_pin=5;

The modules in Node.js area unit libraries. But the operate ‘require()’ can access the module.

var johnny_five=require("johnny-five"); 
var arduino_board=new johnny_five.Board(); 

The console.log statement is comparable to print statement and it’ll print message. Therefore the light-emitting diode pin is ready to output mode and therefore the outlined delay is given to blinking light-emitting diode.

console.log("LED has Started Blinking!");  
var led = new johnny_five.Led(led_pin);  
led.blink(100); 

Now to run the program follow steps below:

  • Open electronic communication
  • Find “LED_Control” folder by following ‘cd’ command
  • Run ‘Node led_blink.js’ command. If with success dead it’ll show “LED has Started Blinking!” as shown within the image below.
Writing_Nodejs_Program_for_Blinking_LED
  • The light-emitting diode can begin blinking within the Arduino UNO Pin five.
  • And this finishes the primary a part of our tutorial i.e. Blinking light-emitting diode with Node.js
Controlling light-emitting diode Brightness mistreatment Arduino and Node.js Webserver

Similar to Blinking AN light-emitting diode with Node.js. this section will have some components i.e. putting in Arduino UNO. Putting in internet Interface and Writing a Node.js program.

Setting up Arduino UNO to regulate Brightness of LED:

To set up the Arduino UNO, merely transfer the sketch “arduino_control.ino” into Arduino UNO board and that’s it. This may come upon the Arduino Board. Because you’ll transfer the whole code with hypertext markup language files from here. The “arduino_control.ino” code has the following necessary steps concerned.

Initially the information measure is ready at 9600.

Serial.begin(9600);

The interface always appearance for incoming computer memory unit and therefore the computer memory unit is written to Pin five that may be a PWM Pin.

while(!Serial.available());
analogWrite(5, Serial.read());

That’s it. this may set the Arduino to latch the computer memory unit to PWM pin and can amendment the brightness of light-emitting diode.

Setting up internet Interface:

To control the brightness of light-emitting diode mistreatment internet interface, one tiny piece of hypertext markup language code is written to own AN interface within the Browser. (arduino uno pins) to own interface follow easy steps below:

produce a brand new folder named “public” within the “LED_Control” folder created before.

Setting_up_Web_Interface_for_Nodejs_and_Arduino

Now transfer the “index.html” and “style.css” files and move each file within the “public” folder created in beginning higher than. Files are often downloaded from here.

Designing_the_Web_Interface_for_Controlling_LED

This will produce a slider on a webpage to regulate the brightness of light-emitting diode mistreatment Node.js and Arduino.

You May Like: How to make a website in 2019?

Web_Interface_UI

Controlling the AN light-emitting diode from the webpage is attention-grabbing.
Check all the web server connected comes here.

Writing Node.js program to regulate Brightness of LED(arduino uno pins)

Next step would be writing the “brightness_control.js” sketch mistreatment Node.js and corporal punishment the code. The execution of the code is a bit just like the Blinking AN light-emitting diode with Node.js.

To write a Node.js program open any text editor (Notepad and Notepad++) and paste the ”brightness_contro” code. It is hooked at the top of this tutorial and reserve it with an extension of ‘.js’ i.e. (brightness_control.js) within the “LED_Control” folder created before therefore you’ll conjointly transfer the code from here.

Similar to Blink light-emitting diode Node.js program, this section will use modules (library). Embody the ‘express’, ‘http’ and ‘serial port’ module.

var express = require('express');
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server);
var SerialPort = require("serialport")//.SerialPort

Now set the COM port and baud rate. Note that in windows, it’ll invariably be COM with an extension of range (COM6, COM4 and COM24). therefore set below PRN when ‘/’. conjointly set baud rate.

var serialPort = new SerialPort("/COM4", { baudRate: 9600 });

Start to pay attention the server at port 8080.

server.listen(8080);

Set the brightness at zero ab initio. Then latch the brightness information to IO with the sockets module, which may be a WebSocket module. The information can receive by internet Interface mistreatment socket protocol.

io.sockets.on('connection', function (socket) {
socket.on('led', function (data) {
brightness = data.value;
var buf = new Buffer(1);
buf.writeUInt8(brightness, 0);
serialPort.write(buf);

Now emit the light-emitting diode brightness price got from socket to light-emitting diode pin.

io.sockets.emit('led', {value: brightness});   
});
socket.emit('led', {value: brightness});
});

Now simply to rectify, add a print statement at the top. Because Console.log maybe a print statement in Node.js.

console.log("Web Server Started go to 'http://localhost:8080' in your Browser.");
Now to run the program by following the steps below:
  • Open command prompt
  • Find “LED_Control” folder by following ‘cd’ command
  • Run ‘Node brightness_control.js’ command. If with success dead it’ll show “Web Server Started to move to “http://localhost:8080″ in your Browser.” just under the command.
arduino uno
  • Currently move to your browser and sort “localhost:8080” within the URL.
  • To alter the brightness simply move the slider from 0-255 values.

The complete code and dealing video of each section is hooked up below. But if you have got any doubt or suggestion then you’ll reach America through forum or comment below. All the code files may be downloaded from here.

CODE

1 – Node.js Code for LED Blinking: (arduino uno pins)

var led_pin=5;
johnny_five=require(“johnny-five”); 
arduino_board=new johnny_five.Board(); 
arduino_board.on(“ready”, function() {  
   console.log(“LED has Started Blinking!”);  
   var led = new johnny_five.Led(led_pin);  
   led.blink(100);  
});

2 -a- Arduino code for controlling Brightness of LED: (arduino uno pins)

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  while(!Serial.available());  //wait until a byte received
  analogWrite(5, Serial.read());//output received byte
}

2 -b – Node.js Code for Controlling Brightness of LED (Arduino Uno pins):

var express = require(‘express’);
app = express();
server = require(‘http’).createServer(app);
io = require(‘socket.io’).listen(server);
 
var SerialPort = require(“serialport”)//.SerialPort
var serialPort = new SerialPort(“/COM4”, { baudRate: 9600 });
 
server.listen(8080);
app.use(express.static(‘public’));             
 
var brightness = 0;
 
io.sockets.on(‘connection’, function (socket) {
        socket.on(‘led’, function (data) {
                brightness = data.value;
               
                var buf = new Buffer(1);
                buf.writeUInt8(brightness, 0);
                serialPort.write(buf);
               
                io.sockets.emit(‘led’, {value: brightness});   
        });
       
        socket.emit(‘led’, {value: brightness});
});
 
console.log(“Web Server Started go to ‘http://localhost:8080‘ in your Browser.”);