100+ Switches in a Single Pin of Arduino

5,353

59

7

Introduction: 100+ Switches in a Single Pin of Arduino

Introduction

Did you run out of input pins? Don't worry, here is a solution without any shift registers. In this video, we are going to learn about connecting more than 100 switches to a single pin of Arduino.

Step 1: ​Working Theory

Take a look at the circuit diagram first, otherwise, you won't be able to understand what I'm saying. Whenever I press a switch the circuit will be completed through a different number of resistors,

  • In the circuit, if we press the 5th switch then the circuit is completing through all the 4 resistors,
  • If we press the 4th switch the circuit is completing through 3 resistors,
  • If we press the 3rd switch the circuit is completing through 2 resistors,
  • If we press the 2nd switch the circuit is completing through 1 resistor,
  • And if we press the 1st switch the circuit is completing without any resistors.

That means the voltage reaching the analog pin A1 will be different for each switch, so we will use analogRead() function to read the values from the pin A1 and then we use an if else if condition to perform different operations for each switch.

Step 2: Let's Build

  • First connect five push switches to a breadboard.
  • Of course, you can connect maximum of 1023 switches theoretically to a 8-bit microcontroller like Arduino.
  • Then interconnect resistors in between the push switches as shown in the diagram.
  • Connect the other end of all the switches to 5v of Arduino, as you can see here I have connected the in such a way that one end is connected to the blue line of the beard board which is connected to the 5v.
  • Then from the end of the final switch connect a wire to analog pin A1 of Arduino.
  • Then connect a resistor across A1 and GND of Arduino, which is for pull down, That is to keep the value to zero when no switch is pressed.

Step 3: Connect Some LEDs

Let's connect some LEDs to check the functioning of our circuit.

  • Connect the LEDs as shown in the circuit,
  • Connect all the positive terminal all LEDs to the 5v.
  • Connect the negative terminal of each LEDs to the digital pin D12 to D8 of Arduino, respectively.
  • Practically we have to connect the LEDs through resistors for good life time.

Step 4: Coding

Take a look at the program. All the lines are commented properly.

Now let's upload the code and see it in action.

Step 5: Applications

  • Keypad
  • Full-sized keyboard for Arduino.
  • Custom mini keyboard for your Raspberry Pi tablet, etc..

Step 6: Drawbacks

  • Multiple switches won't work in a single instant. If you can think of another application post it in the comments.

Thanks.

Be the First to Share

    Recommendations

    • For the Home Contest

      For the Home Contest
    • Make It Bridge

      Make It Bridge
    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge

    7 Comments

    0
    TheShaggyDA
    TheShaggyDA

    3 years ago

    I use this method for detecting fret positions on my guitar synth - essentially treating the resistor chain as the track on a VR, and the brass string as the wiper.

    dsc00068_792.jpgdsc00069_181.jpgdsc00071_208.jpg
    0
    StevenS10
    StevenS10

    3 years ago

    This is a great idea, and it probably works well for up to say 10 switches, which is pretty darn good.

    But take a closer look at the math. It looks like the values you got:

    1023, 509, 338, 253, and 202

    are very close to:

    1024, 512, 341, 256, and 205

    These are of course:

    1024 x 1 , 1024 x (1/2), 1024 x (1/3), 1024 x(1/4), and 1024 x (1/5)

    The difference between consecutive values is getting smaller and smaller. IE, 1023 - 509 is about 500, but 509-338 is 171, and for your last pair, 253-202 = 52.

    When you extend to say 20 switches, the last two numbers will be approximately 1024 x (1/19) and 1024 x (1/20), that is, 54 and 51. At three units apart, you will have exceeded the accuracy that can be measured by this system. You certainly won't be able to put a +-5 on your "if" statement.

    Still, this is a great idea, and it will probably works well for up to say 10 switches, which is pretty darn good, considering you are using only one Arduino pin. But you aren't gonna get 100 switches out of it. Might be interesting to try and see how far you can get. I suspect not much more than 10.

    0
    diyusthad
    diyusthad

    Reply 3 years ago

    Your explanation is awesome, maybe I'll try with the max no. of switches and let you know. anyway, thanks for your detailed study.

    Najad,
    www.diyusthad.com

    0
    Ghloo
    Ghloo

    3 years ago

    Ah, thanks! I guess this is exactly how that 5-keys red&yellow mini keyboard I got from eBay works - I was actually reading values I got from individual buttons (from what I have seen, these may vary big time based on all sorts of factors including the temperature) and then assigning some action based on that value, just like you do. Just didn`t realize I can actually make it HUGE. ;o)

    Do not want to steel your credits, but to figure out values given by individual buttons an eager reader may want to check out http://www.martyncurrey.com/keypads/, where he can find a value tester sketch.

    0
    diyusthad
    diyusthad

    Reply 3 years ago

    :)

    0
    pantalone
    pantalone

    3 years ago

    This is a very clever way to multiplex a large number of momentary push-button inputs on a single pin. Would be be perfect for reading from a keypad. Thanks for sharing!

    0
    diyusthad
    diyusthad

    Reply 3 years ago

    thanks for your support