# How to get pin addresses on a MCP23017

Recently I purchased a few MCP23017. I've been reading up on them and I've gotten a few lights to turn on and such. I am having a problem trying to understand how to figure out the register address for each pin. In this one tutorial I read, they used pin 23 which is 0x14 but I cant seem to figure out how to come at that number. Seems like a lot of tutorials link to this diagram, http://hertaville.com/wp-content/uploads/2013/03/mcp23017_mm.png so can someone help me figure out how to read this. Since hex 14 is 2nd last wouldn't that make its pin 26 and not 23?

Also, I think I've gotten the daisy chaining working properly with multiple of these but just to make sure: To change the hex value of x20 I just change a0-3 to be not ground? One of my chips is getting really hot (luckily I have 10 of them) but it gets detected by the pi in i2cdetect -y 1 as x21 so I figure its correct. Could it just be a faulty chip?

Thanks guys!

## Pin numbers

Your chip has 16 I/O pins that are divided in two groups of 8 pins each, called `bank A` and `bank B`. Physical pins `1-8` of the chip are used for `bank A pins` and physical pins `21-28` are used for `bank B pins`. Rest of the physical pins (`9-20`) are used for other purposes like setting the device address, connecting power and ground, etc.

The device has quite a few registers that can be individually written/read in order to perform different operations. Two of those registers are `GPIOA` (0x12) and `GPIOB` (0x13) 8-bit registers that can be used to read/write whole `bank A` or `bank B` at a time. This means that if you read `GPIOA` register, you will get 8 bits of data and each of them will give you the value of different pin in `bank A`. The same goes for writing - if you want to write some value to any pin in `bank A`, you have to write whole `GPIOA` register setting values of all pins in bank A at once.

## How to calculate bank value

As already mentioned, each bank consists of 8 bit which corresponds to 8 pins values. Here's how the value is computed for `bank B`:

``````P8  P7  P6  P5  P4  P3  P2  P1
``````

and here is for `bank A`:

``````P28 P27 P26 P25 P24 P23 P22 P21
``````

where each of `PXX` corresponds to physical pin number XX.

So if you want to turn `P21` and `P25` on and `P22`, `P23`, `P24`, `P26`, `P27` and `P28` off, you would write value of `00010001` (in binary) which is 0x11 in hexadecimal to the `GPIOA` register.

Each of your `A0`, `A1` and `A2` pins should either be connected to ground or to `VCC`, depending on the address you want to get. You should never left it without any connection since this will make it `float`. This means for the chip it may have different state each time it is read. So if you want to have address `0x20`, you should connect all of them to ground. If you want to have `0x27`, you should connect all of them to `VCC`.

## Final notes

Please note that this is simplified explanation of how this chip works. It has different mode of operations and some more features that can be configured using chip's registers. You can read about such details in the datasheet.

• Very well said. You answered all my questions perfectly! Commented Apr 28, 2013 at 22:23