Using Serial Monitor to Control MCP23017 I2C Port Expander With Arduino

by amachinetech in Circuits > Arduino

32 Views, 1 Favorites, 0 Comments

Using Serial Monitor to Control MCP23017 I2C Port Expander With Arduino

20251130_115352.jpg

Using the serial monitor, this is a way to individually control the outputs of an I2c ports expander connected to an Arduino. I created this to be able to test a portion of another project that I am working on. I needed to verify that all the connections of the port expander were correctly connected to the LED bar that I created for the other project.

Attached is the Data sheet from Digikey for these chips.

Supplies

(1) Arduino. I used a nano but a mega or a uno would work also.

(1) MCP23017 port expander

(16) leds

(16) 200ohm resistors

(3) single pole double throw switches (optional)

Some Background

The MCP23017 port expander has hundreds of uses. In this instance I am using it to control 16 LEDs for a display for another project. Before loading that code I needed a smaller piece of code that I could individually test each of the LEDs to make sure that they were connected the way I had in mind and that all the connections were good. This also tests the communication between the Arduino and the MCP23017 port expander.

Wiring

wiring.png

This is the way that I wired my display. You will notice that the LED display in the pictures only has 14 LEDs on it but the wiring drawing has 16. I only needed 14 for my project but I drew it out with all 16 outputs for this example. The outputs on the port expander are broken into two halves; A section and B section. In the photos you will also see a RFID module. That is part of the other project and isn't included in this wiring.

You will notice that there are three switches on the wiring diagram. This is to set the I2C address. Since I am prototyping I used switches to set the address. If you are making a permanent project and there is only one port expander you can either ground all three address pins for the address of 0x20 or attach them to +5v for an address of 0x27. Either way you need to attach the address pins either to ground or to +5v or the MCP23017 will not settle on an address and you will have communication problems. If you are having a hard time figuring out what the I2C address is then load I2C scanner attached below. This will scan your I2C bus and look for any attached I2C devices and print the address to the serial monitor. Make sure to set the baud rate of the serial monitor to 9600 in the lower right corner of the serial monitor window.

When you are sure of your I2C address load the serialControlI2CportExpander_v1.0.ino


The Program

20251130_115221.jpg
20251130_115257.jpg
20251130_115312.jpg

Once you load the Serial control sketch all of the output ports will be turned on upon power up. Open your Serial monitor and set the baud rate in the lower right corner for 9600. The name of the file and where it is saved on your computer will be printed in the serial monitor window. The default is to send to the A section. If you type number from 0 to 255 then that number will be sent to the A section of the port expander and turn on the lights according to that number. Using the numbers 1,2,4,8,16,32,64 or 128 will turn one light one at a time. Using numbers 1,2,7,15,31,63, or 255 will turn the lights on in a line.

If you type either a or b before the number it will select that respective half of the port expander and then adjust the lights as desired for the entry.

if you enter numbers 1,2,3,4,5,6,7,8,9 etc the port expander will count upward in a binary fashion.

Use this to build your next project

Happy Building

Machinetech