Node-RED blink

If you are looking to make a simple LED blink using Node-RED, you are in the right place. In this answer, I will explain how to use the built-in inject and function nodes in Node-RED to control an LED connected to a GPIO pin on a Raspberry Pi.

Step 1: Set up the hardware

Before we can start controlling anything with Node-RED, we need to set up the hardware. Connect an LED to a GPIO pin on your Raspberry Pi. Make sure to include the appropriate resistor to prevent damaging the LED. For example, if you connect the LED to GPIO pin 18, connect a 220 ohm resistor between the positive leg of the LED and pin 18. Connect the negative leg of the LED to a ground pin on the Raspberry Pi.

Step 2: Import the necessary nodes

In order to control the GPIO pins on the Raspberry Pi, we need to install the node-red-contrib-gpio module. To do this, go to the menu in Node-RED and select "Manage Palette." Click on the "Install" tab and search for "gpio." Install the node-red-contrib-gpio module.

Step 3: Create the flow

Now that we have our hardware set up and our necessary nodes installed, we can create the flow in Node-RED. Drag an inject node onto the flow and set it to inject a message every second. Then drag a function node onto the flow and wire it to the inject node.

[{"id":"b1fb46c9.9b2d5","type":"inject","z":"e025121c.8c33d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":140,"wires":[["44ce6d8.7a038a4"]]},{"id":"44ce6d8.7a038a4","type":"function","z":"e025121c.8c33d","name":"Toggle LED","func":"var state = context.get('state') || 0;\nstate = state ? 0 : 1;\ncontext.set('state', state);\n\nmsg.payload = state ? \"on\" : \"off\";\nmsg.topic = \"18\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":140,"wires":[["3c3f213b.28c9f8"]]},{"id":"3c3f213b.28c9f8","type":"gpio out","z":"e025121c.8c33d","name":"","state":"true","stateType":"bool","pin":"18","intype":"str","debounce":"25","read":false,"write":true,"x":550,"y":140,"wires":[]}]

The function node included in the flow toggles the state of the LED between on and off every time the inject node sends a message. The gpio out node included in the flow reads the state from the function node and sends it to the GPIO pin connected to the LED.

Step 4: Deploy the flow

After creating the flow, deploy it by clicking the "Deploy" button in the top right corner of the Node-RED screen. Your LED should now be blinking!

If you want to change the frequency of the blink, you can change the rate at which the inject node sends messages. If you want to use a different GPIO pin, you can change the pin number in the function node and the gpio out node.

That's it! With just a few simple steps, you can now control an LED connected to a Raspberry Pi using Node-RED.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]