Multiple toggle buttons on 1 analog pin

Hello!

Is there any way I can get 9 buttons to toggle on an analog pin? Each button has its own function (controlling different laser shows) and when you press one it should turn off the last one. I’ve made a momentary push button work with the analog pin but I need them to be toggleable. I’ve ran out of pins on an Arduino Mega :confused:

Thank you!

If you had a pull-up resistor on the analog pin and all the 'A' terminals of buttons connected to the pin, you could use different resistors on each of the 'B' terminals of the switches to GND. When a button is pressed, the voltage divider created by the pull-up and the resistor to ground will present a voltage to the analog pin unique to that button.

You just need to select the resistors to give decent "windows" for each button to reduce the chance of overlap when things like the tolerance of the resistors is taken into account.

An analog read of the pin will return a value between 0 and 1023. Choose your resistor values to return a voltage giving nominal A/D counts of (for example)

switch 0 50 switch 1 150 switch 2 250 switch 3 350 switch 4 450 switch 5 550 switch 6 650 switch 7 750 switch 8 850 No switch > 1000

Yes, it can be done. Simple ways, more complex ways, it depends upon you needs. Both of these can be extended to 9 buttons.

Simple:

https://electronics.stackexchange.com/questions/101409/how-to-debouce-six-buttons-on-one-analog-pin-with-arduino

The only downside to this design is that it can only detect one button down at a time, multiple buttons pressed at the same time cannot be decoded.

Complex:

The R2R ladder approach can detect multiple buttons down as it provides 2^9 output values (512 distinct values)

https://electronics.stackexchange.com/questions/247538/using-as-little-pins-as-possible-for-multiple-buttons/247544#247544

And of course you can always add more digital GPIO pins by using chips like the MCP23017 which only require the two I2C pins. Far simpler than analog ladders.