Help on momentary push switches in series.

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).

Your comments will be greatly appreciated.

Thanks

Thot

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

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

retrolefty:
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.

Thanks for the quick reply!

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

Thot:

retrolefty:
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.

Thanks for the quick reply!

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

Thanks!

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

13uttons.PNG

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

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.

Here some code to test 13 button wired up as I posted

const int keyMin = 0;      // Keymin, discovered through experiment
const int keyMax = 1023;    // keymax, discovered through experiment
void 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
}

dhenry:
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.

be80be:
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.

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.

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 :wink:

R-2R networks only work with SPDT switches, not SPST push buttons.

Are you sure about that? :slight_smile:

With a r2r type button, you can read multiple buttons from one pin, and you can detect simultaneous presses as well.

Why did you start a whole new thread on this keyboard?

CrossRoads:
Why did you start a whole new thread on this keyboard?

To pose appropriate questions in appropriate forum.

Sensor question in sensor, multiplexing in multiplexing, audio in audio etc.
It is the same project which has different aspects to it.
It is is moreappropriate I can ask All the questions in a single thread, but then we will have audio, multiplexing, programming, sensor etc. In a single thread in which most of the questions will not be pertinent.

It's kind of like when a patient comes to see me for an eye problem, a dermatologist for a skin problem and an OBGYN for, well, another problem :slight_smile: even if it's the same individual.

Also, while we are on the topic (forgive the pun) i found interesting partial answers to my questions in older posts, but they were closed so i could not follow up with questions or comments. Why close old topics? Most of them are relevant with current devices.

dhenry:

R-2R networks only work with SPDT switches, not SPST push buttons.

Are you sure about that? :slight_smile:

With a r2r type button, you can read multiple buttons from one pin, and you can detect simultaneous presses as well.

Thank you for pointing me t R/2R networks.
I'm no expert (in fact i learned about it today) but from the wikipedia article it would seem they would work also with momentary pushbuttons.

The problem of the low separation of voltages still remains as the resolution, for 12 pins is:
5/2^12 = 0.00122V

Yes, the adc keyscan approach isn't very good with many buttons, as it is limited by the adc's capabilities.

In this case, you can break the buttons up into two or even more pins with multiple r2r ladders.

So you have a 20-IO pin device, you need 13, you need to be able to detect multiple key presses.
How does adding an R2R ladder help here?

I think this is a more generic solution:

#define KEY_1 0x01 //key1
#define KEY_2 0x02 //key2
#define KEY_3 0x04 //key3
...

//macro to read 6 keys
#define adcKeyscan6(pin)  ((analogRead(pin) + 2) >> 4)

  key_pressed = adcKeyscan6(BUTTON_PIN1); //read button_pin1
  if (key_pressed & KEY_1) {
    //key_1 pressed, do something
  }

  if (key_pressed & (KEY_2 | KEY_3)) {
    //key_2 or key_3 pressed, do something else
  }

  if ((key_pressed & KEY_4) | (key_pressed & KEY_5)) {
    //key4 and key_5 pressed. 
  }
  ...

You can expand it to read more or less pins. It assumes 10-bit adc and buttons are pulled to Vref.