Hacking together 5 buttons on one analog pin

Hello,

I made a mistake and forgot to buy 5 units of pre-made buttons, where each button causes a different reading on an analog pin.
I am not able to order them now... so I am trying to put one together.

I found schematics around (some on this forum) but I am struggling to get the basics going.

For example this one:

But... I have some basic questions. I am trying to get a 2 button setup to work first, and failing...

If I take the 5V output from Arduino, put a 10000 ohm resistor on it, and then feed it to an analog input, what am I supposed to get? I keep on getting 1023, and it's driving me insane.

I am also doing pinMode(A0, INPUT_PULLUP); but I STILL get fluctuating data..
Help...?

Merc.

Please grab a pen, papper, make a foto and post it. Your words don't give a clear picture.

What You want to do has been done plenty of times. There's even a commersially sold shield having both 6 buttons and an LCD.
The trick is using a ladder of resistors and letting the buttons tap the ladder ai different points.

1 Like

With resistor values chosen carefully, and wired differently than @Railroader is suggesting, I've managed up to 22 buttons on one 2 wire 'string' with a small custom reading routine, which includes a lookup table of 22 byte values. With one caveat. No two-or-more simultaneous presses allowed. Other than that, works nicely.

3 Likes

I need some guidance... I am getting really confused by very basic stuff, it's frustrating :frowning:
I want to actually understand this image:

Taken from here.

If all of the switches are open, the flow will go from the 5V to A0, going through R1. My understandsing is that the pin will get the "full voltage".

Now let's go with U1 (the first switch) getting closed. What do I see here? The flow is 5V to R1, but ALSO through GND. Even here I get totally lost... Current will choose the path of less resistance, so none of it will actually go through R1, but will instead... or maybe...

No, I think I am too stupid -- or too tired -- or both -- for this.

Merc.

Haha. We all get to that point in the end of the day.

Just for You getting on. It's not optimal but hopefully You get the picture.
One tip: Make a test code reading the analog input and printing it on serial monitor, for each button. Take down the button values.
Then create intervals for each button value. If an analog value is within the interval for one special button, You know it's pressed.

That´s an optimized design and fully possible practicing Ohms Law and selecting resistors well. But likely a bit too advanced for beginners.

2 Likes

This is another, similar, one:

From here.

So when no buttons are pressed, it's Vcc through the 5 resistors and to ground. No analog pin involved.

When button1 is pressed, Vcc will actually reach A0. Voltage divided between R1 AND R2+R3+R4+R5.

When button 2 is pressed, Vcc will again reach A0,, but voltage divided between R1+R2 AND R3+R4+R5...

is this it...?

No one has mentioned that the buttons and resistors are used to make voltage dividers.

A voltage divider will give you a certain voltage between 0 and full Vcc 5 volts or 1023.

As suggested just print the values of analog read as you press one, then anointed button. Each button should report a different value; the value may fluctuate a bit but never be close to one of the other button's reported value.

Use those values to deduce which switch was closed.

This instructabke article gives some explanation. At a glance it doesn't look too bad, at least it should help you ask more questions.

The internets are full of

    resistor voltage divider multiple buttons analog input

hits. Try a few.

This just in

Yes, you are in track. Do the thing where you try a circuit like that and get some real numbers out of the circuit.

a7

1 Like

The first point. Yes that is how it should be. There is no voltage divider, so vcc is read att A0 and A0 reads 1023.

The second: ig nothing else is connected you should read a fairly vonstsnt 1023. If a resistor to ground is conected you will end somewhere inbetween 0 snd 1023.

If you touch the pin with your hand you may observe some fluctuation. Because your body will work as antennna and may serve as a variable resistor to gnd.

Yes, that is it!

You can also use this library: Arduino - Analog Keypad Library | Arduino Tutorial