Using NodeRED on Raspberry Pi for Relay and LED Control

From ArduinoInfo
Jump to navigation Jump to search


Wiki(Circuit for Node-RED bb).png

Node-RED is a flow-based development tool for visual programming developed originally by IBM for wiring together hardware devices, APIs and online services as part of the Internet of Things (IoT).

Node-RED provides a web browser-based flow editor. It consists of a Node.js-based runtime where you use a web browser to access the flow editor which can be used to create JavaScript functions. Elements of applications can be saved or shared for re-use. Within the browser you create your application by dragging nodes from your palette into a workspace and start to wire them together. With a single click, the application is deployed back to the runtime where it is run.

The palette of nodes can be easily extended by installing new nodes created by the community and the flows you create can be easily shared as JSON files.

Flow-based Programming

Invented by J. Paul Morrison in the 1970s, flow-based programming is a way of describing an application’s behavior as a network of black-boxes, or “nodes” as they are called in Node-RED. Each node has a well-defined purpose; it is given some data, it does something with that data and then it passes that data on. The network is responsible for the flow of data between the nodes.

It is a model that lends itself very well to a visual representation and makes it more accessible to a wider range of users. If someone can break down a problem into discrete steps they can look at a flow and get a sense of what it is doing; without having to understand the individual lines of code within each node.

In this project, we are going to use Node-RED control an LED and a relay which are connected to Raspberry Pi GPIOs.

Hardware Needed

  1. Raspberry Pi 3 (Raspberry Pi Zero W would work too)
  2. At least, 8GB C10 microSD card
  3. Raspberry Pi 5V, 2A recommended power adapter
  4. 5mm LED
  5. 220 ohm resistor
  6. Jumper wires
  7. 5V relay module
  8. 3rd-party PC for editing the Node-RED flows in a web browser

Software Needed

  1. PuTTY for accessing the Raspberry Pi over ssh
  2. SD Card Formatter for formatting the microSD card
  3. Etcher for writing an image unto the microSD card
  4. Raspbian Stretch Lite


Using Raspberry Pi in Headless Mode without a Wired Network Interface

