EXPANDING GPIO CAPACITY

The ESP8266 has seventeen GPIO pins numbered zero to sixteen. However, not all these are exposed on breakout boards because the ESP8266 uses some of them for its own purposes. Further, some pins have limitations that may affect how you can you them. As a consequence of this, you may find that you run out of pins. A summary is below.

GPIO numberingPhysical numberingNotes and Limitations
GPIO0D3GPIO0 is pulled high during normal operation.
GPIO1TX
GPIO2D4Connected to the on-board LED. GPIO2 cannot be low at boot time so be careful what you connect to it.
GPIO3RX
GPIO4D2
GPIO5D1
GPIO6Used internally to connect the flash memory chip.
GPIO7Used internally to connect the flash memory chip.
GPIO8Used internally to connect the flash memory chip.
GPIO9S2Used internally to connect the flash memory chip.
GPIO10S3Used internally to connect the flash memory chip.
GPIO11Used internally to connect the flash memory chip.
GPIO12D6
GPIO13D7
GPIO14D5
GPIO15D8GPIO15 is always pulled low so you can't use the internal pull-up resistor. Bear this in mind when using it as an input.
GPIO16D0

Microchip MCP23017 If you need more pins that the 6-9 that are likely to be available you can connect a Microchip MCP23017 16-Bit I/O Expander with I2C interface.

The MCP23017 has two banks of 8 GPIOs; each pin can sink or source up to 25mA subject to a chip limit of 150mA sink on VSS and 125mA source on VDD. It can operate on voltages between 1.8V and 5.5V. Its I2C base address is 0x20 but this can be changed by connecting combinations of A0, A1 and A2 to 3.3V enabling up to eight devices to be used on a single I2C bus.

The RESET pin on the MCP23017 must be tied to VDD using a 10kΩ resistor. If you do not have this in place the chip may stop working or exhibit strange behaviour.

Exclamation mark You must ensure that each I2C address pin is provided with an external bias and is tied to either ground or 3.3V. This is unlike some other devices where you can use the default address by just leaving the pins unconnected. If you leave any address pin unbiased the address will float and your program will not work reliably.

When you define the base starting address for the 16 pins, the pins will be assigned sequential addresses that map to GPA0 to GPA7 then GPB0 to GBP7. So, for example, GPB1 will have an address that is base+9. Also note that the A and B banks are not organised as you might expect with regard to pin numbers

Microchip MCP23017 address structure

Link to source code. You'll probablyneed to add the MCP23017 library to your IDE. Go to Sketch -> Include library -> Manage libraries and then search for MCP23017; select the Adafruit library and then click Install.

Here is the wiring layout. The example has a push-button on GPA0, which is pulled high by the software. The push-button connects to ground. The LED is connected to GPB0 and is in series with a 330Ω resistor. The program flashes the LED on and off; pressing the button pauses the sequence until it's released.

Wiring layout

The MCP23017 is similar to the PCF8574 that is found on the I2C backpack on LCDs. The PCF8574 has only 8-bit capacity whereas the MCP23017 is 16-bit.

Do this with an Raspberry Pi or an Arduino.