Lux monitoring – LDR Arduino and Nodejs

Hello guys! How are you? I hope everything’s ok!

So, in this post I’ll try explain, how you can do the lux monitoring system, using:

– Arduino Uno,

Photo-resistor LDR,

Two led (green and red) to visual status indicator

Nodejs and chart.js module,  to show the lux range in real time.

Step 0 > Install Nodejs and NPM

To make this task you need install before the nodejs and npm

Step 1 > Electrical connections

See the electrical connections bellow:


Step 2 > Installation of dependencies:

Create the folder the folder Node_LDR;

Into this folder create the packagejson.js:

Into the packagejson.js file, write the code below:

    "name": "LDR_Aduino_Nodejs",
    "version": "0.0.1",
    "dependencies": {
      "chart.js": "^2.2.1",
      "express": "^4.14.0",
      "serialport": "^4.0.1",
      "": "^1.4.8"

Save and close!

After, open your prompt command and run npm install:


When the installation is finished look into the folder   Node_LDR:

– Node_LDR

— node_modules

— package.json

If your folder it’s the same that format, everything’s ok!!

Step 3 > Installation of Arduino IDE:

Click here and install the IDE


Step 4 > C++ code:

Open your arduino IDE and write the follow code:

int portaledred = 10;
int portaledgreen = 11;
int pinoport = 5;
int valorport = 0;
float luminosidade = 0;

void setup() {
 pinMode(portaledred, OUTPUT);
 pinMode(portaledgreen, OUTPUT);

void loop() {
 valorport = analogRead(pinoport);

 luminosidade = map(valorport, 0, 1023, 0, 255);
 Serial.print("Valor do LDR : ");
 Serial.print(" = Luminosidade : ");

 if (valorport > 900)
 digitalWrite(portaledred, HIGH);
 digitalWrite(portaledgreen, LOW);
 digitalWrite(portaledred, LOW);
 digitalWrite(portaledgreen, HIGH);

Make Upload the code on Arduino board:


After the upload is done make a test

Step 5 > Communication between Arduino, Nodejs and Web page:

Open the text editor of your preference and create the app.js with code below:

var app = require('express')();
var express = require('express');

app.use(express.static(__dirname + '/public'));

var http = require('http').Server(app);
var io = require('')(http);

app.get("/", function(req, res){
 res.sendFile("/cygwin64/home/pablo.binotto/node/arduino/index.html");//here is necessary the all url of folder into the c:/

var mySocket;

var serialport = require('serialport');
var SerialPort = serialport.SerialPort;

var mySerial = new SerialPort("COM5", {
 baudrate: 9600,
 parser: serialport.parsers.readline("\n")

mySerial.on('open', function(){
 console.log("Porta aberta.");

mySerial.on("data", function(data){
 io.emit("dadoArduino", {
 valor: data

io.on("connection", function(socket){
 console.log("Usuario conectado")

http.listen(3000, function(){
 console.log("Server running on 3000 port");

Save this file on Node_LDR folder and on the same folder, create the file index.html follow the code below:

<!DOCTYPE html>
<html lang="">
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatiblr" content="IE=edge">
 <meta name= "viewport" content="width=device-width, initial-scale=1">
 <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
 <canvas id="myChart" width="400" height="400"></canvas>
 <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%20var%20ctx%20%3D%20document.getElementById(%22myChart%22)%3B%0A%20%0A%20var%20myLineChart%20%3D%20new%20Chart(ctx%2C%20%7B%0A%20type%3A%20'line'%2C%0A%20data%3A%20data%2C%0A%20options%3A%20options%0A%20%7D)%3B%0A%20%0A%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
 <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%20var%20socket%20%3D%20io()%3B%0A%20socket.on(%22dadoArduino%22%2C%20function(dado)%7B%0A%20console.log(dado)%3B%0A%20%7D)%3B%0A%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />

On your prompt command write: node app.js and click enter…

Open a new web page and go to localhost:3000…

In this video below, you can see the communication happening

Step 6 > Show the result on line graph into the web page:

See you again!

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *