The most commonly used Arduino board, the Arduino Uno, only has 12 available digital IO pins, so you may find yourself quickly running out of available pins on larger projects or projects requiring a number of buttons or a keypad. In this tutorial we’re going to be looking at how you can set up a large number of buttons to run on a single Arduino analog input, using resistors to differentiate between buttons. With this method, you should be able to reliably connect up to 50 push buttons to a single Arduino analog input. We’ve also been able to connect a standard 4×4 keypad to a single Arduino analogue input.

To improve your understanding of this concept, we’re going to be going through the connection of a single push button to your Arduino, then the usual connection of multiple buttons to their own digital IO pins and finally multiple buttons to a single analog pin.

This project assumes you know the basics of Arduino programming, otherwise read our article on getting started with Arduino.

Here is the video guide for Method 2 below, if you don’t want to read through the guide.

What You’ll Need For This Project

  • Arduino (Uno Used Here) – Buy Here
  • Breadboard – Buy Here
  • 5 Push Buttons (Or As Many As Required) – Buy Here
  • A Selection of Resistors (1K, 2K, 3K, 5K and 10K Used For Method 1) – Buy Here
  • A Selection of Resistors (220 Ohms Used For Method 2) – Buy Here
  • Another Resistor With Higher Resistance Than The Highest Used Above (100K Used Here)

Connect A Single Push Button to A Digital Input Pin

To start off, lets look at connecting a single push button to a single digital input pin, Pin 7, on your Arduino and use it to turn off the built in LED on Pin 13.

Connect your push button and 2K resistor as shown in the breadboard diagram below.

Single Push Button On Single Arduino Input

Now lets have a look at the code required to turn the LED on Pin 13 off when the button is pushed.

As you can see from the above connection diagram and code, it is quite simple to connect a single push button and use it to drive other IO or settings in the code on your Arduino. 

Connect Multiple Push Buttons To Multiple Digital IO Pins

The problem comes in when you need to use multiple buttons. A 10 digit keypad for example uses 12 inputs, 10 digits from 0 to 9 and then a further two for the * and # keys. Let have a look at how we’d connect 5 push buttons using the standard digital IO method.

If you’re specifically looking to connect a 4×4 keypad to a single Arduino input, take a look at the linked guide as they are typically wired a little differently to the below example and you’ll need to make some adjustments to your circuit.

Try connecting up 5 push buttons and 5 2K resistors each connected to a separate IO pin on your Arduino as shown in the breadboard diagram below.

Multiple Push Buttons On Multiple Arduino Inputs

Let’s now have a look at the code required to turn off the LED on Pin 13 if any of the push buttons are pushed. I have created a function which runs through the input checking code to avoid code duplication, follow this link if you’d like to read up more on using functions to simplify your Arduino code and make it more efficient.

Now, when you push any of the buttons, your LED should turn off.

Connect Multiple Push Buttons To A Single Analog Input

Now that we’ve looked at how we would typically connect up multiple buttons to multiple Arduino inputs, lets see how we can make the wiring, code and use of inputs more efficient by making use of a single analog input to drive inputs from multiple push buttons. We’ll again be using 5 push buttons in this example but you can reliably connect up to 50 push buttons to a single Arduino analog input.

Method 1

The concept used in this example is quite simple. We assign a different value resistor to each push button so that the analog input will receive a different value (between 0 and 1023) for each button, allowing us to recognise which button has been pressed.

Let’s have a look at the breadboard connection diagram.

Multiple Push Buttons On A Single Arduino Input

We connect a single 100K resistor between GND and the analog input, with the analog input also connected to one leg of all of the push buttons. We then connect different resistors between the 5V and the second leg of each of the push buttons. It is important that each of these resistors are different values so that you get different voltages on the analog input, enabling you to identify each button.

You can already see that the connections required to the Arduino are much simpler in this example than in the previous one.

Now lets have a look at how we write the code to differentiate between the push buttons.

You can already see that the code is much simpler using the analog input as you are not required to define and set up your IO for analog pins. The statements required to check which button is pressed are slightly more complicated than with digital IO.

Essentially your Arduino will read a value between 0 and 1023 on the analog pin and will need to compare this value to a particular range in order to identify which button has been pressed. We use a range rather than an absolute value because the analog input and resistors are very sensitive to external influences such as temperature. A value may be right on the limit between 1012 and 1013 so it will be measured as 1012 when the resistor or room temperature is cold but 1013 when it is warmer.

You will need to set up the range in your code for each push button and the values you use can be worked out in three different ways.

  1. Before writing your actual code, write a simple program to use the serial monitor to display the value read on the analog input for each different button press. Record these values and add a bit (5 to 20) on either side of each value to get your overall range for each button.
  2. Measure the voltage put on to your analog input when each button is pressed and scale this voltage to a number between 0 and 1023. For example, if you measure 4.75V on your input, 4.75 / 5 * 1023 = 972. Add a little above and below the number and your range could be 965 to 980.
  3. Calculate the voltage put onto your analog input by each push button using the known resistance values of your one overall resistor and each individual resistor. Map this voltage to an analog input range as done in option 2.

Obviously the greater the number of buttons you use the smaller your range for each button.

Method 2

The second method is quite similar to the first, with the difference being that you can use a number of the same resistors to make up the circuit. This is useful if you have a pack of similar resistors, such as the 220 ohm ones commonly used for LEDs.

You’re going to connect these up in series to create a voltage divider, for which each pushbutton brings up another resistor in the series connection, allowing you to differentiate between them. This is the method shown in the video in the introduction, so it may be worth watching for more information.

Let’s have a look at the breadboard connection diagram.

Buttons On One Arduino Input

Now lets have a look at how we write the code to differentiate between the push buttons.

You now know how to connect all of your push buttons to a single analog input and free up your IO for larger projects. I hope that you enjoyed this tutorial, let me know how it went for you in the comments section below.

Share This Tutorial

How To Successfully Connect Up To 50 Push Buttons To A Single Arduino Input

6 Replies to “Multiple Push Buttons on One Arduino Input”

    1. If you calculate your resistance values so that any combination of buttons being pressed at the same time does not provide the same resistance as any individual button then you can differentiate between individual and multiple buttons being pushed.

  1. Great solution, I wonder if you can help me based on this multi push buttons , 15 (+ 1 backward ) to make a multiple individual counter . One each counter triple 7 segment display for the counts.

    1. Hi Avi,
      You should be able to connect the 16 push buttons to the one Arduino input using the above method with 15 different resistors.
      To connect a 3 digit 7 segment display to your Arduino, you’ll need to use a shift register, like the 74HC595. I’ll work on a tutorial for connecting a 7 segment display to an Arduino as there are a number of components you need to connect to get the display to work correctly.

Leave a Reply

Your email address will not be published.