Wire.beginTransmission(0x40);
The 7-bit address of a standard MCP23016 (all address pins grounded) is 0x20 and not 0x40.