Piano Keyboard, detecting multiple keys pressed

In summary, the speaker has an old piano keybed with 32 keys that they are trying to turn into a MIDI controller. The keybed has 2 buttons per key for MIDI velocity, but the speaker is not using the timing calculations for this project. They need to know that both buttons are pressed to uniquely identify the 32 piano keys. The keybed has a flat ribbon cable with 16 prongs, and when a specific note is pressed, the speaker can read a resistance of 3.8 Mohms across two pins. They suspect there is some kind of digital component they are unfamiliar with that they need to proceed. They are unsure how to detect which pin on the ribbon is being grounded while
  • #1
mishima
576
43
I have an old piano keybed I am trying to turn into a midi controller for a synthesizer.

The keybed has 32 keys and is just a matrix of diodes and buttons. There are 2 buttons per key (2 buttons per note pressed on the piano). This is for MIDI velocity, ordinarily the time between the two strikes is used to send a midi command representing how hard the piano key was pressed. I am not doing those timing calculations for this project, but I still need to know that both buttons are pressed. This is how the 32 piano keys are uniquely identified.

From the keybed is a flat ribbon cable with 16 prongs. When a specific (lets say C6) note is pressed, I can read 3.8 Mohms across two pins, 1 and 16. At the same time, I can also read the same resistance across 2 and 16.

Of course its easy to detect 1 note with a microcontroller just using a pull up resistor around 20 Mohm. The difficulty I have now is getting multiple notes to work. I have decoded the matrix enough to know the pin combos for each note, but I suspect there is some kind of digital component I am unfamiliar with that I need to proceed.

What's confusing is that I almost need a way to detect which pin on the ribbon is being grounded while at the same time knowing which pins on the ribbon are being pulled high.

A short segment of the pattern is below so you can kind of see why 3 pins uniquely identify a note. The higher number is on the negative side of the diodes.

A5 1 and 14, 2 and 14
B5 1 and 15, 2 and 15
C6 1 and 16, 2 and 16

I can detect 1 and 2 being sent high. I don't know how to detect, at the same time, 16 as opposed to say 15 being taken negative.

Could a shift register help here? Is there some other component that's applicable? Any thoughts?
 
Engineering news on Phys.org
  • #2
I suggest you make a schematic or partial schematic. It's difficult to help you until we have more details.
 
  • #3
I will try to get something together in paint.

Imagine a ribbon with 16 prongs. When a piano key is pressed, 3 of these prongs interact in a predictable pattern. For C6, pins 1 and 16 develop a resistance of 3.8 Mohms and at the same time pins 2 and 16 develop the same resistance. A different key has a different combination of pins, but the same resistance. How can I detect this with a microcontroller?

Detecting a single button like C6 is easy, I could connect a 20 Mohm resistor to pin 1 say and take it to 5V. When the C key is pressed, the voltage divider created between the 3.8 and 20 would bring the 5v down to something in the digital LOW range (0-1.5). This change from 1 to 0 I could then use as a que to play the note.

The problem is piano keys share pins. Another piano key nearby (say B6) would also trigger pin 1 to go LOW. The only difference between the 2 keys is which pin the grounded side of the resistance appears at. 16 for C6, but 15 for B6. Its like I need a way to know which pin gets the other end of the 3.8 resistance somehow.
 
  • #4
Where I come from, Mohm is Mega ohm. I find it hard to believe you are dealing in millions of ohms through a switch.
 
  • #5
Understandable, I originally tried to decode the ribbon with a continuity test. After that failed is when I switched to resistance setting.

The only thing on the keybed board is the 2 switches and 1 diode per piano key. The connections link together in a matrix the same way a simple keypad might. So unless there are some hidden surface mount components somewhere, I'm not really sure why such a high resistance is coming from.
 
  • #6
I'm not understanding why you wouldn't be able to distinguish between different sets of keys.

I'm imaging something very similar to a resistive ladder like R-2R topology in data converters where each key represents a set of R resistor and the output goes into your microcontroller.
 
  • #7
mishima said:
So unless there are some hidden surface mount components somewhere, I'm not really sure why such a high resistance is coming from.
The high resistance could be because it has capacitive switches rather than conductive switches. What happens when you change the polarity by reversing the multimeter leads?

I would expect the keyboard to be scanned by pulsing one wire at the time, while watching all other wires for that pulse. Since diodes are involved, the pulse could be negative or positive.

We need a circuit diagram that actually shows the position and orientation of the diodes. You also need that diagram.
 
  • #8
1616715487047.png


The keybed must be nearly identical to this from what I can tell. I am reluctant to remove the board any furthe from the keys because the rubber contacts on the 64 switches seems tough to put back together again. I can probe the diodes with a multimeter however.

For this section of 32 keys, there are 16 pins on the ribbon cable. From what I've been able to decode, it works out that the rows are pins 1-8 and the columns are pins 9-16.

As Baluncore enlightened, the capacitive nature of the switches let's me see when a certain switch is being pressed by showing its resistance...hence not a short on a continuity test.

