MCP23017 I2C expander code help

Hello!

I've been trying to understand I2C lately and I figured a MCP23017 would be a very simple chip to do it with. I've been following the code here

// pins 15~17 to GND, I2C bus address is 0x20

#include "Wire.h"

void setup()
{
  Wire.begin(); // wake up I2C bus

  // setup addressing style
  Wire.beginTransmission(0x20);
  Wire.write(0x12);
  Wire.write(0x20); // use table 1.4 addressing
  Wire.endTransmission();

  // set I/O pins to outputs
  Wire.beginTransmission(0x20);
  Wire.write(0x00); // IODIRA register
  Wire.write(0x00); // set all of bank A to outputs
  Wire.write(0x00); // set all of bank B to outputs
  Wire.endTransmission();
}

void binaryCount()
{
  for (byte a=0; a<256; a++)
  {
    Wire.beginTransmission(0x20);
    Wire.write(0x12); // GPIOA
    Wire.write(a);    // bank A
    Wire.write(a);    // bank B
    Wire.endTransmission();
    delay(100);
  }
}

void loop()
{
  binaryCount();
  delay(500);
}

I found online.

The main thing is that I can't figure out the 0x20 register anywhere on the datasheet. I just don't get this whole thing. Can somebody explain?

The 0x20 is the address of the chip when all three address lines are pulled low.

Using the address lines you can select a chip address of anything between 0x20 and 0x27.

This address is the device address of the chip on the I²C bus.

You start an I²C transaction with a device (0x20), send the data, then finish the transaction.

It is on page 8 of the data sheet. Note the arduino does not include the read / write as part of the address so that bit pattern is shifted one place to the right.