#include //#define MCP23017_I2C_ADDRESS 0x21, 0x22, 0x23, 0x24 // I2C address of the MCP23017 IC //0x21 const uint8_t LED8 = 8; const uint8_t KEY8 = 9; const uint8_t LED9 = 10; const uint8_t KEY9 = 11; const uint8_t LED10 = 12; const uint8_t KEY10 = 13; const uint8_t LED11 = 14; const uint8_t KEY11 = 15; const uint8_t KEY3 = 0; const uint8_t LED3 = 1; const uint8_t KEY2 = 2; const uint8_t LED2 = 3; const uint8_t KEY1 = 4; const uint8_t LED1 = 5; const uint8_t KEY0 = 6; const uint8_t LED0 = 7; //0x22 const uint8_t KEY7 = 8; const uint8_t LED7 = 9; const uint8_t KEY6 = 10; const uint8_t LED6 = 11; const uint8_t KEY5 = 12; const uint8_t LED5 = 13; const uint8_t KEY4 = 14; const uint8_t LED4 = 15; const uint8_t LED12 = 0; const uint8_t KEY12 = 1; const uint8_t LED13 = 2; const uint8_t KEY13 = 3; const uint8_t LED14 = 4; const uint8_t KEY14 = 5; const uint8_t LED15 = 6; const uint8_t KEY15 = 7; //0x24 const uint8_t LED24 = 8; const uint8_t KEY24 = 9; const uint8_t LED25 = 10; const uint8_t KEY25 = 11; const uint8_t LED26 = 12; const uint8_t KEY26 = 13; const uint8_t LED27 = 14; const uint8_t KEY27 = 15; const uint8_t KEY19 = 0; const uint8_t LED19 = 1; const uint8_t KEY18 = 2; const uint8_t LED18 = 3; const uint8_t KEY17 = 4; const uint8_t LED17 = 5; const uint8_t KEY16 = 6; const uint8_t LED16 = 7; //0x23 const uint8_t KEY23 = 8; const uint8_t LED23 = 9; const uint8_t KEY22 = 10; const uint8_t LED22 = 11; const uint8_t KEY21 = 12; const uint8_t LED21 = 13; const uint8_t KEY20 = 14; const uint8_t LED20 = 15; const uint8_t LED28 = 0; const uint8_t KEY28 = 1; const uint8_t LED29 = 2; const uint8_t KEY29 = 3; const uint8_t LED30 = 4; const uint8_t KEY30 = 5; const uint8_t LED31 = 6; const uint8_t KEY31 = 7; MCP23017 mcp23017_1 = MCP23017(0x21); // instance of the connected MCP23017 IC MCP23017 mcp23017_2 = MCP23017(0x22); MCP23017 mcp23017_3 = MCP23017(0x24); MCP23017 mcp23017_4 = MCP23017(0x23); void setup() { Wire.begin(); // initialize I2C serial bus //mcp23017_1.init(); // initialize MCP23017 IC //mcp23017_2.init(); mcp23017_3.init(); mcp23017_4.init(); // Configure MCP23017 I/O pins configurePinsWithPinMode(); // familiar pinMode() style // Reset MCP23017 ports mcp23017_1.writeRegister(MCP23017Register::GPIO_A, 0x00); mcp23017_1.writeRegister(MCP23017Register::GPIO_B, 0x00); mcp23017_2.writeRegister(MCP23017Register::GPIO_A, 0x00); mcp23017_2.writeRegister(MCP23017Register::GPIO_B, 0x00); mcp23017_3.writeRegister(MCP23017Register::GPIO_A, 0x00); mcp23017_3.writeRegister(MCP23017Register::GPIO_B, 0x00); mcp23017_4.writeRegister(MCP23017Register::GPIO_A, 0x00); mcp23017_4.writeRegister(MCP23017Register::GPIO_B, 0x00); } void loop() { readAndWriteWithDigitalReadAndDigitalWrite(); } void configurePinsWithPinMode() { // Configure output pins mcp23017_1.pinMode(LED8, OUTPUT); mcp23017_1.pinMode(LED9, OUTPUT); mcp23017_1.pinMode(LED10, OUTPUT); mcp23017_1.pinMode(LED11, OUTPUT); mcp23017_1.pinMode(LED3, OUTPUT); mcp23017_1.pinMode(LED2, OUTPUT); mcp23017_1.pinMode(LED1, OUTPUT); mcp23017_1.pinMode(LED0, OUTPUT); mcp23017_2.pinMode(LED7, OUTPUT); mcp23017_2.pinMode(LED6, OUTPUT); mcp23017_2.pinMode(LED5, OUTPUT); mcp23017_2.pinMode(LED4, OUTPUT); mcp23017_2.pinMode(LED12, OUTPUT); mcp23017_2.pinMode(LED13, OUTPUT); mcp23017_2.pinMode(LED14, OUTPUT); mcp23017_2.pinMode(LED15, OUTPUT); mcp23017_3.pinMode(LED24, OUTPUT); mcp23017_3.pinMode(LED25, OUTPUT); mcp23017_3.pinMode(LED26, OUTPUT); mcp23017_3.pinMode(LED27, OUTPUT); mcp23017_3.pinMode(LED19, OUTPUT); mcp23017_3.pinMode(LED18, OUTPUT); mcp23017_3.pinMode(LED17, OUTPUT); mcp23017_3.pinMode(LED16, OUTPUT); mcp23017_4.pinMode(LED23, OUTPUT); mcp23017_4.pinMode(LED22, OUTPUT); mcp23017_4.pinMode(LED21, OUTPUT); mcp23017_4.pinMode(LED20, OUTPUT); mcp23017_4.pinMode(LED28, OUTPUT); mcp23017_4.pinMode(LED29, OUTPUT); mcp23017_4.pinMode(LED30, OUTPUT); mcp23017_4.pinMode(LED31, OUTPUT); // Configure input pins with internal 100K pull-up resistors // Third argument inverts the polarity of the input value when read mcp23017_1.pinMode(KEY8, INPUT_PULLUP, true); mcp23017_1.pinMode(KEY9, INPUT_PULLUP, true); mcp23017_1.pinMode(KEY10, INPUT_PULLUP, true); mcp23017_1.pinMode(KEY11, INPUT_PULLUP, true); mcp23017_1.pinMode(KEY3, INPUT_PULLUP, true); mcp23017_1.pinMode(KEY2, INPUT_PULLUP, true); mcp23017_1.pinMode(KEY1, INPUT_PULLUP, true); mcp23017_1.pinMode(KEY0, INPUT_PULLUP, true); mcp23017_2.pinMode(KEY7, INPUT_PULLUP, true); mcp23017_2.pinMode(KEY6, INPUT_PULLUP, true); mcp23017_2.pinMode(KEY5, INPUT_PULLUP, true); mcp23017_2.pinMode(KEY4, INPUT_PULLUP, true); mcp23017_2.pinMode(KEY12, INPUT_PULLUP, true); mcp23017_2.pinMode(KEY13, INPUT_PULLUP, true); mcp23017_2.pinMode(KEY14, INPUT_PULLUP, true); mcp23017_2.pinMode(KEY15, INPUT_PULLUP, true); mcp23017_3.pinMode(KEY24, INPUT_PULLUP, true); mcp23017_3.pinMode(KEY25, INPUT_PULLUP, true); mcp23017_3.pinMode(KEY26, INPUT_PULLUP, true); mcp23017_3.pinMode(KEY27, INPUT_PULLUP, true); mcp23017_3.pinMode(KEY19, INPUT_PULLUP, true); mcp23017_3.pinMode(KEY18, INPUT_PULLUP, true); mcp23017_3.pinMode(KEY17, INPUT_PULLUP, true); mcp23017_3.pinMode(KEY16, INPUT_PULLUP, true); mcp23017_4.pinMode(KEY23, INPUT_PULLUP, true); mcp23017_4.pinMode(KEY22, INPUT_PULLUP, true); mcp23017_4.pinMode(KEY21, INPUT_PULLUP, true); mcp23017_4.pinMode(KEY20, INPUT_PULLUP, true); mcp23017_4.pinMode(KEY28, INPUT_PULLUP, true); mcp23017_4.pinMode(KEY29, INPUT_PULLUP, true); mcp23017_4.pinMode(KEY30, INPUT_PULLUP, true); mcp23017_4.pinMode(KEY31, INPUT_PULLUP, true); } void readAndWriteWithDigitalReadAndDigitalWrite() { // Read and write individual inputs and outputs mcp23017_1.digitalWrite(LED8, mcp23017_1.digitalRead(KEY8)); mcp23017_1.digitalWrite(LED9, mcp23017_1.digitalRead(KEY9)); mcp23017_1.digitalWrite(LED10, mcp23017_1.digitalRead(KEY10)); mcp23017_1.digitalWrite(LED11, mcp23017_1.digitalRead(KEY11)); mcp23017_1.digitalWrite(LED3, mcp23017_1.digitalRead(KEY3)); mcp23017_1.digitalWrite(LED2, mcp23017_1.digitalRead(KEY2)); mcp23017_1.digitalWrite(LED1, mcp23017_1.digitalRead(KEY1)); mcp23017_1.digitalWrite(LED0, mcp23017_1.digitalRead(KEY0)); mcp23017_2.digitalWrite(LED7, mcp23017_2.digitalRead(KEY7)); mcp23017_2.digitalWrite(LED6, mcp23017_2.digitalRead(KEY6)); mcp23017_2.digitalWrite(LED5, mcp23017_2.digitalRead(KEY5)); mcp23017_2.digitalWrite(LED4, mcp23017_2.digitalRead(KEY4)); mcp23017_2.digitalWrite(LED12, mcp23017_2.digitalRead(KEY12)); mcp23017_2.digitalWrite(LED13, mcp23017_2.digitalRead(KEY13)); mcp23017_2.digitalWrite(LED14, mcp23017_2.digitalRead(KEY14)); mcp23017_2.digitalWrite(LED15, mcp23017_2.digitalRead(KEY15)); mcp23017_3.digitalWrite(LED24, mcp23017_3.digitalRead(KEY24)); mcp23017_3.digitalWrite(LED25, mcp23017_3.digitalRead(KEY25)); mcp23017_3.digitalWrite(LED26, mcp23017_3.digitalRead(KEY26)); mcp23017_3.digitalWrite(LED27, mcp23017_3.digitalRead(KEY27)); mcp23017_3.digitalWrite(LED19, mcp23017_3.digitalRead(KEY19)); mcp23017_3.digitalWrite(LED18, mcp23017_3.digitalRead(KEY18)); mcp23017_3.digitalWrite(LED17, mcp23017_3.digitalRead(KEY17)); mcp23017_3.digitalWrite(LED16, mcp23017_3.digitalRead(KEY16)); mcp23017_4.digitalWrite(LED23, mcp23017_4.digitalRead(KEY23)); mcp23017_4.digitalWrite(LED22, mcp23017_4.digitalRead(KEY22)); mcp23017_4.digitalWrite(LED21, mcp23017_4.digitalRead(KEY21)); mcp23017_4.digitalWrite(LED20, mcp23017_4.digitalRead(KEY20)); mcp23017_4.digitalWrite(LED28, mcp23017_4.digitalRead(KEY28)); mcp23017_4.digitalWrite(LED29, mcp23017_4.digitalRead(KEY29)); mcp23017_4.digitalWrite(LED30, mcp23017_4.digitalRead(KEY30)); mcp23017_4.digitalWrite(LED31, mcp23017_4.digitalRead(KEY31)); }