using MCP23017 I/O expander for inputs

flytyer232:
Hello,

I am trying to use a MCP23017 I/O expander to add the number of I/O pins that are available on my Arduino Mega 2560. i am most interested in adding inputs, to detect voltage across a sum of 160 pins, and reporting the address of the logic "high" signal.

Here is the code that i got off this tutorial is: Tutorial: Maximising your Arduino’s I/O ports with MCP23017 | tronixstuff.com

 /*

Example 41.2 - Microchip MCP23017 with Arduino
Arduino Tutorials | tronixstuff.com > chapter 41
John Boxall | CC by-sa-nc
*/
// pins 15~17 to GND, I2C bus address is 0x20
#include "Wire.h"
byte inputs=0;
void setup()
{
Serial.begin(9600);
Wire.begin(); // wake up I2C bus
}
void loop()
{
Wire.beginTransmission(0x20);
Wire.write(0x13); // set MCP23017 memory pointer to GPIOB address
Wire.endTransmission();
Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317
inputs=Wire.read(); // store the incoming byte into "inputs"
if (inputs>0) // if a button was pressed
{
Serial.println(inputs, BIN); // display the contents of the GPIOB register in binary
delay(200); // for debounce
}
}




The issue with this code is it only checks the B port, and i would like to declare the A port as a input as well. Any idea on how i would do this?

I would suggest you read the Microchips Data Sheet for the MCP23017. It will tell you how to configure the gpio pins as input, even assign pullup resistors to the inputs and how to read the current values on the gpio pins.

MCP23017 data sheet

I would set the gpio pins to input by setting register 0 = 0xff, 1= 0xff, then if you wanted pullups Set register 0x0C = 0xff, and 0x0D = 0xff. then read from 0x12 and 0x13.

Chuck.