This tutorial is looking to understand the concepts of Node-RED and to use it to control LEDs via the Raspberry Pi's GPIO pins.
What is Node-RED?
Node-RED is a new programming language and a visual programming tool with a drag-and-drop visual tool which comes pre-installed on Raspberry Pi and is an open source tool for building Internet of Things (IoT) applications.
On the left of the screen, you can see many coloured blocks. These blocks are called nodes. In the centre, you have the Flow and this is where you make the program. You can create as many flows as you want and they can all run at the same time. For this tutorial, we will only need one flow.
If you select a node, you can see how it works in the info tab. Scroll right down to the bottom of the list and you will see some nodes labelled Raspberry Pi.
There are 40 pins on the Raspberry Pi board and they provide a number of different functions.
To identify the functions for each pin and their number on the board see the following figure.
Anything connected to these pins will always receive 3.3V of power
Anything connected to these pins will always receive 5V of power
Ground used to complete the circuit
These pin are for general-purpose use and can be configured as input or output pins
Special purpose pins
Note: Randomly plugging wires and power sources into your Raspberry Pi may destroy it, especially if you are using the 5V pin.
You will need some software and hardware equipment:
Node-RED is already installed software on Raspberry Pi but in case if not, open a terminal from puTTY software and type:
$ sudo apt-get install nodered
You’ll be prompted to confirm the installation, type Y and press Enter.
The installation should be completed after a couple of minutes.
Connect the components as the figure bellow:
First of all you have to open Node-RED from Raspberry Pi Graphical User Interface (GUI),
Note: If you don’t have screen you can open a Raspberry Pi GUI from your laptop click here and follow steps.
2. Now go to the Internet menu and open Chromium Web Browser.
3. In Chromium, locate the address bar at the top and type:
then press Enter. This will display the Node-RED interface.
Note:localhost is the address the Raspberry Pi uses to refer to itself and :1880 means that it is looking at port 1880.
4. Scroll the nodes down to Raspberry Pi, you will see two nodes with the label rpi gpio: used to talk to the GPIO pins on the Raspberry Pi. The first one with the raspberry icon on the left, is for inputs. The node, with raspberry icon on the right, is for outputs. Drag an output node onto the blank page in the middle.
5. Double-click on the node and Change the GPIO pin to be GPIO17 and click Initialise pin state?. Leave the setting for Initial level of pin on low and give the node a name and click Done.
6. scroll back up to the list of nodes and drag an inject node onto the flow.
7. Double-click on the inject node. click Payload and choose string and type 1 in the Payload box . Type On in the Name box. Press Done.
8. Repeat the previous step to create one more inject node, but for this node add 0 as the payload message, and call this node Off.
9. Click on the grey dot on the inject nodes ( for each On and Off nodes ) and drag to the grey dot on LED node to join them up.
10. Click on the Deploy button on the top right. A message should pop up at the top saying "Successfully injected: On".
11. Now click on the blue square on the left of the On node. The LED should turn on. And do the same way on Off node to turn off the LED.
Now you know the basics of Node-RED, what about going further and add button to the circuit, to do that the following steps should act as a guide:
2. As you are controlling the LED with button, so we no longer need the On and Off inject nodes, so to delete them click on them and press Delete on the keyboard.
3. Add new rpi gpio node, click on it and choose pin7 – GPIO4 as GPIO, click on pullup on resistor?. And choose Read initial state of pin on deploy/restart?, For the name, write any name you want, but here I will name it Button .
Note: pullup means that GPIO 4 will be set to HIGH, and pressing the button will cause it to go LOW.
4. Now join up your button node output to the existing debug and LED node. Deploy the flow and test it by pressing the button.