# Two switches on one Analog pin

**URL:** <https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426>\
**Category:** General Electronics\
**Created:** [March 17, 2022, 5:10pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426 "2022-03-17T17:10:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![shayshez](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@shayshez](https://forum.arduino.cc/u/shayshez)\
**Post date:** [March 17, 2022, 5:10pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/1 "2022-03-17T17:10:29Z")

</div>

In my project I need to free up one pin so I came up with the idea to connect 2 buttons with voltage dividers to one analog pin.  
In the simulation it works as expected and I can read if any of them is pressed or both are pressed, is there a reason why not do it?

 ![Screen Shot 2022-03-17 at 18.53.16](https://europe1.discourse-cdn.com/arduino/original/4X/a/b/2/ab273fcf71852cf9dbfb0fed39f02e0bb79ec2e3.jpeg)

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [March 17, 2022, 5:13pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/2 "2022-03-17T17:13:50Z")

</div>

That's how this shield works

 ![20220317_131329](https://europe1.discourse-cdn.com/arduino/original/4X/8/c/e/8ce3d582840dfd9bea1b840bb13f98dd5bbbda3d.jpeg)

---

<div class="post-metadata">

**Author:** ![shayshez](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@shayshez](https://forum.arduino.cc/u/shayshez)\
**Post date:** [March 17, 2022, 5:16pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/3 "2022-03-17T17:16:34Z")

</div>

Should I expect problems with bouncing?

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [March 17, 2022, 5:19pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/4 "2022-03-17T17:19:49Z")

</div>

Hello  
No.  
Add a Serial.println(AnalogValue); delay(1000); to monitor the real values.  
Have a nice day and enjoy coding in C++.

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [March 17, 2022, 5:22pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/5 "2022-03-17T17:22:39Z")

</div>

```cpp
const uint16_t button_delay = 75;

void loop() {
  current_time = millis();

  // Button timing
  if (current_time - button_prev_time >= button_delay) {
    // Check Buttons
    button_val = analogRead(button_pin);
    button_prev_time += button_delay;
  }
}

```

No issues, this is how I do it.

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [March 17, 2022, 5:34pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/6 "2022-03-17T17:34:08Z")

</div>

Also, your decision tree does not take into account what it eliminates as it goes. That is ,it makes unnecessary checks:

```cpp
if (a > 900) {
  // do something a > 900
} else if (a > 800) {
  // do something 900 >= a > 800
} else if (a > 700) {
  // do something 800 >= a > 700
}

```

You should reply with a copy of your code in a code block \</\> so others can see it too

---

<div class="post-metadata">

**Author:** ![DVDdoug](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dvddoug/32/671533_2.png) [@DVDdoug](https://forum.arduino.cc/u/DVDdoug)\
**Post date:** [March 17, 2022, 8:03pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/7 "2022-03-17T20:03:11Z")

</div>

This can work and some people wire-up 10 switches to a "resistor ladder voltage divider".

But personally, I **hate** to see people going digital-to-analog-to-digital when they **really** just want digital. 😉

---

<div class="post-metadata">

**Author:** ![shayshez](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@shayshez](https://forum.arduino.cc/u/shayshez)\
**Post date:** [March 17, 2022, 8:30pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/8 "2022-03-17T20:30:54Z")

</div>

I hate it as well but this is my best solution in this specific project.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [March 17, 2022, 10:11pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/9 "2022-03-17T22:11:41Z")

</div>

> [@shayshez](#):
>
> Should I expect problems with bouncing?

Just the same as using digital pins. You need to include a debounce code _outside_ the decision tree.

It should be **reasonably** reliable for just two switches.

My experience is that the little "tact" switches are not always of good quality and many devices using this trick for multiple buttons such as computer monitors and MP3 players - as well as my car odometer which presumably just uses the button as a digital input anyway - fail over time due to moisture ingress in the button causing current leakage.

---

<div class="post-metadata">

**Author:** ![RDeangelis](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@RDeangelis](https://forum.arduino.cc/u/RDeangelis)\
**Post date:** [March 17, 2022, 11:01pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/10 "2022-03-17T23:01:17Z")

</div>

I think it's a brilliant idea! Good engineering is coming up with a reliable way to accomplish a task with a minimum of materials for lower overall cost. You done good!

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [March 19, 2022, 7:39am UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/11 "2022-03-19T07:39:21Z")

</div>

> [@shayshez](#):
>
> In the simulation it works as expected

In the real world it depends on the type and quality of your switches.

Its better to post your code properly, but I can see its just a "proof of concept" for this idea.  
Whether this solution is appropriate for your project will depend on the parts we havent yet seen.

You havent even told us which arduino you plan to use, and maybe you would be better advised to use one that supports more pins?

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [March 19, 2022, 12:25pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/12 "2022-03-19T12:25:00Z")

</div>

Just make sure the signal is stable before decoding, ie get 2 or 3 reads in succession that agree well enough - otherwise you might be unlucky and catch the voltage mid-transition.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [March 19, 2022, 12:40pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/13 "2022-03-19T12:40:48Z")

</div>

> [@MarkT](#):
>
> Just make sure the signal is stable before decoding,

As per #9.

It is called "debouncing". For an analog signal, you either debounce the analog by taking successive readings and waiting for minimal variation, or feeding successive readings through your decision tree and requiring **all** to be the same result over a period of say, 5 milliseconds.

I favour the latter because the analog decision tree can be encapsulated in a function which is called in the same way as other digital reads, and because the decision is clearer rather than having a secondary (additional) analog criterion of just how **much** the reading is allowed to vary over successive reads (as it is likely to do, possibly more than just ±1).

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [March 19, 2022, 9:39pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/14 "2022-03-19T21:39:18Z")

</div>

I've never had a bounce issue with this setup, but this method of checking will remove the highest and lowest of 3 reads and leaves the center value in the button\_val variable. This is the quickest method I can think of to eliminate 1 outlier that's higher or lower than the group.

```cpp
uint16_t lowest;
uint16_t highest;
uint16_t button_val;
uint16_t button_val_sum;
const uint16_t button_delay = 75;

void loop() {
  current_time = millis();

  // Button timing
  if (current_time - button_prev_time >= button_delay) {
    // Check Buttons
    lowest = 1024;
    highest = 0;
    button_val_sum = 0;
    for (byte index = 0; index < 3; index++) {
      button_val = analogRead(button_pin);
      if (button_val < lowest) lowest = button_val;
      if (button_val > highest) highest = button_val;
      button_val_sum += button_val;
    }
    button_val_sum -= lowest + highest;
    button_val = button_val_sum;
    // button_val has had the lowest and highest of 3 reads removed
    // call your decision tree for button_val here

    button_prev_time += button_delay;
  }
}

```

Untested

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 15, 2022, 9:39pm UTC](https://forum.arduino.cc/t/two-switches-on-one-analog-pin/970426/15 "2022-09-15T21:39:44Z")

</div>

Closed on September 15, 2022, 9:39pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
