Number of pins, you always have less pins available on your microcontroller, then you want …
There is actually a simple solution for this, and the magic word is “I2C”.
The I2C protocol let us communicate with other “devices”, over only 2 pins !
And those specific I2C devices can be “addressed”, so you can have multiple of the same device
connected onto the same 2 pins. And if that isn’t enough, you can also daisy-chain different type of devices !
(because they al have different addresses)
Please check this wiki page for more info about I2C.
1. Multiplexer – TCA9548A – 8-channel I/O
This little device, offers 8 I/O channels. Via I2C commands you can set a channel as input or output,
and also read/set it’s value. You can use this library.
You can have 8 different addresses between 0x70 and 0x77, via bridging A0 till A2.
(so you can have 64 I/O ports)
See here how to set the addresses / see here for the corresponding Arduino library
2. Multiplexer – PCF8575 – 16-channel I/O
This little device, offers 16 I/O ports. It works the same as the multiplexer, but uses different commands.
(And of course also another library)
You can also have 8 different addresses between 0x020 and 0x27, via bridging A0 till A2.
(so you can have 64 I/O ports)
See here for the corresponding Arduino library
3. Multiplexer – PCA9685 – 16-channel PWM (ideal for servos)
And this one, has 16 PWM outputs, ideal for dimming LEDs or controlling servos.
Addressing start at 0x40, and has 62 addresses …
(so you can control a whopping 992 servos, LEDs, or any combination of outputs that need PWM)
See here for the corresponding Arduino library
4. LCD controller via I2C
You can find this module on Ebay and Ali (and any other website for that matter),
by searching for “I2C LCD”.
Normally, a character LCD, from 1×8 chars (till 4×20 chars), needs “lots” of I/O-pins,
but by attaching this little pcb, to the back of such and LCD, you can control it with 2 pins 🙂
See here for the necessary Arduino library
Of course, there are more I2C modules then what is contained in this small list,
but I hope this gives you some inspiration and a “getting started” with I2C …