Each piano key gets 2 of these capacitor switches in the diode matrix. In the schematic above, this means a certain piano key would have assigned a pair of switches sitting to the left and right of each other.
 
  • #9
You cannot expect sensible readings from a digital ohmmeter with diodes involved. You need to use the diode test function if it has one.
 
  • Like
Likes berkeman
  • #10
mishima said:
The keybed must be nearly identical to this from what I can tell.
We really cannot advance this discussion until we see a diagram of the actual circuit used. Where is the second switch?
 
  • #11
Think of the piano key from a side view, like a lever rotating on its fulcrum. The two switches per key are underneath and get pressed in order as the piano key presses onto both of them.
 
  • #12
mishima said:
The two switches per key are underneath and get pressed in order as the piano key presses onto both of them.
Are the switches in parallel ?
Where is the diode in the matrix ?
 
  • #13
Grounds for both switches on a certain piano key are connected, but not the positive side.

Diode test shows ~0.58 V across the same sets of pins for a certain piano key. The 32 keys are laid out in a matrix like this:

asdf.png

Green circles are the white piano keys, purple circles are black keys. In orange is shown the way the circuit connects when the C6 piano key is pressed.

What I'm confused on is how to hook this up to a microcontroller in a way that I can detect both C6 and B5 for example. B5 is the key just left of C6 in the matrix above. They both use positive lines 1 and 2. Only the ground line is different. 15, instead of 16. How can a microcontroller know B5 is using a different ground line?
 
  • #14
mishima said:
What I'm confused on is how to hook this up to a microcontroller in a way that I can detect both C6 and B5 for example. B5 is the key just left of C6 in the matrix above. They both use positive lines 1 and 2. Only the ground line is different. 15, instead of 16. How can a microcontroller know B5 is using a different ground line?
Apparently you didn't read this:
-
Baluncore said:
I would expect the keyboard to be scanned by pulsing one wire at the time, while watching all other wires for that pulse. Since diodes are involved, the pulse could be negative or positive.
For instance, the rows would be driven and the columns would be read. This is typically the way keyboards are read.
 
  • Like
Likes sophiecentaur
  • #15
So,

connect 20 Mohm resistors from pins 1-8 to 5V
write 5V to lines 9-16
read pins 1-16

if C6 is pressed, I would see 0 at pins 1, 2 and 16. I would see 1 at all other pins.
 
  • #16
Why are you choosing 20 Megohm resistors? You would use resistors on the lines that you read, not the lines that are driven.
 
  • #17
Look at it as an 8 x 8 matrix. Read it in 8 steps.
Sequentially raise to +5V only one pin of the set 1 to 8.
For each row read all 8 column bits 9 to 16.
What processor will you be using to read the keys ?

Alternatively you could have pull-up resistors on pins 1 to 8.
Sequentially ground the pins 9 to 16, reading pins 1 to 8 each time.

I would number the row and column pins as 0 through 15.

Check the switch resistance again.
This time reverse the polarity of the meter leads to counter the diode.
 
  • Like
Likes .Scott
  • #18
Checked switch resistances again: 3.6-3.8 Mega Ohm (20 dollar wal-mart multimeter)
Diode test with poles reversed: infinite resistance

The value of 20 megaohm was calculated using the equation for a voltage divider. With R1 as 20 MΩ, and R2 as 3.8 MΩ, I get a switch open voltage of 5 (the 20 acts as a pull up) and a switch closed voltage of ~0.79. These two voltages correspond to a logic HIGH (anything 3-5 V works) and logic low (anything 0-1.5 works). So in other words when the button is pressed the logic level at the corresponding 1-8 pin would change from 1 to 0.

As for which processor, I'm trying to get this to work with something like an arduino uno because its what I have on hand. This first board is for 32 keys but there is a second board with the other 56 keys I've yet to decode the matrix for. Trying to get just 2 notes working before I tackle that next step. Once the right piano key is detected and the correct MIDI signal setup with the code, a serial communication line will be used (on arduino this is pin 1).

Baluncore said:
Look at it as an 8 x 8 matrix. Read it in 8 steps.
Sequentially raise to +5V only one pin of the set 1 to 8.
For each row read all 8 column bits 9 to 16.
What processor will you be using to read the keys ?

So, if I followed the method outlined here, how are 9-16 connected?
 
  • #19
I really question whether you are inerpreting the dvm readings correctly. Are you measuring the resistance of the switch alone or are you measuring with the diode in series?
-
Until you completely understand this, it is foolish to continue on with plans on how to connect all 16 wires.
 
  • #20
I'm just tapping the multimeter leads on the ribbon cable coming off the keybed. There are 16 pins. When piano key C6 is pressed, I see the resistance across pairs 1 and 16 and 2 and 16. Negative lead is on 16 and positive on 1 and 2.
 
  • #21
Resistors over 100kΩ will be unreliable due to moisture condensation. I would expect the switch resistance to be between 1Ω and 1kΩ. I think you are measuring two diodes in opposition.

The matrix.
You need an 8 bit output port to drive all columns 9 thru 16.
Initialise the column output port to be all bits high.

