The MCP23017 is a versatile I/O expander from Microchip that allows a microcontroller to control up to 16 additional digital inputs or outputs via a two-wire I2C serial interface. This component is particularly useful in applications where additional I/O pins are needed, such as in button matrices, LED control, or when interfacing with multiple sensors. Common applications include home automation systems, industrial controls, and hobbyist projects where expansion of I/O capabilities is required.
Pin Number | Pin Name | Description |
---|---|---|
1-2 | A0-A1 | Hardware address pins to configure the I2C address |
3 | NC | No Connection (must be left unconnected) |
4 | VSS | Ground (0V) reference for the power supply |
5-12 | GPB0-GPB7 | General Purpose I/O pins (Port B) |
13 | VDD | Positive power supply pin |
14 | SCL | Serial Clock Line for I2C communication |
15 | SDA | Serial Data Line for I2C communication |
16-23 | GPA0-GPA7 | General Purpose I/O pins (Port A) |
24 | RESET | Active-low reset input |
25-28 | NC | No Connection (must be left unconnected) |
#include <Wire.h>
// MCP23017 I2C address (configured by A0, A1 pins)
const int mcpAddress = 0x20;
// Registers addresses
const int IODIRA = 0x00; // I/O direction register for port A
const int IODIRB = 0x01; // I/O direction register for port B
void setup() {
Wire.begin(); // Initialize I2C
pinMode(13, OUTPUT); // Use onboard LED for output
// Set all GPA pins to output, all GPB pins to input
mcpWrite(IODIRA, 0x00); // All A pins as output
mcpWrite(IODIRB, 0xFF); // All B pins as input
}
void loop() {
// Toggle all GPA pins
mcpWrite(0x12, 0xFF); // Turn on all GPA pins
delay(500);
mcpWrite(0x12, 0x00); // Turn off all GPA pins
delay(500);
}
// Function to write data to a register on MCP23017
void mcpWrite(int reg, int data) {
Wire.beginTransmission(mcpAddress);
Wire.write(reg); // Register address
Wire.write(data); // Data to write
Wire.endTransmission();
}
Q: Can I use multiple MCP23017 devices on the same I2C bus? A: Yes, you can use up to 8 MCP23017 devices on the same I2C bus by configuring each with a unique hardware address (0x20-0x27).
Q: What is the maximum I2C clock frequency the MCP23017 can handle? A: The MCP23017 supports a maximum I2C clock frequency of 1.7 MHz in HS mode.
Q: Do I need to use external pull-up resistors on the I/O pins? A: No, external pull-up resistors are not required on the I/O pins unless you are using them in an open-drain configuration.