The MCP23017Control
class provides an interface for interacting with the MCP23017 I/O expander chip over I²C on Arduino platforms. This class includes methods for initializing the chip, configuring pin modes, and reading/writing digital values to individual pins or entire banks.
- MCP23017Control(byte address = 0x20)
address
: I²C address of the MCP23017. Default is0x20
.
Initializes the MCP23017 I/O expander. This method sets up the I²C communication and configures all pins on the MCP23017 as inputs for safety.
initWire
: A boolean parameter that determines whether the Wire (I²C) library should be initialized within this method. Defaults totrue
.- If
true
,Wire.begin()
will be called to initialize the I²C communication. - If
false
,Wire.begin()
will not be called, allowing for manual initialization of the I²C bus elsewhere in your code.
- If
Sets the mode of a specific pin.
pinNumber
: Pin number (0-15).mode
: Mode of the pin (INPUT
,OUTPUT
,INPUT_PULLUP
).
Writes a digital value (HIGH or LOW) to a specified output pin. If the pin is set as an input, it automatically changes the pin mode to INPUT
or INPUT_PULLUP
.
pinNumber
: Pin number (0-15).state
: State to write (HIGH or LOW).
Reads and returns the state (HIGH or LOW) of a specified input pin.
pinNumber
: Pin number (0-15).
Sets a specified output pin to high.
outputNumber
: Pin number (0-15).
Sets the mode for all pins in a specified bank (Bank A or Bank B) of the MCP23017.
bank
: Bank number (0 for Bank A, 1 for Bank B).mode
: Mode to set for all pins in the specified bank (INPUT
,OUTPUT
,INPUT_PULLUP
).
Writes a byte value to all pins of either Bank A or Bank B.
bank
: Bank number (0 for Bank A, 1 for Bank B).value
: Byte value to write to the bank.
Reads and returns the state of all pins in either Bank A or Bank B.
bank
: Bank number (0 for Bank A, 1 for Bank B).
Sets the mode for all pins across both Bank A and Bank B of the MCP23017.
mode
: Mode to set for all pins (INPUT
,OUTPUT
,INPUT_PULLUP
).
Returns the combined state of all 16 pins as a 16-bit value.
Writes a 16-bit value to set the states of all 16 pins.
value
: 16-bit value representing the state of each pin.
Returns a 16-bit number with only one bit set, based on the specified position.
bitPosition
: Position of the bit to set (0-15).
#include "MCP23017Control.h"
MCP23017Control mcp(0x20); // Replace 0x20 with your MCP23017's I2C address
void setup() {
mcp.begin(); // Initialize the MCP23017 with default Wire.begin()
// or
mcp.begin(false); // Skip Wire.begin() if I²C has been initialized elsewhere
}
// Set pin 0 as an output and pin 8 as an input with pull-up
mcp.pinMode(0, OUTPUT);
mcp.pinMode(8, INPUT_PULLUP);
// Write HIGH to pin 0 (e.g., to turn on an LED)
mcp.digitalWrite(0, HIGH);
// Read the state of pin 8
byte state = mcp.digitalRead(8);
if (state == HIGH) {
// Pin 8 is HIGH
}
// Set pin 3 to HIGH
mcp.setPinHigh(3);
// Set all pins in Bank A to OUTPUT
mcp.setBankMode(0, OUTPUT);
// Set all pins in Bank B to INPUT_PULLUP
mcp.setBankMode(1, INPUT_PULLUP);
// Write a byte value to Bank A
mcp.writeBank(0, 0xFF); // Set all pins in Bank A to HIGH
// Read the current state of Bank B
byte bankBState = mcp.readBank(1);
// Set all pins to be inputs with pull-up resistors
mcp.setAllPinsMode(INPUT_PULLUP);
uint16_t allAllPins = mcp.readAllPins();
// Set the first 8 pins to HIGH and the rest to LOW
mcp.writeAllPins(0x00FF);
// Get a bitmask with only the 5th bit set
uint16_t bitmask = MCP23017Control::getBitmask(5);
This example demonstrates toggling all pins on and off in an alternating pattern.
void setup() {
mcp.begin();
mcp.setAllPinsMode(OUTPUT); // Set all pins as outputs
}
void loop() {
mcp.writeAllPins(0xFFFF); // Set all pins HIGH
delay(500);
mcp.writeAllPins(0x0000); // Set all pins LOW
delay(500);
}
- The
MCP23017Control
class does not currently provide explicit error handling for I²C communication failures. - Ensure that the I²C address provided to the constructor matches the hardware configuration.
- The class is designed for standard Arduino boards and may require modifications for use with other microcontroller platforms.
1.0.0
- Initial release of
MCP23017Control
. - Support for basic I/O operations, bank-level control, and pin mode configurations.