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

58,245

216

27

## 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
• 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!

## Recommendations

17 1.8K
81 9.6K
1 188 20K
8 1.0K

• ### Game Design: Student Design Challenge

is it possible to assign 1 pinOUT for differents variables? help!!!

I want to do this with a dozen 2 pin IF Receivers/Emitters. I tried it with 2 IF pairs (in place of the normally open switches you used) and the numbers for the 3 switches were only slightly different. I used 1K, 2.2K and 22k resistors, but got values that were almost the same at each switch. How do I make this work?

Very nice work and an excellent posting. A few years ago I did something similar to create 42 inputs using a single Nano. I added RF transmission with an HC12 to make it portable. The receiver Nano I connected to my PC and a small VB program. The program speaks the selection made. Here is a short video of it in action:
https://www.49h.com/42input_demo.mp4

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

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

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.

So, if I understand correctly :
The Arduino can read values up to 1023, or 10 bits. So we could use 10 resistances, with Ohm resistance equal to each "bit" value (1,2,4,etc), and have 10 buttons on the same line. From there, we could have multiple buttons pressed at the same time and detect them all ?
Since we need some wiggle room I'd tend to drop the first few bits, and maybe start at the 4th or 5th so we can have a range of values instead of an exact value to use. That would still leave us with 6 or 7 buttons per line, that can all be pressed at the same time.

I want to test this, I'm mostly looking for people to tell me if it's a stupid idea or not. Electricity lessons in high schools were a long time ago, I'm trying to get back into it... I may miss something there.

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

Thank ! I am working on a toughbook pi and needed something to convert the keyboard. We are talking 86 keys but your example is a good start !
Come check it out; github.com/toughpi

Thank you so much!

Thank you, I was looking for this.

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?

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

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

Very clever! thanks for sharing

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