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.

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.

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 Here) – 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.

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.

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 require 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.

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

One Reply to “Multiple Push Buttons on One Arduino Input”

Leave a Reply

Your email address will not be published.