12
\$\begingroup\$

I'm doing some project and I'm using Arduino to prototype, I have to use 10 pushbuttons (along with more things) and I don't have enough pins.

One solution I could think of is to use the analog pins and use each of them for two push buttons, something like this:

schematic

simulate this circuit – Schematic created using CircuitLab

That way I can read the pin A0 and know which of them is being pushed by looking if the voltage is 5V or half of that.

Is this a good idea? The different push buttons are NEVER supposed to be pushed at the same time, which is the only problem I can think off.

Are there better ways?

\$\endgroup\$
4
  • \$\begingroup\$ How many pins are you trying to use? \$\endgroup\$
    – W5VO
    Jul 12, 2013 at 16:39
  • \$\begingroup\$ @W5VO I would need 22, with 10 buttons, the board only has 18... so two buttons for each analog pin I only need 5 analog pins and the board has 6. \$\endgroup\$ Jul 12, 2013 at 17:04
  • 1
    \$\begingroup\$ Just FYI, many automobile manufacturers use the same approach for steering wheel-mounted controls for the audio system. They have (give-or-take) 8 buttons, each with its own resistor. This allows them to run a single wire (or pair of wires to guarantee a clean return) from the wheel to the audio head. It is a perfectly good approach. \$\endgroup\$
    – DoxyLover
    Jul 12, 2013 at 19:03
  • \$\begingroup\$ I believe you can actually use three pins to input theoretically an infinite number of switches using some shift registers. It is also possible to use 8 pins to input 256 switches using multiplexers. \$\endgroup\$
    – Alvin Wong
    Jul 13, 2013 at 3:42

5 Answers 5

22
\$\begingroup\$

Why waste multiple analog pins for two switches each, when you could do any number of buttons on a single analog pin?

Two ways of doing it. One is in series, the other is parallel.

enter image description here

enter image description here

This is how some car steering wheel audio controls are. And how some of the older ipod inline controllers work.

Depending on the resistors you use, if you need multiple buttons pressed at the same time, and how sensitive your analog in is, you could have all 10 buttons on a single pin.

\$\endgroup\$
6
  • \$\begingroup\$ Thank you, I also thought about this, but also thought that maybe the voltage readings weren't as exactly as I need them to be to be able to rely on this method. I will try this as well and see how it works. \$\endgroup\$ Jul 12, 2013 at 20:22
  • \$\begingroup\$ Since my resistor network analysis fundamentals are not quite solid, I tried using CircuitLab to simulate the series circuit, and draw out a truth-table, for the voltage measure on the analog input pin. I found that, the combinations B2+B3 result in 2.499V and the combinations B1+B4 result in 2.498V. With such close figures, I think the typical 10-bit ADC won't be able to differentiate the multi-input combinations correctly. What do you say ? Of course, one key at a time, would work perfectly -- no doubt. Note B1=Button1. \$\endgroup\$
    – bdutta74
    Jul 13, 2013 at 9:15
  • \$\begingroup\$ Actually, B2+B4 gives 1.666V, while B2+B3+B4 also gives 1.666V ! The solution might be using different values of resistors for R1, R2, R3, R4 and not keep all of them of 1K. What say ? \$\endgroup\$
    – bdutta74
    Jul 13, 2013 at 9:22
  • 7
    \$\begingroup\$ A 2R/R ladder w old turn the buttons into a D/A, and every combo of button presses would have a unique value. I'd trust it to work with N-3 buttons, where N is the ADC resolution \$\endgroup\$ Jul 13, 2013 at 11:05
  • 1
    \$\begingroup\$ @icarus74 yes, using different values instead of all 1k, would give unique combinations. Try 1k, 2k, 4k, 8k, 16k, etc (Or 1k, 4k, 16k, etc, for a wider range) \$\endgroup\$
    – Passerby
    Jul 13, 2013 at 18:48
10
\$\begingroup\$

That would work, but a better way is with a matrix. This is the same concept as multiplexing LEDs with a matrix, but with switches.

enter image description here

This is a 2x2 matrix. A useful matrix is bigger, because at this size, you aren't saving any pins over connecting the switches individually.

With a 3x3 matrix you can get 9 switches. You need 10, so you can either add one more row or one more column and support 12 switches, or just put the 10th switch on its own pin.

The advantage here is that you can use digital IO, which is cheaper and usually more plentiful than analog IO. A shift register is a cheap way to add more digital IO, if you run out.

If you want even fewer pins, you can, for some increase in complexity, use charlieplexing. You will have to add diodes in addition to your switches, and these diodes probably cost as much as a shift register. However, if cost isn't your main concern then it may have some advantage. With this method, you could read all your switches (up to 12, actually) with four pins.

