MCP23017 IO Expansion Board I2C Interface SG IO E017

SG-IO-E017
SG-IO-E017-A

Overview

This product adopts I2C interface to realize 16 I/O port expansion. Support for Raspberry Pi Zero/Zero W/Zero WH/2B/3B/3B+/4B etc models as well as Arduino and STM32. We provide C and Python demo codes for the Raspberry Pi, as well as Arduino and STM32 demo codes, that can implement input testing, output testing, and interrupt testing.

Product Parameters

ModelSG IO E017SG IO E017 A
Size45mm(Length) x 25mm(Width)35mm(Length) x 25mm(width)
Control ChipMCP23017MCP23017
Signal InterfaceI2CI2C
Supply Voltage3.3V/5V3.3V/5V
Expansion I/O1616
Interrupt PinINTA、INTBINTA、INTB

The resource profile diagram is shown in the figure below:

Resource Definition

① PH2.0 connector leads out control pin

② MCP23017 chip

③ Expand PA and PB ports

④ DIP switch, set I2C address

⑤ Reserve 2.54mm pitch control pin

Usage

This module demo codes sets the PA port (PA0~PA7) as the output and cycles the high and low levels at an interval of 1 second. The PB port (PB0~PB7) is set as the input. In addition, it is configured that when the level of any pin of the PB port changes, an interrupt is generated through the INTB pin. At this time, the level of the PB port is read.

Raspberry Pi Demo Codes Usage

Since the bookworm system no longer supports the wiringpi library, the example program for this system uses the lgpio library, and for the bullseye system, the wiringpi library version of the example program can be used.

Hardware Interface Configuration Description

The demo codes in the Raspberry Pi motherboard adopts the pin definitions number by wiringPi, and the bookworm system uses the pin definition of the BCM number.

The module with Raspberry Pi motherboard wiring is defined in the following table:

PinPin FunctionRaspberry Pi
VCCPower supply positive(3.3V/5V)3.3V
GNDPower supply groundGND
SDAI2C data lineSDA1
SCLI2C clock lineSCL1
INTAPA port interrupt pinP0(wiringpi number)
INTBPB port interrupt pinP1(wiringpi number)

Demo Codes Usage

Wiringpi Library Installation

First, use the "gpio -v" command on the terminal to check whether the wiringpi library has been installed. If the information of 2.52 appears, it means that the installation has been successful. You can skip this part of the content, otherwise you need to install it as follows:

sudo apt-get install wiringpi

#For Raspberry Pi systems after May 2019 (earlier ones do not need to be executed), an upgrade may be required:

wget https://project-downloads.drogon.net/wiringpi-latest.deb

#Version 4B upgrade of Raspberry Pi,If your download fails or the #speed is very slow, you can download it at the following linkand then #and copy it to the Raspberry Pi motherboard:

#https://github.com/seengreat/wiringpi-library

Version 4B upgrade of Raspberry Pi

sudo dpkg -i wiringpi-latest.deb
gpio -v

If version 2.52 appears, the installation is successful

#For the Bullseye branch system, use the following command:

git clone https://github.com/WiringPi/WiringPi
cd WiringPi
./build
gpio -v

#Running gpio - v will result in version 2.70. If it does not appear, it indicates an installation error

If the error prompt "ImportError: No module named 'wiringpi'" appears when running the python version of the sample program, run the following command

#For Python 2. x version

pip install wiringpi

#For Python3. x version

pip3 install wiringpi

Note: If the installation fails, you can try the following compilation and installation:

git clone --recursive https://github.com/WiringPi/WiringPi-Python.git

Note: The -- recursive option can automatically pull the submodule, otherwise you need to download it manually.

Enter the WiringPi Python folder you just downloaded, enter the following command, compile and install:

#For Python 2. x version

sudo python setup.py install

#For Python 3. x version

sudo python3 setup.py install

If the following error occurs:

SG-IO-E017/SG-IO-E017-A(MCP23017)

At this time, enter the command sudo apt install swig to install swig. After that, compile and install sudo python3 setup.py install. If a message similar to the following appears, the installation is successful.

SG-IO-E017/SG-IO-E017-A(MCP23017)

Lgpio library installation

For the bookworm system, the sample program uses the lgpio library. The following is the installation command for the library:

wget https://github.com/joan2937/lg/archive/master.zip
unzip master.zip
cd lg-master
make

sudo make install

Open I2C interface

sudo raspi-config

Enable I2C interface:

Interfacing Options -> I2C -> Yes

