6
\$\begingroup\$

I have 15 output buttons and 4 digital inputs. As in the picture: enter image description here How do I make each button activate its corresponding address. For example, when I press s3, I want GND to be sent to inputs 0 and 1. When I press s7, I want GND to be sent to inputs 0,1 and 2.I have to do this without one button influencing the other

\$\endgroup\$
  • 1
    \$\begingroup\$ That's what diodes are for. Such an arrangement is called a "diode matrix". \$\endgroup\$ – Dave Tweed Oct 23 '17 at 15:35
  • \$\begingroup\$ I could not find any logic with the diodes, always one of them ends up putting two keys in short \$\endgroup\$ – Eduardo Cardoso Oct 23 '17 at 15:38
  • 1
    \$\begingroup\$ This is an area where a microcontroller excels - scanning a keyboard matrix (in your case 4x4). Many can scan with no extra parts, like diodes or pullup/pulldown resistors. Furthermore, a scanned switch matrix is easy to de-bounce. \$\endgroup\$ – glen_geek Oct 23 '17 at 16:14
  • \$\begingroup\$ Do I gather that you also want to be able to somehow recognize N-key press events? In other words, if I press \$S_5\$ and \$S_{10}\$ you want to somehow see 0101 and 1010 being sent in?? \$\endgroup\$ – jonk Oct 23 '17 at 19:12
  • 1
    \$\begingroup\$ You are not answering Jonk's question. Read it again. You need to define what is to happen when (1) a second key is pressed while the first is held down (2) when the second key is released before the first and (3) when the second key is released after the first. (4) What is to happen when \$ N \$ keys are pressed? \$\endgroup\$ – Transistor Oct 24 '17 at 13:11
6
\$\begingroup\$

From the comments:

Yes, my inputs are already pull-up, the problem is that with this type of configuration I can only recognize one key at a time, if I activate keys 1 and 2 at the same time the input will receive 3.

It sounds as though you need a proper keyboard matrix controller.

enter image description here

Figure 1. The MM74C922 16-key encoder.

  • The MM74C922 and MM74C923 (20-key version) provide all the necessary logic to fully encode an array of SPST switches.
  • On-chip pull-up devices.
  • No diodes needed.
  • Internal debounce with single external capacitor.
  • "Data Available" goes high when a valid entry has been made.
  • Available output returns to a low level when the entered key is released - even if another is depressed. The Data Available will return high to indicate acceptance of the new key after a normal debounce period. This two-key roll-over is provided between any two switches.

You even get to be able to detect SW0 thanks to the Data Available signal!

enter image description here

Figure 2. MC74C922 internal schematic.

You won't be able to use your common ground switch arrangement but all other potential problems will be solved.

\$\endgroup\$
12
\$\begingroup\$

The most direct way to do it is to build a diode matrix:

schematic

simulate this circuit – Schematic created using CircuitLab

The diodes in each column directly encode the "address" of the switch.

Note that I'm assuming that the inputs rise to a positive voltage if not grounded; i.e., they source current when grounded. (Add pullup resistors if necessary.) If the voltage is negative, then reverse the direction of the diodes.

\$\endgroup\$
  • \$\begingroup\$ +1 Yes I like them drawn that way too. its much more intuitive. You forgot the pull-ups though. \$\endgroup\$ – Trevor_G Oct 23 '17 at 15:58
  • \$\begingroup\$ @Trevor: Yes, I finally have a reason to use the "diagonal diode" symbol in CircuitLab. :-) The OP didn't mention pullups. He just wants to "send a ground" to the input signals. I'm assuming the inputs are already pulled high by some means. \$\endgroup\$ – Dave Tweed Oct 23 '17 at 15:58
  • \$\begingroup\$ Yes well.. without pull-ups he will always have ground.. or worse. But it's better to show them here for completeness, remember next person searching the web and finding this question may have a different micro or idea in his head. \$\endgroup\$ – Trevor_G Oct 23 '17 at 16:02
  • 2
    \$\begingroup\$ @EduardoCardoso: the diodes prevent one button influencing another in that they prevent one turning all on. You need to modify your question to clarify that you want to prevent two button pressed simultaneously giving an erroneous code. \$\endgroup\$ – Transistor Oct 23 '17 at 16:41
  • 3
    \$\begingroup\$ @EduardoCardoso Specifically, re. "if I activate keys 1 and 2 at the same time the input will receive 3" you should edit the question to state what must/may happen if two (or more?) buttons are pressed? E.g. (a) No input registered; (b) first button continues to operate (ignores others until released); (c) second (and subsequent) button(s) replace the original; (d) something else. \$\endgroup\$ – TripeHound Oct 24 '17 at 8:28
8
\$\begingroup\$

You can use a couple of 74LS148 encoders as shown in their data sheet.

enter image description here

There are also purpose built chips that scan the buttons connected as an array like the 74C922

Or if you have the ability to program one, a small ROM or FPGA can do it.

There are other ways that use many diodes.

However your statement "have to do this without one button influencing the other" and comments imply you need the ability to detect more than one button pressed at the same time. That is an entirely different proposition.

In that case you would need some form of keypad scanner. Perhaps communicating over I2C like a TCA8418.

Another alternative is to turn your buttons into a rudimentary digital to analog convertor and feed that analog value into whatever analog to digital convertor you may have available. Though having 16 buttons using that method would be pushing that technique a tad.

ADDITION

Since it's only 15 switches, another way this can be done is to use a shift register rather than an array. That will mean you only need three pins on the micro and a suitable algorithm to manipulate the shift register.

Drive type: Serial in parallel out 74LS673: Feed one output low through the shift register and read back the status on the common line.

enter image description here

Read Type: Parallel in serial out 74LS674: Load from the switches into the shift register all 16 switches attached to 16 pullups, then step feed the word back to the micro.

enter image description here

I think I like the latter best since you can grab the complete switch status at one moment in time.

With all of these you would need to handle debounce in code.

\$\endgroup\$
  • \$\begingroup\$ That looks like Trevor's "Visio-Lab". Is it? \$\endgroup\$ – Transistor Oct 23 '17 at 17:59
  • \$\begingroup\$ :) good eye @Transistor \$\endgroup\$ – Trevor_G Oct 23 '17 at 18:00
2
\$\begingroup\$

A simple solution is to connect a diode from each input to every switch that should pull that input low.

schematic

simulate this circuit – Schematic created using CircuitLab

Figure 1. A simple diode arrangement to encode the buttons.

How it works:

  • Pressing SW1 pulls down IN0 only.
  • Pressing SW2 pulls down IN1 only.
  • Pressing SW3 pulls down both IN0 and IN1.
  • Pressing SW4 pulls down IN2.
  • etc.
\$\endgroup\$
  • 1
    \$\begingroup\$ Pull-ups are required, may be internal to micro or not. \$\endgroup\$ – Trevor_G Oct 23 '17 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.