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

45,265

214

27

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

About: Arduino Tutorials by Codebender.cc Team

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!

2 People Made This Project!

Recommendations

  • Retro Tech Challenge

    Retro Tech Challenge
  • Tinkercad Student Design Contest

    Tinkercad Student Design Contest
  • Lamps and Lighting Contest

    Lamps and Lighting Contest

27 Comments

0
rohoop
rohoop

4 months ago

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

0
Waynewhitney
Waynewhitney

Question 4 months ago on Introduction

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?

0
peachville
peachville

8 months ago

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

0
CamelXP
CamelXP

6 years ago on Introduction

What will happen if you press two buttons at once?

0
selftouh
selftouh

Reply 6 years ago on Introduction

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

0
ziggi231.
ziggi231.

Reply 6 years ago on Introduction

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

0
meliudaj
meliudaj

Reply 6 years ago on Introduction

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.

0
Arakwar
Arakwar

Reply 9 months ago

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.

0
cybericius
cybericius

Reply 6 years ago on Introduction

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

0
zhenwye
zhenwye

1 year ago

Nice Thanks

0
cf.j.rockland
cf.j.rockland

1 year ago

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

0
VõK4
VõK4

3 years ago

Thank you so much!

0
portoinfo
portoinfo

5 years ago

Very good! Thank you!

0
kenyer
kenyer

5 years ago

Thank you, I was looking for this.

0
dbarrerap
dbarrerap

6 years ago on Introduction

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?

0
codebender_cc
codebender_cc

Reply 6 years ago on Introduction

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

0
grayl
grayl

6 years ago on Introduction

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

0
Douglas N
Douglas N

6 years ago

Very clever! thanks for sharing

0
mike_skinner
mike_skinner

6 years ago

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