Node Red with Arduino Simple Example

Node Red is flow based programming tool useful to build IoT(Internet of Things) application. It allows users to write IoT program to connect sensors, actuators etc and display the sensor data on the web.  Here a simple example of using node red with Arduino is illustrated. In this demonstration the state of a LED which is connected to Arduino is sent and displayed on local web site. The state of the LED can be either ON or OFF controlled using a push button. If the push button is pressed then the LED is turned ON and vice versa. The state of the LED is sent from Arduino via USB serial port to the PC which is running the node red. In node red a simple application is created to display the state of the LED. The display showing the state can then be viewed on the browser on local IP. The node red application can be run either locally or on public IP address and on IoT cloud such as IBM Cloud, or Arduino IoT cloud and other free IoT clouds.

How this works is illustrated by the following diagram.

Arduino with Node Red LED state illustration


First we need to write code for the Arduino and upload code. The Arduino code used here is simple program that detects the state of a push button and turns on or off a LED. According to the state of the push button and therefore the LED we write the state to the serial port of Arduino. The following circuit diagram shows connection of the Arduino with LED and push button is shown below.

The LED is connected to digital pin 13 and grounded via 220Ohm resistor. The push button is connected to the digital pin 2. On one side it is grounded and the other side pulled high to +5V.

 The program code for the Arduino is as follows.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

The above program can be found in the Arduino IDE button example. In the program we monitor the state of the button and when it low the LED remains off. When the push button is pressed the LED is turned on. This is then printed on the serial monitor as shown below.

 The same message is also sent via the USB serially to the PC.

Node Red

In order to use Node Red we have to install node.js. This can be downloaded and installed from the following link.

When the node.js is installed the npm(Node Package Manager) gets also installed.

In windows OS, we install the node red using npm by typing the following command in the command prompt window.

npm install -g --unsafe-perm node-red

Once the node red is installed we have to run the node-red application by typing the following commad.


This installation process is illustrated below.

For other operating system the installation process of  see:

Creating Node Red application

  To start writing Node Red application we open a browser and type in the following localhost:port address:

Then you should see node red interface.

node red interface

 On the left side we have the node panel where different nodes can be dragged into the blank flow. On the right side of the blank flow we have configuration and setting panel.

The serial port I/O is not installed by default in node red and a dashboard nodes is required to display our data so we have to first install them. We can install new libraries for the node red from the menu > manage palette as shown below.

In the manage palette, go to install tab and install the node-red-node-serialport and node-red-dashboard as shown below.

Once installed, these node palette should appear on the left side node panel.

From there we have to drag the serial in node from network node and text node from the dashboard node into the node red sketch.

 Now we have configure the two nodes. Double click on the serial node to bring up its properties window. In the properties window select the serial port to which the Arduino is connected to. Here COM17 was used and set the same baud rate of 9600bps as used in the Arduino sketch above.

We can also provide name Arduino to the serial in node.

Then we have to configure the text node of the dashboard. Double click the text node to bring up its properties window. Here we have to set the group and the tab name. 

Finally connect the serial in node with the text node.

node red program for arduino

 On the top bar click on the Deploy button and then open a new tab in the browser and type in 127.00.1:1880/ui. You should see the following displayed on the browser.

node red dashboard

Initially the state is in OFF state. When the button is pressed the LED will turn on and the Arduino will sent ON message to the node red application written above and the dashboard will show ON state. This is demonstrated in the following video.

So this tutorial showed how to make a simple IoT application using Node Red and Arduino. Here data about the state of the LED(or button) was sent to PC running node red application and the red node application displayed the data on the web based dashboard. Although here data was presented at local IP we can host website from home to display the data in public IP address. Also here the data was transferred via the USB serial link but we can also use an alternative method which is to transfer data from NodeMCU via WiFi to the node red application. 

Node red is easy to use and requires no hard coding. Node Red is better IoT application framework then such as using javascript libraries like P5.js illustrated in the tutorial How to display Sensor data in real time on Web.

Post a Comment

Previous Post Next Post