In this example we connected a Raspberry Pi 3 with 3 relay boards via MCP23017 I2C port expander and switch the relays.
You can control 16 relays via MCP23017. Via the I2C port of the Raspberry Pi you can control up to 8 MCP23017 (you can assign 8 different addresses to the MCP23017). So you could easily control 128 relays.
The Raspberry Pi does not have enough power to switch 16 relays !! An additional power supply must be connected. We have connected an additional breadboard power supply.
To control the relays with the Raspberry Pi via a port expander MCP23017 must be activated on the Raspberry Pi I2C. This can be done by the raspi-config by command line or via the desktop Raspberry PI Configuration.
The software WiringPi must be installed. Is already installed in the standard case with current images for the Raspberry PI (Y2017).
The layout Raspberry Pi with I2C port expander MCP23017 and 12 relays.
We do not describe here the exact pin assignment, which pin from where to where. Everyone can inform themselves, if he wants, which pin has which function.
We have only basically the GPIO 3.3Volt and the 5V current voltage separately. It is recommended several times. It should also be noted that this layout works only over very short distances, because there is still no ULN2803 in between. This layout will be shown in a different manual.
Control of the relays
If you have plugged the cables on the port expander MCP23017 as on our plan, the MCP23017 has the address 20.
To check whether the MCP23017 is addressable and has the address 20, enter this command:
i2cdetect -y 1
As seen in the figure, the system has found a device with address 20.
There are now various possibilities and commands to address the new GPIO ports via the MCP23017.
The very complicated hexadecimal code control
i2cset -y 1 0x20 0x01 0x00 #all Pins GPB are Output bottom row i2cset -y 1 0x20 0x00 0x00 #all Pins GPA are Output top row
To know exactly which hex code controls which pin, you have to look at the data sheet of the manufacturer the individual hex codes. http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
How to pin to control is found in the “SUMMARY OF REGISTERS ASSOCI
ATED WITH THE GPIO PORTS “table
Easy Way: Or you can control each individual pin with a number with the gpio command.
gpio -x mcp23017:100:0x20 mode 108 out
Mcp23017 = is our port expander MCP23017
100 = The GPIOs start at number 100
0x20 = is the address of our MCP23017 (20)
Mode = the mode of the pin is determined in or out.
108 = is the GPIO number 8 on the MCP23017 (the far left in the lower row)
Out = is an output
gpio -x mcp23017:100:0x20 write 108 1 gpio -x mcp23017:100:0x20 write 108 0
These two instructions switch pin 8 (108)
The most common error source is the wrong GPIO pin number. Here the numbering.
Please note, this is the top view as on the breadboard! The circuit diagram is mirrored!
The GPIO pin numbers start at 100 because we gave the GPIO -start address 100 to the mcp23017, which has address 20.
If the mcp23017 would be assigned the GPIO start address 200 as:
gpio -x mcp23017:200:0x20 write 208 0
So the same pin, which first had the number 108, would now have the number 208.