Home > Projects > Interfacing(USB – RS232 – I2c -ISP) Projects > Connect several digital inputs to one analog input using Arduino

Connect several digital inputs to one analog input using Arduino

Summary of Connect several digital inputs to one analog input using Arduino


This article presents a solution to Arduino digital pin limitations by using resistor dividers. By connecting multiple buttons to a single analog pin with distinct resistor values, each button generates a unique voltage level when pressed. The method also supports detecting simultaneous button presses by calculating the combined parallel resistance, enabling complex inputs like joystick diagonals without extra pins or extensive soldering.

Parts used in the Resistor Divider Button Project:

  • Arduino
  • Buttons
  • Resistors (1K ohm, 100 ohm, 330 ohm, 470 ohm)
  • Analog Pin (Analog 0)
  • Digital Pins

I often run out of digital pins on my Arduino. Anything as complicated as, say, a video game controller, was near impossible with the amount of pins I had available. Multiplexing buttons works, but it requires lots of connections and soldering. So, I put together a solution!

Arduino Connect several digital inputs to one analog input

Step 1: Resistor Dividers in Five

In a circuit consisting of one resistor and a 9V battery, the resistor drops 9V across itself. So, the resistor’s forward voltage is 9V. Two resistors of equal value would each drop half the voltage, 4.5V, each being half the total resistance in the circuit. If one resistor is 100 ohms, and a second resistor is 300 ohms, each will have a proportional forward voltage. In a 12V circuit, the 100 ohm resistor would drop 3V and the 300 ohm resistor would drop 9V. There’s more to resistor dividers (such as potentiometers), but that is beyond the scope of this instructable. I suggest reading this for more info: https://www.sparkfun.com/tutorials/207

Step 2: Schematics and Explanation

The first diagram above is the standard way to connect buttons. Each connects to its own digital pin, and each has its own pull-down resistor. In the second diagram, all of the buttons connect to the same analog pin.  They also connect to positive via a 1K ohm resistor, and negative through different value resistors. When button 1 is pressed, the voltage level at Analog 0 is equal to {R2 / (R1+R2)} * Vcc or {100 / (1000+100)} * 5 = 5/11 of a volt or 0.45V. Solving for the other two buttons with 330 and 470 ohm resistors, we get 1.24V for button 2 and about 1.6V for button 3. We could continue adding buttons with higher value resistors, such as 680 ohms, 1.5K ohms, 3.3K ohms, and so on.

Arduino Connect several digital inputs to one analog input schematic

Also useful is the fact that pressing two buttons simultaneously will produce a completely independent value. Based on the equation for parallel resistors, 1/R=1/R1 +1/R2 … 1/Rx, we can determine that buttons one and two pressed together will produce an overall resistance of 1/100 +1/330 =1/R, so solving for R, the result is about 77 ohms. Plug that into the voltage divider formula with 77 ohms as R2, {77 / (1077)} * 5 = 0.35V, we get an entirely new voltage reading. Possible uses include being able to read diagonals on joysticks that use buttons, like this one: https://www.sparkfun.com/products/9182, without several if..then…else if statements on the Arduino.

For more on resistors in parallel, read this article: http://www.electronics-tutorials.ws/resistor/res_4.html

Major Components in Project
Arduino

For more detail: Connect several digital inputs to one analog input using Arduino

Quick Solutions to Questions related to Resistor Divider Button Project:

  • How does the resistor divider method save digital pins?
    It connects several buttons to a single analog input instead of requiring one digital pin per button.
  • Can I press two buttons simultaneously with this setup?
    Yes, pressing two buttons creates a unique voltage reading based on their parallel resistance calculation.
  • What formula determines the voltage for a single button press?
    The voltage is calculated using {R2 / (R1+R2)} * Vcc where R2 is the button resistor and R1 is the series resistor.
  • Which resistors are suggested for adding more buttons?
    Higher value resistors such as 680 ohms, 1.5K ohms, and 3.3K ohms can be added sequentially.
  • How do you calculate resistance when multiple buttons are pressed?
    You use the parallel resistor equation 1/R = 1/R1 + 1/R2 ... 1/Rx to find the total resistance.
  • What is a practical application for reading diagonal joystick inputs?
    This method allows reading diagonals on joysticks without needing multiple if-then-else statements.
  • Does this project require lots of soldering connections?
    No, it avoids the need for the lots of connections and soldering required by traditional multiplexing.
  • What voltage source is used in the example circuit?
    The example uses a 5V supply (Vcc) to demonstrate voltage drops across the resistors.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top