You need an 8 bit input port that will read all the 8 rows at the same time.
There will be 8 pull-up resistors, one on each row, to the +ve logic power supply.

The code would go like;
Set output 9 low, all others high. Read the 8 rows, if any bit is low that switch is closed. Compare the byte read with the last byte read of that column to detect a change of switch status. Repeat for bits 10 thru 16, one low at the time.
 
  • Like
Likes Averagesupernova
  • #22
I'll prototype some code over the weekend and see if I can get 2 notes working.

Is there a name to this procedure I can look up for more research?
 
  • #23
mishima said:
I'll prototype some code over the weekend and see if I can get 2 notes working.

Is there a name to this procedure I can look up for more research?
A better idea would be to actually understand the readings on the dvm first. Your approach equates putting gas in the car when you can't even find the filler cap and expecting the car to run. If you want to do a little research first (which is highly recommended) Google keyboard switch matrix or some related phrase. It is a very very common method to decode which key/keys are pressed. Not that we don't want to see you come back here and hear about your project, but there are other places on the net that explain this. When you read several and understand them, you will be well on your way.
 
  • #24
  • #25
Baluncore said:
Are the switches in parallel ?
Where is the diode in the matrix ?
The switches are independent - the time between making their their contacts gives the velocity of the key stroke.
(non- PF) people who have gone through this same exercise already. A good Web search would yield the answer and a working circuit, I'm sure.
Google Polyphonic keyboard circuit design or something like that.
 
  • #26
Let me be more specific.
I will assume that the microprocessor you are using has 8 pins that can be dedicated for digital output and 8-pins that can be dedicated for digital input. (If not, you will need to use multiplexers)

1) Connect the digital outputs from the microprocessor to Row1 to Row8.
2) Connect the digital inputs from the microprocessor to Col1 to Col2.
3) On microprocessor start-up:
3a) Configure the digital outputs to active pull-up and pull-down. If that configuration is not available, then configure for active pull-up and install 8 20KOhm pull-down resistors, one on each output line. If that configuration is not available or you are confused, tell my exactly what microprocessor you are using and I will be more specific.
3b) Configure the digital inputs with a pull-down resistance as well. If that is not available, use 20KOhm pull-downs out those inputs as well.
4) On each read cycle (perhaps every 100usec):
4a) Set the outputs for all low except Row1. If all eight of these are being accessed through a single byte with the LSB assign to Row1, this would be 0x01 (hexadecimal 1).
4b) Wait at least 0.1 usec (or check you data sheet for a more precise value).
4c) Red back the Row1 button states from the input pins.
4d) Repeat steps 4a-4c for each of the eight rows. If you are using a single byte for output as described in step 4a, the output values will be 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, and finally 0x80.

Once you get that working, there are other considerations. For example, if key-bounce is a problem and you have access to the 64 lines between the buttons and the diodes, then small caps can be installed.
 
  • #27
A couple of notes.

The outputs of microcontrollers are designed to pull down or sink more than they pull up, so it is therefore better to pull down the columns 9 to 16. The output port can then be open collector and does not need active pull up.

Microcontrollers often have an input voltage threshold related to ground, so it is better to read and to pull up rows 1 to 8 with resistors. Some input ports can be configured to source pull up current, eliminating the resistors.

Avoid capacitors across switches. The key matrix is a switched current path circuit. Capacitors across switches would couple the column scan to every row when read. If needed, a digital scan can eliminate bounce in the software.
 
  • #28
Just wanted to say thanks, got it working today. :smile:
 
  • Like
Likes sophiecentaur and Baluncore

FAQ: Piano Keyboard, detecting multiple keys pressed

How does a piano keyboard detect multiple keys being pressed at once?

The piano keyboard uses a system of sensors and circuits to detect when a key is pressed. When multiple keys are pressed at once, the sensors send signals to the circuits, which then interpret the signals and determine which keys are being pressed.

Can a piano keyboard detect an unlimited number of keys being pressed simultaneously?

No, most piano keyboards have a limit on how many keys can be pressed at once. This is known as the "polyphony" of the keyboard. The polyphony varies depending on the type and quality of the keyboard, but most can detect at least 64 keys being pressed at once.

How does the piano keyboard know which key is being pressed?

Each key on a piano keyboard has a unique sensor and circuit assigned to it. When a key is pressed, the sensor sends a signal to its corresponding circuit, which then sends a signal to the sound generator to produce the corresponding note.

Can a piano keyboard detect when a key is being held down?

Yes, piano keyboards have a sustain pedal that allows keys to be held down and sustain the sound. The keyboard also has a mechanism that detects when a key is being held down and adjusts the sound accordingly.

Are there any limitations to the accuracy of a piano keyboard in detecting multiple keys pressed?

While piano keyboards are generally very accurate in detecting multiple keys being pressed, there can be limitations in certain situations. For example, if a key is pressed very quickly after another key, it may not register as a separate key press. Additionally, the sensitivity of the keys can affect the accuracy of detection.

Back
Top