We have seen varied samples of Blinking AN light-emitting diode mistreatment totally different Microcontrollers and different programming languages. Today, we’ll use an awfully widespread JavaScript framework Node.js to regulate the light-emitting diode connected to Arduino Uno. we’ll conjointly use Node.js interface to regulate the brightness of light-emitting diode from a webpage mistreatment Arduino. By few tweaks and mistreatment ESP8266, this could be born-again into an IoT project wherever the light-emitting diode are often controlled from anyplace (Learn Arduino Uno pins structure).

What is Node.js?

The Node.js may be a wide used JavaScript-based framework designed on Google Chrome’s JavaScript V8 Engine and applied in developing I/O intensive internet applications like single-page applications, video streaming sites, etc.

During this tutorial, we’ll follow the similar approach i.e. 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: The Arduino UNO can act as a web server and hypertext markup language website are hosted on a computer or portable computer.

Components needed

Hardware:

  1. Arduino UNO Board (arduino uno pins)
  2. Led
  3. Resistor

Software:

  1. Arduino IDE: For uploading sketch to Arduino UNO Board.
  2. Firmata: it’s a protocol for human action with completely different micro-controllers from computer code on a laptop, smartphone, etc. The Firmata computer code is often loaded in any micro-controller board (like Arduino, Teensy) and is in a position to speak to any portable computer, computer or smartphone. Firmata Library comes with Arduino IDE, therefore no have to be compelled to transfer from anyplace.
  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 between the Arduino Boards and laptop. Johnny-Five is tested with a range of Arduino-compatible Boards like Arduino UNO, NANO, Promini, etc. during this tutorial, the Johnny-Five library must be downloaded so as to use all its options. The installation guide is explained later-on during this tutorial.

Circuit Diagram
The circuit diagram is extremely basic, (learn all arduino uno pins out) we tend to simply have to be compelled to connect AN light-emitting diode with Arduino.

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, the Node.js must be downloaded and came upon. For downloading the Node.js atmosphere simply follow easy steps.

Step 1:- transfer the .exe file of Node.js from its official web site.

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

Setting_Up_Node.js_Framework

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

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

To_heck_Nodejs_is_nsatalled

Step 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:

Step 1:- Open electronic communication

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

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

Step 4:- this may install all dependencies of Johnny-Five.

Step 5:- conjointly install necessary libraries employed in this tutorial for 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

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

npm install express
npm install socket.io
npm install 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, the Firmata computer code must be loaded into the Arduino UNO. To load the Firmata computer code, simply follow these easy steps:

  • Connect the Arduino UNO mistreatment USB cable
  • Open Arduino IDE choose|and choose} Arduino UNO board (If mistreatment alternative board then select various one) 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, 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 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 necessary steps in a blink_led.js code file.

Initially outline the Pin of microcontroller wherever 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 johnny-five module has to be enclosed and therefore the board has to be selected. The modules in Node.js area unit libraries. 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. and 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 Web server

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. 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 invariably 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 AN light-emitting diode from the webpage is attention-grabbing to be told and it will through with alternative microcontroller by making a web server and host the webpage on the webserver. 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, Notepad++, etc.) ANd paste the ”brightness_contro” code hooked up 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. 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, COM24, etc.), 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 sockets module, that 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. Console.log may be 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 are hooked up below. conjointly 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

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

var led_pin=5;
var johnny_five=require(“johnny-five”); 
var 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);  
});

Part 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 was received
  analogWrite(5, Serial.read());//output received byte
}

Part 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.”);