Relay control simplified
If you haven’t already, make sure to go through the setup tutorial. Once you’ve finished that we’re ready to go! The MCP23008 is a Integrated port expander that controls eight I/O channels through the I2C bus. This is one of our favorite chips to get started with, it is simple to use, and provides some great interactive feedback through the I/O so you can see your project working very quickly. Naturally, when we decided to start building libraries for Node-Red, this was the first product we grabbed. We manufacture dozens of boards that utilize this chip, you can check them out here.
If you haven’t already, get your devices plugged in, on a Raspberry Pi, that means installing the adapter, hooking up the I2C cable to the In port on the Relay Board, and powering the board. On any platform without native I2C, we’re going to assume you have a USB to I2C Adapter, which should also be connected to the MCP23008.
Now that you’re plugged in, let’s get this party started!
We are constantly working to improve our software libraries, as such, some of the screen shots in these articles may not reflect the latest functionality of the node. Please make sure to check out the "Info" tab on the node for the most up to date information.
Install the library
To make your life easier, we’ve build a library for the MCP23008 that provides a node-red node. This should prevent you from needing to muck about in the data sheet to find the appropriate I2C commands. You can use NPM to install this library from within your node-red directory (usually ~/.node-red):
npm install --unsafe-perm ncd-red-mcp23008
You can try this command without the --unsafe-perm
flag, it is there to allow nodeJS to access your hardware devices, it may not be necessary on all setups, but won’t hurt anything in any case.
Once this is done, spin up your node-red instance and you should see a node in the palette called ncd mcp23008. Drag this on over to the dashboard, and double click to open the configuration form.
ALL of our ncd-red-* libraries are functional without node-red as well. Check out the 'test.js' file to see example usage for vanilla NodeJS development.
Configure the Node
I2C Connection
Once you’ve opened the configuration panel, you’ll need to set up the I2C connection, click on the pencil icon next to that field, and you’ll be presented with some simple options.
If you are using a native I2C port, select I2C Bus from the Comm Type drop down, this will pull in a list of active I2C Busses available on your device.
If you are using the USB to I2C adapter, select NCD USB, and select your USB device from the I2C Bus list. The I2C Port field should stay at 0, unless you are using a BridgeX5, in which case it should reflect the I2C Port you are using on that device.
Once that’s done, click Add at the top of the form to save your connection configuration, this will automatically attach it to your new MCP23008 node.
MCP23008 Setup
Now that you have an I2C Connection configured, go ahead and set up the rest of the device. There are instructions in the info pane that explain what each field is for, so we’ll just go through a basic configuration here.
The default I2C Address for this board is 32 (0x20), so if you don’t have any jumpers installed on the address pins, you can leave that value as is.
If you are using inputs on the board, you probably want to be able to catch button presses and such, I generally set the interval pretty low for this, I’ve put it at 100 (ms) in this setup.
Since I’m using an interval, and don’t need to know when the status DOESN’T change, I’ve checked the “Only send message on change” checkbox.
To make reacting to events on specific channels easier, you can select “Output all channels” which will give you 9 total outputs, the first 8 of which are channels 1-8, the last output sends a payload that contains a JSON object with the state of all channels.
Lastly, I’m using a 4 channel relay board, so I uncheck channels 5-8 on the Channel Direction list, so the board knowns those are going to be used as inputs.
Once you’ve got this setup, click “Done” up top.
The node-red-dashboard package is a great way to quickly add a UI component to your project, we even use it during development of these libraries!
Control and Monitor
Now that we have our MCP23008 configured, we need to be able to use it. Node-red comes with a really handy node for testing things just like this, it’s called “Inject”. Let’s drag it over and set it up to control a relay.
There is a dropdown on the payload field you should use to select “09 Number” to define it’s a numeric payload, set the payload to 1. To control an individual channel, the topic of the message should be channel_{n}, in this case we’re just going to control channel 1, so we’re going to set the topic to “channel_1”.
I’ve given it a name of “Turn on Relay 1” and click Done. Since we may, at some point, need to turn this relay off, I’ve made a copy of the node (select the node and click cmd+c on mac, or cntl+c on windows, then cmd+v or cntl+v) and changed the payload of the copy to 0, and the name to “Turn off Relay 1”.
Once you’ve done that, drag the outputs of those Inject nodes over to the input of the MCP23008 node.
Now we can control the relay, but what if we need to act on a status change? Another node that gives us an easy way to see messages moving through the flow, is the “Debug” node. Grab one and move it to the right side of the MCP23008 node. By default, this node will print the payload of any message it receives to the Debug pane, since this is exactly what we want, all we need to do is drag the first output of the MCP23008 node over to the input of the debug node.
Your flow should now look something like the screenshot on the left. If it does, click on the “Deploy” button on the top right of the window.
All of those little blue dots will go away, indicating that your flow is up and running, and you can start testing! Click on the buttons next to your Inject nodes to turn your relay on and off, the debug window should show you a 1 or 0, respectively, whenever the relay status changes.
You can use the logic nodes, along with the input/output nodes that are a part of node-red to do some really cool stuff with your relay controller, more information about expected payloads and topics that the MCP23008 can utilize are available in the Info tab on the node itself, just double click on it any time to read up on how it works.
We hope you have as much fun playing with this as we did building it! If you find any issues, or would like to participate in the development, check out the GitHub repo. Check out the videos below for some ideas on the next steps to take.
TLDR; Import this flow
[{"id":"a31fd1e1.64637","type":"ncd-mcp23008","z":"ac30b31e.1f5ab","name":"","connection":"","addr":"32","interval":"500","onchange":true,"send_init":true,"outputs":9,"output_all":true,"io_1":1,"io_2":1,"io_3":1,"io_4":1,"io_5":false,"io_6":false,"io_7":false,"io_8":false,"persist":true,"startup":"6","x":470,"y":400,"wires":[["803ada18.49d828"],[],[],[],[],[],[],[],[]]},{"id":"803ada18.49d828","type":"debug","z":"ac30b31e.1f5ab","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":360,"wires":[]},{"id":"5b914e1f.07dee","type":"inject","z":"ac30b31e.1f5ab","name":"Turn on Relay 1","topic":"channel_1","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":380,"wires":[["a31fd1e1.64637"]]},{"id":"dcd18c08.4ea5d","type":"inject","z":"ac30b31e.1f5ab","name":"Turn off Relay 1","topic":"channel_1","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":440,"wires":[["a31fd1e1.64637"]]}]
Don’t forget to add your I2C Connection after you import this!
Video Tutorials
Introduction
A quick introduction to using NCD’s MCP23008 Node Red module. In this video we will install the ncd-red-mcp23008 module, configure it for use, and get some initial use out of it by turning a relay on and off. In this video we used the following hardware:
4- Channel General Purpose SPDT Relay Shield + 4 GPIO with IoT Interface
Timer Tutorial
In this video we will use NCD’s MCP23008 Node Red module. We will trigger a relay for a specified duration using out of the box functionality in Node Red.A quick introduction to using NCD’s MCP23008 Node Red module. In this video we used the following hardware:
4- Channel General Purpose SPDT Relay Shield + 4 GPIO with IoT Interface
Lets talk about Inputs
In this video we will have fun by setting some DIOs on the MCP23008 to inputs and triggering a relay output all in Node Red without writing a single word of code. In this video we used the following hardware:
4- Channel General Purpose SPDT Relay Shield + 4 GPIO with IoT Interface
An Elegant UI with Dashboard
In this video we will create an elegant Node Red UI for controlling the relays on our MCP23008 NCD relay controller as well as monitoring inputs using Dashboard. We will demonstrate the use of the interface on our iPhone but it can be done just as well on Android. In this video we used the following hardware:
4- Channel General Purpose SPDT Relay Shield + 4 GPIO with IoT Interface