Question on multiplexing-demultiplexing with a 4051

I have an Arduino set-up in which I need a few more pins than the Arduino Leonardo can provide. Specifically, I have three digital pins available, but require 5 digital pins. Of those 5 pins, three will be used as digital inputs for switches, and two will be used as digital outputs for LEDs.

I'm not particularly familiar with muxing-demuxing. Is it possible to use a single 4051 as both digital input and digital output? Or can it only be used either as digital input or as digital output?

I also have a CD4021BE available...perhaps this would be a better choice?

Arduino-x: Is it possible to use a single 4051 as both digital input and digital output? Or can it only be used either as digital input or as digital output?

It will work both ways.

[quote author=Runaway Pancake link=msg=2093474 date=1424018056] It will work both ways. [/quote] But you would be a fool to use it for outputs because you need to rapidly multiplex to keep it from appearing that the LEDs are flashing. If you require extra pins then use a port expander or shift register.

Grumpy_Mike: But you would be a fool to use it for outputs because you need to rapidly multiplex to keep it from appearing that the LEDs are flashing.

Grumpy Mike, I'm glad you mentioned the flashing problem.

If I re-worked my circuit, I could organize it such that the shift register would handle only digital outputs (specifically LEDs). Would the 4021 shift register that I mentioned in my original post be a suitable choice? Or would you recommend something else?

The 4021 has only three digital outputs. These are not latched so when you use them you can get flicker as the outputs change. If that is acceptable then fine.

Hi,

I have three digital pins available, but require 5 digital pins

Look for simpler solutions first:

Did you know most analog inputs can be used as digital inputs/outputs? Do you have any of those unused?

What are you using all the other Arduino pins for? It may be easier to free up two of those than adding extra chips.

Paul