Go Down

### Topic: Number of inputs with a CD4021 limited to 8? (Read 185 times)previous topic - next topic

#### Skalpa

##### Mar 30, 2019, 10:11 pm
Hi all,

Like a lot of people, I'm interested in increasing the number of inputs into an Arduino Uno for my project (namely, hook up a children's toy with 40 different buttons : the press of button "i" triggers song "i" on an SD card).

I tried and successfully tested the method using a a parallel to serial shift register.

What I don't understand in all the explanations I've read (various tutorials and forums) is why ONE CD4021 limits the number of inputs to 8, and why you need to add another one to increase the number of inputs by 8.

If I understood correctly, with the circuits I saw, if I press a switch, it will input a 1, so I will be able to return something like 0001000. Understandably, this limits the number of inputs to 8.

But what if I use let's say 2 cables from 1 switch to the CD4021, I'll be able to return something like 00101000.

I'm assuming that in theory, I could return 255 (11111111) values from just 1 CD4021.

I am wrong somewhere? Is there an electrical problem with that? I've tried it quickly on a bread board, and it seems to work.

#### Grumpy_Mike

#1
##### Mar 30, 2019, 11:20 pm
Quote
is why ONE CD4021 limits the number of inputs to 8,
Because each input feeds into a D-Type flip flop, then the state of one flip flop is transferred to the next in the chain and eventually to the output. As you only have 8 flip flops in the chip then you are limited to 8 bits per chip.

Quote
But what if I use let's say 2 cables from 1 switch
I don't understand what you mean by this.

Quote
I'm assuming that in theory, I could return 255 (11111111) values from just 1 CD4021.
Yes you could, but what you are mixing up is bits and the combination of bits. Each one of those 0 or 1's represents one input. So your 11111111 is made up from 8 inputs that can be 0 or 1. In combination this results in 256 different patterns of zero and ones. But then you only have 8 bits.

#### Paul__B

#2
##### Mar 30, 2019, 11:45 pm
Suspect your approach is a little confused.

Firstly, you refer to a CD4021, but this is an older IC series, you should be using a 74HC4021 or in fact, a 74HC165 which may actually be more readily available.

Now what you perhaps are trying to visualise is a matrix.  Here is an incomplete one using two PCF8574 - you could just as easily use a single PCF8575 which has 16 I/O pins.

Fully implemented, this allows you to use 64 buttons and interface it via the I2C bus on just two Arduino pins shared with other I2C devices.  These are readily available as modules on eBay.

#### PaulRB

#3
##### Mar 31, 2019, 10:35 amLast Edit: Mar 31, 2019, 10:58 am by PaulRB
I'm not sure the OP is thinking of a matrix at this stage. Just encoding up to 255 buttons to 8 inputs.

@Skalpa: it can be done. But there is a problem I suspect you have not yet realised. So far, I guess you only tried to do it with a single button?

Start simple, using only 2 inputs. That should allow you to connect up to 3 buttons. Button 1 is connected to input 1 and button2 is connected to input 2. Button 3 is connected to both inputs, yes? Try it and see what happens. Or just draw it out on paper and imagine what will happen when you press each of the 3 buttons. You should very quickly see the problem.

Here's a clue. The fix involves using diodes.

#### PaulRB

#4
##### Mar 31, 2019, 11:17 amLast Edit: Mar 31, 2019, 11:29 am by PaulRB
Or...

If you can find dpst buttons you like, you can connect up to 196 buttons to 8 inputs without diodes. That's 8 connected directly to the 8 inputs (using these buttons as through they were spst) and 168 buttons connected to pairs of 2 inputs.

#### Skalpa

#5
##### Apr 12, 2019, 03:14 pm
Hi,

I'm sorry, my case and my question seem to not have been very clear, but basically, PaulRB got what I was trying to do, and answered my question : there is an electrical problem if I just connect the 20 or so switches to just one CD4021.

So I guess I'll just serialize 3 or 4 parallel to serial shift registers.

One last question : can I go on and use the CD4021 I've mentioned and tried or is there a clear advantage of using a 74HC165, mentioned by Paul__B?

#### PaulRB

#6
##### Apr 12, 2019, 04:10 pmLast Edit: Apr 12, 2019, 04:19 pm by PaulRB
What about fixing the problem with diodes, as I suggested?

For 20 switches:

8 switches connected to a single 4021 pin with no diode needed.

12 switches connected to pairs of 4021 pins with 2 diodes.

Total 24 diodes. Just small signal like in4148 should be fine.

#### Skalpa

#7
##### Apr 13, 2019, 03:42 pm
Sorry PaulRB, I did discard this solution a bit fast

I might indeed try it with that.

Thanks again!

Go Up