## Arduino – Multiple Buttons To 1 Pin Using ADC

In this tutorial, we will learn to interface multiple push-buttons on 1 pin in Arduino using Analog-to-Digital Converter(ADC). Why would we need to connect multiple push-buttons to one pin? Because microcontrollers come with a limited number of I/O pins. By interfacing many switches to one pin, we can save pins for other uses. Also multiplexing pins through software leads to fewer connecting wires and less messy project development.

## Overview Of Switch Multiplexing Using ADC

The microcontroller used in the Arduino development board has an inbuilt Analog-to-Digital Converter(ADC). For example, the Atmega328 microcontroller in Arduino UNO can convert analog voltage between 0 and 5 Volts to a 10-bit binary number. This means that we shall get a decimal value between 0 to 1023 for voltages between 0 and 5 Volts. We will use this feature to read analog voltages that will differ depending on a resistor network connected to the switches.

## Components Required For This Project

• Any Arduino compatible board.
• Resistors.
• Tactile Pushbuttons.
• LEDs.

## Method 1: How To Connect Multiple Switches To One Arduino Pin.

In this example, we will connect 4 switches to one analog pin of Arduino. Each of these switches will control an LED. The resistor values are 1K, 2.2K, 4.7K, and 10K, for which you can refer to the diagram below. The resistors are rated for 0.25 Watts. You can use any value resistor you like, provided there is a significant difference between the resistance values. Connect the resistors and LEDs to your Arduino as shown in the image below.

The resistors connected to the 4 switches give unique voltage values at the A0 pin of Arduino. Let us consider the case when Switch 1 (S1) is pressed and all other switches are off. The 1K (R1) and 2.2K (R5) resistors get shorted through the switch and act as a voltage divider. A basic voltage divider is shown in the image below.

The voltage seen by the A0 pin can be calculated as:

$V= 5V \times \frac{2.2K}{2.2K+1K}$

$\Rightarrow V= 3.4375\;Volts$

The corresponding digital value registered by the ADC will be 1024x( 3.4375 V ÷ 5 V)=704. Instead of calculating this repeatedly, we shall use the following code to find the ADC value corresponding to different button presses. Copy and upload the following code to your Arduino board.

.wp-block-code {
border: 0;
}

.wp-block-code > span {
display: block;
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
void setup() {
Serial.begin(9600);
}

void loop(){
for (int i=0; i < 6; i++){
}
delay(200);
}Code language: C++ (cpp)

Now, press the 4 switches one by one, and note down the corresponding values from the Serial Monitor. We will use these values in our next program. In my case, I got the ADC values as 700, 415, 210, and 105 corresponding to the switches S1, S2, S3, and S4 respectively. Note that the value 700 is almost close to 704, which I obtained using the formula in the paragraph above.

The following code detects the button presses and toggles the corresponding LEDs.

# Source: Electrocredible.com, Language: Arduino
#define VAL1 700
#define VAL2 415
#define VAL3 210
#define VAL4 105

void setup() {
//Pins for LEDs
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}

unsigned long lastRead=millis(); // variable used for rejecting multiple readings within a short time
void loop(){
for (int i=0; i < 40; i++){
}

}
}
}
}
}Code language: C++ (cpp)

Switch Interfacing Code Explained:

The VAL1, VAL2, VAL3, and VAL4 variables are set to the ADC values we found from the first program.

#define VAL1 700
#define VAL2 415
#define VAL3 210
#define VAL4 105Code language: CSS (css)

Then we calculate the average value of 40 readings of the ADC. As there might be a slight deviation in readings, we use the following piece of code to detect button presses if the ADC values fall within a certain limit. Also, the time between consecutive button presses is calculated using the millis() function. Without this, we will notice multiple results in a single button press. This is similar to debouncing a push-button.

if ((ADC_value>VAL1-50)&&(ADC_value<VAL1+50)&&(millis()-lastRead>300)){
lastRead=millis();Code language: HTML, XML (xml)

According to the code above, if the ADC values are between 650(700-50) and 750(700+50), the LED connected to Digital Pin 2 will toggle its state. You may be required to tweak this part of the code to get the desired result. Similarly, the other button press events are detected depending on the ADC reading.

Here, we will connect 3 pushbuttons with our Arduino. Each switch will be connected to the 5V supply voltage on one end, and to the ground through resistors on the other end. We are using resistors of values 2.2K, 4.7K, and 10K in this example. Connect the resistors and LEDs to your Arduino as shown in the image below.

Use the first program in this article to know the ADC values returned by the switches. Press the 3 switches one by one, and note down the corresponding values from the Serial Monitor. In my case, I got the ADC values as 838, 697, and 395 corresponding to resistors 2.2K, 4.7K, and 10K respectively.

The following program can be used to detect the button presses and toggle the respective LEDs. The code is the same as the one we saw above, except that this time we are using 3 switches.

# Source: Electrocredible.com, Language: Arduino
#define VAL1 838
#define VAL2 697
#define VAL3 395

void setup() {
//Pins for LEDs
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}

unsigned long lastRead=millis(); // variable used for rejecting multiple readings within a short time
void loop(){
for (int i=0; i < 20; i++){
}

}
}
}
}Code language: PHP (php)

## Troubleshooting

If you notice that a switch press is not registered, upload the first program in this article and view the ADC values in your Serial monitor. Changing the length of connecting wires or using different switches may change the ADC reading.

Building the circuit on a breadboard might cause errors in your ADC readings due to loose connections. It is better to solder the switches for proper results. If you have any questions or suggestions, please leave your comments below.

Watch the video below of multiple push-buttons being interfaced with Arduino UNO.

Posted

in

by

1. 1. 