Go Down

### Topic: Help on momentary push switches in series. (Read 4386 times)previous topic - next topic

#### Thot

##### Nov 13, 2012, 07:27 pm
Hello,
I am building a 13 keys jeyboard, the keys being momentary push switches).

The keyboard is not going to be close to Arduino so I want to cut down on the amount of wires going back and fort from him (Arduino).

I came up with two possibilities (see picture)
The one above should work at rest (nothing pushed) but I fear that if you close any switch, all pins will go high. Am I correct?

The second one should work but I am using 13 resistors in parallel. Do I need to increase the resistance of each 13 times? That is using 13 120 K resistors (not sure if I have 130 K) ?

Also, Arduino need to be able to sense the pressing of any combination of buttons (up to 10 at a time, for a standard 10-fingered player).

Thanks

Thot

P.S. Sorry for the quality, I am not Forrest Mims III  and I don't know (yet) how to use CAD software.

There are three kind of people in the world: Those who can count, and those who can't

#### retrolefty

#1
##### Nov 13, 2012, 07:38 pmLast Edit: Nov 13, 2012, 07:46 pm by retrolefty Reason: 1

The top circuit is incorrect, pushing any button will set all the input pins to HIGH. The second is correct, however I would use much lower resistor values from say 10k to 50k ohms. However the simplest method to wire up buttons is to turn on the internal pull-up resistors in your sketch software for each pin you are using and wire a ground to the other side of each of the switches. So in your sketch, reading a LOW means that button is being pressed and reading a HIGH means it's not being pressed. That way you need no external resistors and just one common ground wire to one side of all the buttons and of course one wire each between each button and it's arduino digital input pin. That should clean up your external wiring some.

PS: your switches are not being wired in series, they create four independent parallel paths for the four input pins. You just are taking advantage of the fact that those switches have additional terminals that are wired together to allow you to daisy chain either a +5vdc or a ground bus on one side for ease of wiring.

Lefty

#### Thot

#2
##### Nov 13, 2012, 08:11 pm

However the simplest method to wire up buttons is to turn on the internal pull-up resistors in your sketch software for each pin you are using and wire a ground to the other side of each of the switches.

I came across this before but I am confused as how it works and how to turn on the internal pull up resistor
Can you give or point me to an example?

Thanks
There are three kind of people in the world: Those who can count, and those who can't

#### retrolefty

#3
##### Nov 13, 2012, 08:38 pm

However the simplest method to wire up buttons is to turn on the internal pull-up resistors in your sketch software for each pin you are using and wire a ground to the other side of each of the switches.

I came across this before but I am confused as how it works and how to turn on the internal pull up resistor
Can you give or point me to an example?

Thanks

Sure. The latest arduino IDE has made it even simpler. In your setup function code just have a mode statement for each input pin you will be using as so:

pinMode(pin#, INPUT_PULLUP);

Lefty

#### Thot

#4
##### Nov 13, 2012, 08:48 pm
Thanks!
There are three kind of people in the world: Those who can count, and those who can't

#### be80be

#5
##### Nov 13, 2012, 08:55 pm
Here a simple way to add 13 buttons all resistors are 220 ohm you'll only need Ground  and a A0 pin
and there code that works with the idea in the Playground

#### Thot

#6
##### Nov 13, 2012, 09:32 pm
That's an interesting idea!

The node on top is connected to +5, right?
Then the circuit acts as a voltage divider with each switch press bypassing a resistor and decreasing the total resistance.
But if the resistors have all the same value, can the program tell which button is pressed or only how many are pressed?
What's the value of the capacitor?

And, can you post the link to the playground article?

Thanks

There are three kind of people in the world: Those who can count, and those who can't

#### dhenry

#7
##### Nov 13, 2012, 09:46 pm
You should think of R2R type networks and imaging your buttons are pins outputing to that R2R network.

This can uniquely determine which buttons are pushed.

#### be80be

#8
##### Nov 14, 2012, 12:25 am
Here some code to test 13 button wired up as I posted

Code: [Select]
`const int keyMin = 0;      // Keymin, discovered through experimentconst int keyMax = 1023;    // keymax, discovered through experimentvoid setup() {  Serial.begin(9600);}void loop() {  // read the sensor:  int key = analogRead(A0);  // map the sensor range to a range of four options:  int keyValue = map(key, keyMin, keyMax, 0, 3);  // do something different depending on the   // range value:  switch (keyValue) {  case 0:    // your hand is on the sensor    Serial.println("button1");    break;  case 1:    // your hand is close to the sensor    Serial.println("button2");    break;  case 2:    // your hand is a few inches from the sensor    Serial.println("button3");    break;  case 3:    // your hand is nowhere near the sensor    Serial.println("button4t");    break;    case 4:    // your hand is nowhere near the sensor    Serial.println("button5");    break;case 5:    // your hand is nowhere near the sensor    Serial.println("button6");    break;case 6:    // your hand is nowhere near the sensor    Serial.println("button7");    break;case 7:    // your hand is nowhere near the sensor    Serial.println("button8");    break;case 8:    // your hand is nowhere near the sensor    Serial.println("button9");    break;case 9:    // your hand is nowhere near the sensor    Serial.println("button10");    break;case 10:    // your hand is nowhere near the sensor    Serial.println("button11");    break;case 11:    // your hand is nowhere near the sensor    Serial.println("button12");    break;case 12:    // your hand is nowhere near the sensor    Serial.println("button13");    break;  }   delay(1);        // delay in between reads for stability}`

#### MarkT

#9
##### Nov 14, 2012, 02:46 am

You should think of R2R type networks and imaging your buttons are pins outputing to that R2R network.

This can uniquely determine which buttons are pushed.

Doesn't work - R-2R networks only work with SPDT switches, not SPST push buttons.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### Grumpy_Mike

#10
##### Nov 14, 2012, 09:35 am

Here a simple way to add 13 buttons all resistors are 220 ohm you'll only need Ground  and a A0 pin
and there code that works with the idea in the Playground

That circuit can tell if one switch is presses and tell which one is being presses. However it can not cope with multiple switches being presses which is what the OP wanted.

#### Thot

#11
##### Nov 14, 2012, 01:46 pm
That's what I thought.
And I came to the conclusion that with a resolution of 1024 on the ADC in theory you could wire up the circuit to implement a binary progression for 12 buttons  (No button =0, button 1=1 button 2=2, button 3=4, button 4=8 etc.), in practice, that is unfeasible as the difference between button presses will be only 1 value, or 0.00488 V (e.g. buttons 1,2,3 pressed = 7 [0.034 V] ; button 4 only = 8 [0.039 V]) which even with very tight tolerance resistors will be sensitive to tiny supply voltage fluctuations.

If someone can prove my logic wrong and give me an example of such a circuit I will be very happy.

Incidentally, I read a long time ago that the very first digital computers designs were based on the decimal system (the most intuitive to us humans) but they never got to work in practice because of the unreliability of detecting those differences in voltage. The binary system was eventually used because it's much easier to detect No-voltage/voltage.

I feel like I re-discovered the wheel.

There are three kind of people in the world: Those who can count, and those who can't

#### michael_x

#12
##### Nov 14, 2012, 02:19 pm
If you do not have the 13 Arduino pins available, PISO shift registers were invented for your problem. Forget about the analog trick in your case.
µC is about serializing the physical world to handle it in a sequential processor

#### dhenry

#13
##### Nov 14, 2012, 03:00 pm
Quote
R-2R networks only work with SPDT switches, not SPST push buttons.