loading

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!

<p>Very good! Thank you!</p>
<p>Thank you, I was looking for this.</p>
<p>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?</p>
<p>Hello, yes you can. With serial monitor you can find analog value that arduino receive when a button is pressed. </p>
<p>What will happen if you press two buttons at once?</p>
<p>just the button that has the highest value will be considered the clicked button :D</p>
<p>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</p>
<p>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.</p><p>The program definitely doesn't need to &quot;do nothing&quot; it is very easy to just add to the program to account for a 3 ohm load and turn on LED 1 &amp; 2 accordingly.</p><p>Allowing you to press any combination of buttons at one time... even all 4.</p><p>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.</p>
<p>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</p>
<p>Yes, that's correct ;)</p>
<p>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</p>
Very clever! thanks for sharing
Yes!!! Very cool idea and blindingly simple = thanks for sharing!
<p>You can build a whole keyboard with this! :P </p>
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 ?
<p>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).</p>
<p>That's so awesome! But what will happen when you press more than one button at once?</p>
<p>Hi! Nothing special... The arduino will received one different value, so leds will remain 'off'.</p>
<p>whats awesome is that you can use as many buttons as you want! Thats awesome!</p>
Wow! Never thought of that before.

About This Instructable

7,387views

196favorites

License:

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