Raspberry Pi with I2C Port expander MCP23017 16 Relay switching

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.

Raspberry Pi I2C Enable
Raspberry Pi I2C Enable

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.

Raspberry Pi with I2C Port expander MCP23017 16 Relay switching
Raspberry Pi with I2C Port expander MCP23017 16 Relay switching

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
Raspberry Pi with I2C Address
Raspberry Pi with I2C Address

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

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!

MCP23017 Pin-addresses
MCP23017 Pin-addresses

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.

3 Replies to “Raspberry Pi with I2C Port expander MCP23017 16 Relay switching”

  1. Hi

    Hope this gets to you, I am trying to install the node and I can’t get it to install. I will admit I’m a noob with Linux.

    Is it possible for you to help me pls if you don’t mind?

  2. this is EXACTLY what i’ve been looking for …..BUT
    how do i use the command within a program???
    “gpio -x mcp23017:100:0x20 write 108 1”
    my aim is to have two “mcp23017’s” controlling two 16 channel relays”.
    i’m trying to make a water feature in my garden with 3 sets of coloured lights RED, GREEN, BLUE eight of each colour and eight waterjets to control water flow.
    each water jet will have a RED,GREEN and BLUE light. whichever water jet comes on (chosen at random) a random light will come on.

    I’m VERY new to the Raspberry Pi and have been struggling for several weeks now, you help would be greatly appreciated

    Regards Frank

Leave a Reply

Your email address will not be published.