SG-IO-E017/SG-IO-E017-A(MCP23017)
SG-IO-E017/SG-IO-E017-A(MCP23017)
SG-IO-E017/SG-IO-E017-A(MCP23017)
SG-IO-E017/SG-IO-E017-A(MCP23017)

Restart the device.

sudo reboot

Run the command to check whether I2C is started.

lsmod

If i2c_bcm2835 and spi_bcm2835 are displayed, it means I2C module is started.

SG-IO-E017/SG-IO-E017-A(MCP23017)

Install the i2c-tools tool to confirm

sudo apt-get install i2c-tools

View connected I2C devices

i2cdetect -y 1

Check the address and indicate that the I/O expansion board and the Raspberry Pi are successfully connected, and the DIP switch is connected to the high level by default, and the displayed address is 0X27;

SG-IO-E017/SG-IO-E017-A(MCP23017)

Installation of Python libraries

The demo codes uses the python 3 environment. To run the python example program, you need to install smbus:

sudo apt-get install -y python-smbus

C Demo Codes

Open the demo codes/raspberry_pi/c directory

sudo make clean
sudo make
sudo ./main

Connect the expansion board according to the demo codes description. After running the program, the output result of the Raspberry Pi terminal is as follows:; "Port B: ff" means that all the input pins of the PB port of the module are high by default (0xff: 1111111); "Port A" refers to PA0~PA7 pins, "port A output high" refers to all PA port pins outputting high level, "port A output low" refers to all PA port pins outputting low level, and the module PA port output pins cyclically switch high and low levels at an interval of 1 second. Users can also connect PA0~PA7 to LEDs, and it can be observed that LEDs are flashing constantly. At this time, the output test is normal;

Connect the PB0 port to the GND with a jumper, and then trigger the interrupt on the INTB pin. The Raspberry Pi terminal outputs "portB: fe", that is, the input level of the PB port changes from the original 0xff (11111111) to 0xfe (11111110).

SG-IO-E017/SG-IO-E017-A(MCP23017)

Python Demo Codes

Open the demo codes\raspberry_pi\python directory

python3 mcp23017.py

The demo codes of Python version is similar to that of C language. The running result is shown in the following figure:The demo

SG-IO-E017/SG-IO-E017-A(MCP23017)

Arduino Demo Codes Usage

Wiring Instructions

SG-IO-E017Pin FunctionArduino
VCCPower supply positive(3.3V/5V)5V
GNDPower supply groundGND
SDAI2C data lineSDA
SCLI2C clock lineSCL
INTAPA port interrupt pinD2
INTBPB port interrupt pinD3

Demo Codes Usage

Open the project file demo codes\Arduino\mcp23017\mcp23017.ino with the Arduino IDE, click Verify, and upload it to the development board after verification.

SG-IO-E017/SG-IO-E017-A(MCP23017)

Click the tool, open the serial port monitor, set the baud rate to 115200, and observe the data changes.

SG-IO-E017/SG-IO-E017-A(MCP23017)

Open the serial port monitor and the display is as follows. Set the baud rate to 115200 (as shown in "1" in the figure below). The program running results are shown in the figure below:

SG-IO-E017/SG-IO-E017-A(MCP23017)

STM32 Demo Codes Usage

Wiring Instructions

SG-IO-E017Pin FunctionSTM32
VCCPower supply positive(3.3V/5V)3.3V
GNDPower supply groundGND
SDAI2C data linePB11
SCLI2C clock linePB10
INTAPA port interrupt pinPB3
INTBPB port interrupt pinPB4

Demo Codes Usage

Open the main. Uvprojx project file in the directory demo codes\STM32\USER with Keil_Vision5 software, and connect the module with STM32 according to the above table; The program is downloaded to the STM32 development board after being compiled without errors;

SG-IO-E017/SG-IO-E017-A(MCP23017)

The debugging information output serial port in the demo codes is USART1, where PA9 is Tx and PA10 is Rx; After connecting the cable, open the Serial Port Debugging Assistant, select the serial port number, adjust the baud rate to 115200, click "Open", and the Serial Port Debugging Assistant window is shown as follows:

SG-IO-E017/SG-IO-E017-A(MCP23017)

Resources

Product

Schematic

SG-IO-E017(MCP 23017)

SG-IO-E017-A(MCP 23017)

Demo Codes for bullseye system

Demo Codes for bookworm system

Data Sheet

MCP 23017

Technical Support

Technical Support and Product Notes