4051 button wiring

Hi everybody, question from a newbie.

I've built a circuit for connecting 8 buttons to my Arduino via a 4051 multiplexer. The code and the circuit work, but I'm not sure if the circuit I've built is the most efficient way of doing it.

Attached is a schematic of my circuit.

My main questions are:

Are the 100k resistors neccesary?
Should I be using a pull down resistor, or would a pull up be better?
Do I need a pull down for every button, or just one on the 4051 (and if so, where?)
Do I need a resistor on the ground of the 4051?

Thanks in advance!

Which is somewhat surprising in itself.

I note your previous discussion in another area. Whilst this is not quite "cross-posting" as such, which is anathema to the moderation of these discussions, it is generally unproductive to on the one hand, ask for advice on different aspects of the same project in different places, and on the other, to fail to give essential information such as the circuit diagram you have just posted, in each and every such question.

Well, it isn't.

Hard to say, since you show no 100k resistors in the diagram. :astonished:

FAQ. A pull-up is almost always preferable since it avoids wiring the supply voltage itself out to pushbuttons, and especially if those pushbuttons are remote from the PCB, which always risks various consequences of an accidental short from the Vcc to ground - or to some other power source. Having the pushbuttons connect to ground (and therefore using a pull-up) means that the worst that can generally happen from a short circuit is a "cipher" with the button appearing to be pressed when it is not.

Well, since the buttons to which the 4051 (and we are I hope talking about a 74HC4051 rather than a CD4051) has not selected at any given moment, are entirely irrelevant at that time, there is no need to connect a pull-up to them. So if you use a pull-up on the common ("Z") terminal of the 4051, it will act on whichever button is currently selected - the only time you need it to.

The Arduino however provides a built-in pull-up function by setting "pinMode(2,INPUT_PULLUP);" (if in this case, 2 is the pin in question), so you generally need no pull-up at all - you use the Arduino itself.

And - there is no reason to use an analog input for this, though they are perfectly functional as digital I/O.

Question is, if it is called the "ground" on the 4051 pinout, why would you not - connect it to ground as specified? What makes you think a resistor should be placed between this ground terminal, and ground?

Only because you are using CMOS which has high impedances and negligible static current draw, does placing such a resistor here actually permit the circuit to function at all.


Now as to your previous questions about debouncing. In order to poll and debounce eight inputs, the proper way is on each pass through the master loop where you have determined that a new millisecond has since passed, to step through each index of the multiplexer, shifting in sequence the corresponding pushbutton state into successive bits of a byte value which then represents the combined value of the eight buttons. You then compare this value to the "previous" state which you have noted; if it is the same then you reset the debounce count variable to its maximum - say, 20 - and complete this section of code (back into the main loop). If the (byte) value just read is not the same, decrement the debounce counter and see whether it is zero or not. If it is not zero, this means that the value has not been stable for a full debounce period (20 milliseconds) so you simply complete this section of code. Finally, if the debounce counter is zero, then the current value clearly has been stable for a full debounce period so you then compare it to the "previous" or "last stable" state that you have noted (in a variable). Where it differs from the last stable state then you conclude that this is a new stable state which should be acted upon, so you examine and act upon each bit which differs from that last state, re-writing the "previous" state to match the bit acted upon until the "last" state now matches the current state and you move on from there.

I suppose I should code this algorithm into a generic reply to these questions. :slight_smile: Note that there may be different possible valid sequences of the steps involved.

The importance of this is that it debounces eight (extensible to essentially any multiple of eight) inputs at a time, such as the code I wrote to service the keyboard of the Tandy TRS-80 "color computer" back in the 1980s. :wink:

Thanks for your reply. I wasn't sure on the protocol for this sort of thing, but since it was an unrelated (somewhat) question I thought I'd ask it in the multiplexing section, as it seemed more relevant.

My apologies, I meant to type 100 ohm resistors.

I'm using a CD4051BE - it was just what I had to hand. What is the difference? At this stage I'm just prototyping, so if a 74HC4051 is better then I'll order some of those for the finished product.

I've rewired the circuit to use a pull up. I'm using external ones because my board does not have internal pull-up resistors (Teensy 3).

Find attached my updated schematic.

Not to worry, but the more complete your description of the situation, and the more people can get a "grip" on all the matters involved, the better the information that can be provided, which is why I caution against spreading it over different areas (which not everyone covers). I only note the prior questions because I chased through your "history".

Oh, them! Not strictly necessary, though we have had a few discussions about whether a series resistor is desirable to protect against accidental fault voltages occurring on external wiring such as to the pushbuttons, from being applied to the Arduino, or in this case, the 4051.

OK, the difference is that the 74HC4051 has a lower "on" resistance which may or may not be beneficial in this case - the difference actually is of a similar order of magnitude to your 100 ohm series resistor! If your pull-up is 10k, then just about any combined series resistance up to 1k will still allow the inputs to be positively brought low, so it is unlikely to be a problem.

The other difference is that the 74HC4051 is specified for 5V Vcc, essentially for digital logic while the CD4051BE operates up to about 15V, which is very useful if you are really going to use it as an analog multiplexer with op-amps and such.

The internal pull-ups are a function of the ATmega chip itself, so I am pretty sure the Teensy has that function. You can check up on that.

You have a little blunder on that last circuit in the top right corner.

Why are you using a analogue input for a digital signal?

If you must put in a 100R seriese resistor then just have one between Z and A0.

Paul__B:
Oh, them! Not strictly necessary, though we have had a few discussions about whether a series resistor is desirable to protect against accidental fault voltages occurring on external wiring such as to the pushbuttons, from being applied to the Arduino, or in this case, the 4051.

I'll leave them as is - better to spend a couple of quid on resistors than to have to replace a whole IC if the poop hits the blower.

Paul__B:
OK, the difference is that the 74HC4051 has a lower "on" resistance which may or may not be beneficial in this case - the difference actually is of a similar order of magnitude to your 100 ohm series resistor! If your pull-up is 10k, then just about any combined series resistance up to 1k will still allow the inputs to be positively brought low, so it is unlikely to be a problem.

The other difference is that the 74HC4051 is specified for 5V Vcc, essentially for digital logic while the CD4051BE operates up to about 15V, which is very useful if you are really going to use it as an analog multiplexer with op-amps and such.

That's good to know. I've got a few of the CD4051BEs lying about, and if they work okay I'll continue to use them - at least while prototyping

Paul__B:
The internal pull-ups are a function of the ATmega chip itself, so I am pretty sure the Teensy has that function. You can check up on that.

You have a little blunder on that last circuit in the top right corner.

The Teensy 3 uses a Freescale Kinetis ARM-based MCU, and there are no pullups in there. The teensy website was pretty explicit about that. If I switch to a standard MIDI implementation rather than USB MIDI I'll be using a standalone ATMega, and in that case I would use the internals.

What's the mistake? Is it the number on the switch?

Thanks again for your help. I know us newbies can be frustrating.

I was cautious about that. "mrburnette" is the enthusiast and expert in those; I have none so am in no position to stick my neck out.

You have the puhbutton apparently connected to Vcc.

Not necessarily. As long as we get there in the end. :smiley: