How to access 5 buttons through 1 Arduino input

Using this method, Iā€™ll show you how you can access 5 (or even more) inputs through 1 Arduino pin. These buttons will only be read correctly if only one is pushed at any time though.

As we go through it Iā€™ll explain whatever background info you need to know, so as long as you can blink a button, read a switch and read an analog input, youā€™ll be fine. If you canā€™t do any of these, Iā€™ll point you in the right direction in the relevant steps as well.

arduino access 5 buttons through

Step 1: Parts List

This is pretty simple:

1 x Arduino (Obviously)
1 x 100K Resistor (Brown Black Yellow)
1 x 1KĀ Resistor (Brown Black Red)
1 x 10KĀ Resistor (Brown Black Orange)
1 x 33KĀ Resistor (Orange Orange Orange)
1 x 68KĀ Resistor (Blue Gray Orange)
4 x Push button switches
Some wires to connect it all

You can use any push buttons you might have lying around, and the resistor values are not critical. More on this later.Step 2: The theory ā€“ How buttons are normally read.

Step 2: The theory ā€“ How buttons are normally read.

Lets first look at how you would normally read a button. In its simplest form, you would connect your button like the circuit shown.

As you can see, you need 1 Input pin and 1 resistor per button, and then you can check the state in your Arduino sketch using thi

Iā€™m not showing all the setup etcā€¦ Obviously you need to declare everything and set the pin as an input, etc. You can see the full example on the Arduino website.

This is fine if you are only using 1 or 2 buttons, but what if you need 10? That leaves very little IOĀ pins for anything else you might want to do.

Step 3: The theory ā€“ Multiple buttons on one pin

So how do you put multiple buttons on one pin?

You cheat! The secret to this is using an analog input pin, not digital.

You can read about how the analog input works by going through this Arduino tutorial. Essentially, what you need to know though is that when there is 0V on the analog pin, analogRead() returns a value of 0 and if there is 5V, analogRead will return a value of 1023. For any voltage between 0V and 5V, analogRead will return a number proportional to the voltage.

We canā€™t actually change the voltage that is supplied to the pin (Not easily at any rate, and Iā€™m lazy, so easy is important), but if you remember from Ohms law, V=IR. The current (I) is fixed, which means that we just need to add a resistor between the supply voltage and the analog pin to change the voltage.

For those of you that were getting excited about all the maths thatā€™s necessary to calculate the voltages, Iā€™m going to have to disappoint youā€¦ Iā€™m lazy, so IĀ donā€™t need maths.

Letā€™s get a bit more practical, and Iā€™ll show you why we donā€™t care about the maths. We know that the analog pin reads voltages and we know that we can change those voltages by adding a resistor between it and the supply voltage. We also know that weā€™ve gone this far because we want to be able to read switches, so we should probably toss some switches in too.

Now, for those that are interested, To design this, you start with what you know. IĀ know how to connect a single switch to a single input. IĀ wanted 5 buttons, so IĀ duplicated it 5 times. IĀ then simplified it by having a single pull down resister connected to all the buttons, and then simply put resistors between the buttons and the supply voltage and tied all the inputs together.

If you connect each button to the supply voltage through a different value resistor, depending on which button is pushed, the value returned by analogRead would be different, and you can use a bunch of if statements to see which button was pressed. The reason we donā€™t need maths is because we just connect it all up, push the buttons and print the returned values to the serial port.

Step 4: At last we can breadboard it.

Using the previous circuit, build it on a breadboard. Iā€™ll show you how IĀ did it, but this is very dependent on which buttons you use, so youā€™ll need to use your imagination. IĀ used micro buttons from old computer mice. IĀ also only had 4 buttons (that worked properly at any rate), so IĀ decided to only do 4 buttons, but the code is set up to handle a 5th button.

Once the circuit is built, you can hook up the GND and 5V to the Arduino, and connect the buttons to analog pin 0 (You can change it, just remember to change it in the sketch).

Remember that IĀ said we donā€™t need the maths?Ā IĀ prefer trial and error. For your resistor values, you should pick values that are fairly evenly spread between an arbitrary lower and higher range. I found that values spread between about 1K and 100KĀ work best. For my resistors, IĀ happened to have 1K, 10K, 33KĀ and 68KĀ lying around, so IĀ used those. If IĀ had a fifth button, i would add a 47KĀ resistor between 33K and 68K.

After IĀ built it, IĀ realize now that the 1KĀ resistor is probably not needed. One of your buttons could be connected directly to 5V, so you need one resistor less than buttons (and the one pull down resistor shared by all the buttons). If one of your buttons is connected to 5V it should always return a value of 1023. So if you want to save a couple of cents, leave out the resistor on button 1.

arduino access 5 buttons through schematic

Step 5: Testing it

Download the attached sketch and upload it to your Arduino. Once it is uploaded, open the serial monitor as well. to test it, watch the serial monitor and then hold down button 1. You will see that the values returned will fluctuate for a bit before settling down in a small band of numbers. Write down the biggest and smallest numbers.

Using the 1KĀ resistor with my button, IĀ got values ranging between 988 and 1011.

After repeating this for all the buttons, IĀ got the following values:
Using the 1KĀ resistor with my button, IĀ got values ranging between 988 and 1011.
Using the 10KĀ resistor with my button, IĀ got values ranging between 910 and 929.
Using the 33KĀ resistor with my button, IĀ got values ranging between 767 and 768.
Using the 68KĀ resistor with my button, IĀ got values ranging between 400 and 609.

The first thing that is clear is that the range is bigger for some buttons. IĀ retested several times with consistent results. IĀ actually replaced the second button because IĀ was getting all kinds of results. IĀ was getting numbers ranging from about 510 all the way through 910, so if you get a huge range of numbers, try a different button.

Once you have these values, you can create a sketch that will read the state of a specific button.

Step 6: Coding it.

To code it, IĀ simply took the debounced button example and modified that to set a state based on which button was pressed rather than just a simple HIGHĀ or LOWĀ state.

You can download the attached sketch. It is actually quite simple. The first section sets up all the variables and constants weā€™ll use.

[box color=ā€#985D00ā€³ bg=ā€#FFF8CBā€ font=ā€verdanaā€ fontsize=ā€14 ā€ radius=ā€20 ā€ border=ā€#985D12ā€³ float=ā€rightā€ head=ā€Major Components in Projectā€ headbg=ā€#FFEB70ā€³ headcolor=ā€#985D00ā€³]

previous circuit[/box]


For more detail: How to access 5 buttons through 1 Arduino input

About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation, and engineering. I have a clear and simple writing style and am skilled in using infographics and diagrams. I am a great researcher and is able to present information in a well-organized and logical manner.

Scroll to Top