You can add a GPIO expander to the NodeMCU for $2.
The other day, I was writing about NodeMCU GPIO pin limitations. Since then, I have uncovered an easy way to create many more GPIO pins for this device for only a couple of dollars. The solution is a PCF8574 IO Expansion Board I/O Expander available on EBay and elsewhere. I have ordered a few.
This type of expansion board uses the I2C bus, which is a two wire serial data bus. I2C or Inter-Integrated Circuit Bus. The I2C bus was originally designed for short data communications between chips on a circuit board. It is now mostly used for short distance serial communications between a microprocessor and a peripheral. It runs fairly fast and only needs two pins – one for clock, one for data. And, since it is addressable, you can daisy chain I2C peripherals on the same bus (same to pin connections) and set different addresses for each.
I2C GPIO Expander
In short, you can connect the GPIO expander to two pins on the NodeMCU. In turn, the GPIO expander will provide you with 8 additional GPIO. So, the net gain is six more pins. Although, if you are using some of the pins for input, you will also need another NodeMCU pin to read interrupts from the GPIO expander. These interrupts will tell your program when data is available to be read as an input (high or low).
If I daisy chain two of these expanders together, I will be able to get a net gain of 12 pins (16 – two wire I2C – two interrupts.) As far as I know, the NodeMCU Arduino core implements I2C in software, so I will have my choice of pins for I2C implementation.
This type of port expansion first became popular a few years ago for interfacing the LCD1602 display. This LCD display uses 8 bits of parallel data for writing to the display. In turn, this hogged 8 digital pins on the Arduino. As a result, several makers started providing a piggyback board from running this popular display on I2C rather than 8 Arduino pins.
Finally, if you want to roll your own, there are various I2C port expander integrated circuits available.