\$\endgroup\$
3
  • \$\begingroup\$ Just a question. Why is this better if I will need more pins? This way I will need 6 pins for the 9x9 matrix plus one more pin for the tenth, while in what I made, I would need only 5 pins. \$\endgroup\$ Jul 12, 2013 at 18:52
  • \$\begingroup\$ @MyUserIsThis this scales better as the total number of switches goes up. ex a 5x5 matrix lets you connect 25 switches to 10 pins. As a more general comment, doing things the conventional way both makes it easier for anyone else to understand what you've done; and for you to recognize what other peoples circuits do (because you've trained yourself on the standard implementation). \$\endgroup\$ Jul 12, 2013 at 21:16
  • \$\begingroup\$ @MyUserIsThis see edits. Passerby has a good answer, also. \$\endgroup\$
    – Phil Frost
    Jul 12, 2013 at 21:43
2
\$\begingroup\$

The analog input is a valid approach, you should be able to have several buttons on it. I would also like to suggest using an IO Expander chip like the MCP23017. It has 16 pins that can be inputs are outputs and its controlled with I2C using two pins. Adafruit has an Arduino library for it.

\$\endgroup\$
2
\$\begingroup\$

Yes, that seems like a good idea, but anyways I'm going to propose an alternative that just came to my mind.

If you have access to logic gates you could map many n buttons to ceil(log2(n + 1)) pins through boolean logic. As an example, if you have 4 buttons, but only 2 pins you can create a configuration like this:

Buttons | Pins
----    | --
0123    | 01
----    | --
0001    | 00
0010    | 01
0100    | 10
1000    | 11

That is, button0 pressed should have pin0 and pin1 low; button1, pin0 low and pin1 high; button2, pin0 high an pin1 low; and button3, pin0 and pin1 high.

From this the following boolean expressions would arise

pin0 = button2 OR button3
pin1 = button1 OR button3

A mapping of 4 buttons to 2 pins could thus be realized with only 2 OR gates. You would need an additional pin and some additional logic, though, for indicating whether any button is being pressed at all.

Of course, there will still be problems if several buttons are pressed simultaneously.

Also, if you will still be going with the resistor approach, consider using larger values on the resistors other that 100 ohms since 5 V through resistors on the order of 100s of ohms would yield a current on the order of 10s of mA which is kind of unnecessarily high. I guess more reasonable values would be 10k Ohm or 47k Ohm.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for your approach. I also thought about something like that, but I don't have logic gates and I have tons of resistors. And yes I usually use 10k resistors for this, 100 ohms was just the default value of the circuit editor. Thank you again for your help. \$\endgroup\$ Jul 12, 2013 at 17:05
  • \$\begingroup\$ I think I will finally use the resistor approach, as I won't have to buy logic gates, plus your logic approach saves me only 1 pin and I don't need that. I will have this in mind though for bigger projects. Thank you very much. \$\endgroup\$ Jul 12, 2013 at 17:07
  • \$\begingroup\$ Okay! Good luck with your project! \$\endgroup\$
    – nijoakim
    Jul 12, 2013 at 17:11
0
\$\begingroup\$

The following page may be useful:

http://txapuzas.blogspot.co.uk/2010/07/papertecladoanalogico-varios-pulsadores.html

It's written in Spanish, but the esentials are diagramed, I think that it's a very good idea for you.

On this video you can see the final result, the sketch is on the page also, encapsulated function to make easy the implementation on your projects

Youtube video: 10 keys keyboard on a single pin

Here is a schematic from the above site:

enter image description here

enter image description here

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Please add a summary of what the link contains in case it ever dies, especially when it's in Spanish and this is an English language site. \$\endgroup\$
    – PeterJ
    Mar 24, 2014 at 12:24
  • \$\begingroup\$ Sorry, may be the normally situation that in Spain it's normal to refer an English publication... But the link it's very interesting, says how to construct a 10 keys keyboard using only one pin with photo description step-by-step, the diagrams to make a board and a image to make the cover of the buttons. Also the code to manage the keyboard, easily understood. Nice developped I think. \$\endgroup\$ Mar 26, 2014 at 8:27
  • \$\begingroup\$ It's not so much just the Spanish side of things, I agree the diagram is clear, looks good and I can work out what it's about just based on that. But Stack Exchange discourages link only answers because they aren't useful if the link dies, imagine if that blog is closed / deleted tomorrow how useful the answer would be. Maybe you could include the schematic and a sentence or two about it and just leave the link as a reference for further information. \$\endgroup\$
    – PeterJ
    Mar 26, 2014 at 8:37
  • \$\begingroup\$ I'm a rookie in this forum, how I can make that you tell me? Some section in special...? Thanks for your time \$\endgroup\$ Mar 26, 2014 at 9:57
  • \$\begingroup\$ I've just included the schematic and the reference where it came from but it might be also worth translating a bit more of the page to give some context how it works and what the ADC readings will be. \$\endgroup\$
    – PeterJ
    Mar 26, 2014 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.