One of the biggest problems you could face in your current/next project, is when you’re out of free inputs to use.
Sometimes you can save a lot of inputs using some tricks, and there’re really a lot of them.
In this blog post we’re going to know how you can use many push buttons using only one analog input pin. John Boxall from tronixstuff.com demonstrates how we can do that.
Almost all MCUs come with an ADC unit, which is responsible to convert the voltage from an analog value to a digital one (digitizing), for example Arduino UNO, which uses Atmega328 MCU, has an 8-bit ADC.
ADCs convert the voltage to a number (level), so a 8-bit resolution ADC converts Vin to 256 levels.
By using this fact, we can build a voltage divider using a resistor for each button, using one ADC line and recognize each button.
John used Arduino UNO to implement this hack. He used one of the ADC lines, enabled its internal pull-up resistor and connected the buttons and resistors to it, as shown in the following diagram.
So now every button has a unique ADC value as the following:
- 1023 for nothing pressed (default state).
- 454 for button one.
- 382 for button two.
- 291 for button three.
- 168 for button four.
- 0 for button five.
To see the full details of this hack, and to get the source files, you can refer to tronixstuff website.