You can use the Raspberry Pi in Headless mode and access it via SSH (using a software like PuTTY) without any wired interface (like a LAN connection from the Raspberry Pi to the Router, in which the remote computer that would be accessing the Raspberry Pi via SSH.

Boot directory

Follow these steps:

  1. Insert the microSD card to a computer through an microSD card adapter
  2. Format the microSD card and write the Raspbian Stretch OS to the SD card
  3. Open the boot drive of the SD card (i.e. the root directory as seen on a PC mounting the SD card) that appears in "This PC" directory of a windows PC and create an empty file called SSH with no file extension.
  4. Also create a file named wpa_supplicant.conf. This is a configuration file for wireless access. Copy and paste the text below to the file:
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 
    "Your_Network_Name" represents the name of the name of the Wi-Fi. It is case-sensitive and character-sensitive. "Your_Network_Password" represents the password of the Wi-Fi network.
  5. Save (ctrl+o) and exit (ctrl+x)
  6. Insert the microSD card into the Raspberry Pi and power it up
  7. Make sure the router or mobile hotspot used in the wpa_supplicant.conf is turned ON, then connect the 3rd-party PC to the same hotspot
Obtaining IP address
  1. Obtain the IP address of the Raspberry Pi; if using an Android phone, go to Settings → Wireless & networks → Tethering & portable hotspot → Portable WLAN hotspot → Device list
  2. Open PuTTY and type the IP address of the Raspberry Pi to access it via SSH
    Login using PuTTY
  3. A terminal would open. The default username is pi and the default password is raspberry (Note: The username and password are case-sensitive. It is advisable to change them immediately)
    Logged in
  4. To enable ssh, it is necessary to go into the Pi's configuration settings. Type sudo raspi-config. Go to Interfacing Options → SSH → Yes.

Reboot the Pi and login again using PuTTY

Installing Node-RED on Raspberry Pi

Before installing Noode-RED, ensure the Pi is updated and upgraded:

sudo apt-get update && sudo apt-get upgrade

The recommended way to install Node-RED on Pi is to use the script below:

bash <(curl -sL

Start Node-RED



node-red-pi --max-old-space-size=256 

if having memory constraints on the Pi i.e. if there is not much available RAM on the Pi.

If you want Node-RED to run when the Pi boots up you can use

sudo systemctl enable nodered.service

Use this to disable auto-start when the Pi boots up:

sudo systemctl disable nodered.service

Use this to stop Node-RED


Designing a Flow to Control Relay and LED

The Node-RED editor window consists of four components:

  1. The header at the top, containing the deploy button, main menu, and, if user authentication is enabled, the user menu.
  2. The palette on the left, containing the nodes available to use.
  3. The main workspace in the middle, where flows are developed by dragging nodes from the palette and wiring them together. The workspace has a row of tabs along the top; one for each flow and any subflows that have been opened.
  4. The sidebar on the right.

It is necessary to have the hardware ready before designing the Node-RED flow. Set up the circuit as shown:

Wiki(Circuit for Node-RED bb).png

Notice that the Vin of the relay module is connected to the 3.3V Pin of the Pi instead of the 5V Pin. This is because the voltage level for a digital HIGH (1) state in the raspberry Pi is 3.3V as opposed to the 5V of the Arduino. A digital LOW (0) signal is use to trigger ON the relay while a digital HIGH (1) signal is used to trigger OFF the relay. Therefore, in order to trigger OFF the relay with the digital HIGH signal from the Pi which is 3.3V, it is necessary to set it's Vin to 3.3V to make this the reference signal for the digital HIGH.

It is necessary to install the Node-RED dashboard nodes to enable you design a better user interface (UI).

To achieve this, follow these steps:

  • Navigate to the Node-RED directory
 cd ~/.node-red/
  • Install the dashboard nodes on the Pi using the PuTTY terminal
 sudo npm install node-red-dashboard
  • Open a web browser such as Google Chrome or Mozilla Firefox and type in the IP address of the Pi followed by ":1880 for example, A page similar to this should be displayed, which would let you design your Node-RED flows:
  • From the dashboard category of the palette, drag and drop the switch node onto the workspace
  • Double-click on the switch node on the workspace to edit the properties
  • Click on the Group bar to add new ui_group
Node6 create new ui group.PNG
  • Type in the desired name and click on the Tab drop-down arrow to add new ui_tab
Node7 edit the group.PNG
  • Type in the desired name and click Add → Add
Node8 edit the tab.PNG
  • Change the ON Payload and OFF payload to string
Node3 change payload to string.PNG
  • Set the ON Payload value to 0 and the OFF Payload value to 1. This is because the relay is triggered ON by a digital LOW signal (0) and triggered OFF by a digital HIGH signal (1). You can adjust this according to the type of relay being used in the project.
Node4 set ON and OFF payloads.PNG
  • Set the Label (the text that would appear beside the switch button) and the Name (the text that would appear as the heading)
  • Click on Done
  • From the Raspberry Pi category of the palette, drag and drop the Raspberry Pi output node onto the workspace
Node9 add RPi output node for GPIO.PNG
  • Double-click on the node to edit its properties
Node10 select the output pin.PNG
  • Set the pin to Pin 5 (GPIO03)
  • Check the initialize pin state?; set initial level of pin - high (1) and click Done
Node11 initialize pin state and set it to HIGH.PNG
  • Join the switch node to the PIN node by clicking on the dot at the right of the switch node and dragging it to the dot on the PIN node
Node12 join the nodes by clicking and dragging.PNG
  • Click on deploy
  • Open a new tab on the browser and append /ui to the initial url, e.g
  • A user interface would show up which would be used for the control
Node13 user interface.PNG

See demo:

Please email comments, critiques, suggestions and questions to: