Introduction: How To: Multiple Buttons on 1 Analog Pin - Arduino Tutorial

The Arduino uno board has 12 availabe digital I/O pins and 6 analog input pins, sometimes, on complex or 'big' projects, we run out of digital pins. In this tutorial you will learn how to use multiple buttons on one analog pin.

For example, if you have 4 buttons, you will need 4 digital pins to read them. But, if you connect them all together, with different resistors, you can read them from only one analog pin!

Let's see how it works

Step 1: What You Will Need

For this tutorial you will need:

  • Arduino uno
  • Breadboard
  • 4x button sw
  • 4x leds (yellow,red,blue,green)
  • 4x 330 Ohm (or 220Ohm) resistors
  • 1x 1K, 2K, 3K and 10K resistors

Step 2: How It Works

It's quite simple, every time a button is pressed, the Arduino receives an analog value from 0 to 1023. Depending resistance to each button this value changes. By this way we can recognize which button was pressed.

Now let's see how it works in practice!

Build the circuit above and program the Arduino with the following sketch. Bellow you will find the Codebender serial monitor, just press the connect button to start serial communication.


As you can see if no button is pressed the value obtained is 0. Now press the first button (with 1K resistor) and keep a notice with the value that you received. Keep going with all of them.

Mine values are:

  • 1st button: 1013
  • 2nd button:1004
  • 3rd button: 995
  • 4th button: 871

Tip: You can use as many buttons you want ;)

Step 3: The Circuit

Let's see how we can use these values to control some leds. Build the above circuit, the connections are pretty easy.

Step 4: The Code

Here's the code, embedded using Codebender!

You can make your own modifications to the code by clicking the "Edit" button.

Tip: Don't use absolute values, for example: instead 1013, use 1010 to 1015 ;)


Step 5: Well Done!

You have successfully completed one more Arduino "How to" tutorial and you learned how to use multiple buttons on only one analog pin!

I hope you liked this, let me know in the comments.
There will be more of them, so make sure to click Follow button!

Comments

author
portoinfo (author)2016-06-05

Very good! Thank you!

author
kenyer (author)2016-05-24

Thank you, I was looking for this.

author
dbarrerap (author)2015-08-07

Just before I start, why does it have to be those high resistance values? can it be done with lower values , such as 220, 330, 470? What does the value in the Serial Monitor represent?

author
codebender_cc (author)dbarrerap2015-08-15

Hello, yes you can. With serial monitor you can find analog value that arduino receive when a button is pressed.

author
CamelXP (author)2015-07-02

What will happen if you press two buttons at once?

author
selftouh (author)CamelXP2015-07-02

just the button that has the highest value will be considered the clicked button :D

author
ziggi231. (author)selftouh2015-07-04

Sadly, no. Ohms law doesn't work this way. If you press buttons with 2 and 4 ohms the parallel resistance will be 1/(1/2+1/4) wich is equal to 1.(3) ohms. so the program will do nothing as the highest resistance that is coded is 1015 ohms

author
meliudaj (author)ziggi231.2015-07-07

You are mostly correct... While yes, the parallel resistance of pressing the 2 ohm and 4 ohm button simultaneously would create a 3 ohm resistance.

The program definitely doesn't need to "do nothing" it is very easy to just add to the program to account for a 3 ohm load and turn on LED 1 & 2 accordingly.

Allowing you to press any combination of buttons at one time... even all 4.

This is actually a common practice, I just tore apart the control panel on a low end ink jet printer a few weeks back that had 3 buttons hooked up exactly like this.

author
cybericius (author)meliudaj2015-07-08

No, it is really 1.3 Ohm. The parallel resistance is always lower than the smallest member as the current can share paths. 1/(1/2+1/4)=1/(0.5+0.25)=1/0.75=1.3

author
codebender_cc (author)meliudaj2015-07-08

Yes, that's correct ;)

author
grayl (author)2015-07-05

For a more accurate distinction between buttons, or a more accurate way of capturing multi-buttons, you may want to consider using an R-2R tree. See https://en.wikipedia.org/wiki/Resistor_ladder

author
Douglas N (author)2015-07-02

Very clever! thanks for sharing

author
mike_skinner (author)2015-07-01

Yes!!! Very cool idea and blindingly simple = thanks for sharing!

author

You can build a whole keyboard with this! :P

author
phzietsman (author)2015-07-01

This is a very handy trick! Try swapping out the 3k with a 5k, then you would be able to pickup combinations as well... I think ?

author
codebender_cc (author)phzietsman2015-07-02

Hi! You can use different resistors if you want. For example, you can use 1K, 5K, 10K, 18K ;) You just need to find out the analog value that Arduino received every time you press the button through serial monitor (step 2).

author
Kesselwagen (author)2015-07-02

That's so awesome! But what will happen when you press more than one button at once?

author

Hi! Nothing special... The arduino will received one different value, so leds will remain 'off'.

author
Sarvasv Kulpati (author)2015-07-01

whats awesome is that you can use as many buttons as you want! Thats awesome!

author
Saiyam (author)2015-07-01

Wow! Never thought of that before.

About This Instructable

7,620views

196favorites

License:

Bio: Arduino Tutorials by Codebender.cc Team
More by codebender_cc:How to use a magnetic door switch / sensor with ArduinoHow to Measure LUX With ArduinoHow to Control an RGB LED Strip - Arduino Tutorial
Add instructable to: