home.social

#keyboard-matrix — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #keyboard-matrix, aggregated by home.social.

fetched live
  1. Remotely Analysing a Keyboard Matrix

    I’ve been having a conversation on discord helping someone to try to work out the keyboard matrix of a keyboard in order to eventually use it with a microcontroller.

    But discord being discord, its fine for chat, but we’re both starting to lose track of where we’re at in the analysis, so I thought I’d write up what we have so far to refer back to. And it might help someone else attempting to do the same thing.

    Warning! I strongly recommend using old or second hand equipment for your experiments.  I am not responsible for any damage to expensive instruments!

    If you are new to Arduino, see the Getting Started pages.

    Previous Projects using a Keyboard Matrix

    These are some of my previous projects that have tried to work out or implement a keyboard matrix.

    The Principle of Operation of a Keyboard Matrix

    There are some great resources out there describing how a keyboard matrix works. Here is a really good one describing lots of options:

    But the general idea is that it isn’t possible to have one switch with its own IO pin, so a 2 dimensional matrix is created with some shared GPIO pins serving many keys, but they can only be activated by a common GPIO pin.

    Typically the number of keys supported is thus number of shared pins x number of common pins.

    The pseudo code for scanning them will go something like this:

    Set all COMMON pins as OUTPUTs
    Set all KEY pins as INPUTs with PULLUPs

    Set all COMMON pins HIGH
    FOREACH COMMON pin:
    Set that COMMON pin LOW:
    FOREACH KEY pin:
    IF KEY Pin is LOW:
    The key at location COMMON+KEY has been pressed
    Set COMMON pin HIGH again and move onto the next COMMON pin

    By going through this loop quickly enough, a microcontroller can scan an entire keyboard pretty efficiently.

    So, in general we will need COMMON + KEY number of IO pins to serve COMMON x KEY number of keys.

    There are various additional tricks that can be used to reduce the number of IO pins even further – using things like shift registers, multiplexers, line decoders, or IO expanders. These trade number of IO pins for additional chips and code complexity.

    One issue to watch out for with a keyboard matrix is the concept of ghosting. I talk through the issue in this post: Pi Pico MIDI Matrix Decode – Part 4.

    The general problem is that with a matrix arrangement, having two keys pressed at once can create spurious paths through the matrix making it appear that a key has been pressed when it hasn’t.

    The way to solve that is to use diodes as part of the matrix to ensure that the current can only flow in the expected direction and not via odd paths when several keys are pressed at the same time.

    You can read more about the issue and how to solve it here:

    The Problem

    In this specific case, we were attempting to work out how the keyboard matrix for a Casiotone CT-101.

    So my main reference for working out the matrix of an old keyboard is: Keyboard Matrix Decode so those are the principles I’ll be following here.

    Here is a photo of the main PCB keyboard connector:

    And the reverse:

    So my initial thinking is that the larger traces around the edge might be power and GND, but if this is a pure matrix then there won’t be any power or GND links on that connector – it might be all matrix.

    Those parallel connectors on the unpopulated/unconnected set of holes might be a clue – maybe that is one side of the matrix?

    Here is an annotated version of the above, completing some of the links from the other side of the PCB:

    At this point, we switched to the keyboard PCB itself.

    There are a number of clues here that are really helpful.

    So we can see blocks of six diodes, which will be there to stop ghosting. That is a good place to start. Each bank of six implies a pattern to me – so I wonder if there are six lines on one side of the matrix. It would be worth trying to work out which diodes connect to which wires in the ribbon – that might give one set of common points.

    There are 49 keys, so a 6+8 matrix would give 48, so it might need 6+9 to fully scan the whole thing.

    interesting – its a 16 way connector and I’m not sure, but it looks like the connection near the “1” isn’t connected to anything making 15 = 6 + 9…

    I’d guess at this point that those spare 7 holes alongside the connector on the PCB might be test points for the 6 common parts of the matrix – it looks like the bottom one might be connected to GND.

    It looks like the 3rd one down isn’t connected – it goes to that unfilled link/arrow/(diode?). So I initially wondered if that might be for bigger keyboards that need 7 common links. But then I realised that it is connected on the other side of the board to a black/white wire.

    At this point, my suggestion is to focus on the keyboard PCB. We need to map out the diodes to the ribbon with a meter then try to link LEDs between those ribbon links and the rest to see if we can work out which keys are linked to which pairs of the ribbon.

    At this point I’d guess that we’ll find each of the bottom (say) diodes share a common link on the ribbon – so that should be a quick test.

    In fact this backs that up too – looks like 6 common points shared between the PCBs and 3 additional links to cover the number of keys – 3×6 means up to 18 off this additional PCB…

    That smaller PCB might be serving 13 keys – 2×6 plus one extra hence needing three COMMON lines.

    At this point I’m thinking that the six wires connect to every 1 in 6 keys each.  Then which set of 6 keys is governed by the other links – but without testing with a meter/LEDs I’m just guessing – it could be the other way round 🙂

    Taking out the small pcb might reveal the pattern…

    At this point I went off in search of anything that might help. Then I found this in a Casio CZ-5000 service manual: … this is PCB M425-KY3 – the CT-101 PCB is labelled M425-KY1.

    That looks very, very similar to how that small PCB might work – notice the 6 KI lines and 3 KC lines, serving 13 keys.

    From the same manual, I found this:

    So at this point I’m wondering if the same mapping might have been used with the ribbon on the CT-101 too.

    It ought to be possible to stick an LED between say KI7 and KC5 and see which key makes it work.  If these schematics apply I think that would be the top key…

    I think at this point I’d be probing that ribbon with a meter/LEDs to see if it matches this schematic and if so – we have the matrix.

    I think our chances are pretty good that the centre of the ribbon is those 6/7 test points I mentioned earlier on the other PCB.

    In fact at this point I’m pretty sure this would match – the topmost pin doesn’t look connected – that would be KC0 on the CZ5000 which has 61 keys so will need the additional line…

    Actually there are 17 pins on the CZ-5000 – there are 6 common pins for both, but the CZ needs two extras to add another octave on (2×6 = 12).

    So I think for the CT-101 there are 6 “data” lines at the bottom, then 6 common lines, then 3 more “data” lines at the top, and then one spare, unconnected. This is shown below.

    When testing, it has to be remembered that the keyboard has some diodes too – so it is important to make sure the LED/power go the right way round to work with the diodes on the PCB too.

    At this point, we managed to find a service manual for a Casiotone CT-401 which was really handy! Here are some useful extracts.

    PCB M422-KY1:

    PCB M422-KY2:

    The keyboard matrix:

    Section 4.1 of that manual gives a hint at what they might have done with the CT-101.  The trick is working out which are the KC lines and which are the KI lines.

    It should be possible to buzz-out for continuity from one side of those diodes to the right connections on the ribbon and that should at least give an idea of where the 6 KI lines are.  Looking back at that schematic – the non-line side of the diodes should be connected to each other and one KI line – I’ve highlighted KI0 below as an example:

    I’m guessing the leads of the diodes I’ve highlighted in the photo below would be  connected together and then all of them to one of the ribbon connections.

    If we can repeat that for each of the six diodes that will give us the 6 KEY lines for the matrix (KI).

    Once the six KI lines are mapped out, we can then use a circuit a bit like this one to work out where the different KC connections are:

    i.e find out which keys in each octave use KC0; then move the battery connection to KC1 and repeat; then KC2; etc and the slowly map out which of the keys correspond to which of the KC and KI lines.

    The aim is to be able to label the ribbon with KI and KC numbers and then make a table like the one in section 4.1 of the service manual.

    Note – I almost always get the direction wrong in a matrix the first time I think about it. It isn’t helping that the CZ circuit I found is shown the opposite way round to the circuit in the CT service manual! But I think the KI lines should be the positive side of the battery and the LEDs aligned accordingly.

    Eventually we’ll have an experiment going something like:

    • Connect to KC0
    • Work out which keys are now connected to KI0 to KI5
    • Then connect to KC1 and do the same for KI0 to KI5

    Then eventually we’ll get something like:

    • C2 = KC1 + KI4
    • C2# = KC1 + KI5
    • D = KC2 + KI0
    • D# = KC2 + KI1

    and so on.

    Closing Thoughts

    We believe the matrix looks as follows:

    Where the pin numbers across the top and down the left hand side are the pin numbers from the ribbon cable. The numbers in the table go from 1 to 49 and correspond to the keys themselves, from left (lowest C=1) to right (highest C=49).

    There are a few oddities – like why is pin 10 apparently the lowest note of each set of six… apart from the first set, where it seems to be note 49? And why do pins 1 or 16 seem to be connected to give the first not?

    More recently, I’ve found the “Casio Mod Guide” by Robin Whittle, which has a description of the key matrix for some similar boards. It states:

    I don’t quite know if this maps over onto the CT 101 or not, but probably need to sit and unpick those sentences and the provided tables to see!

    Some more clues:

    Combining these tables with the pins from the experimental mapping gives me the following:

    So that does seem to match with the oddities in the mapping. I’d say with that, we have the full list of KIn and KCn mappings now.

    Hopefully this gives a bit of insight into the kind of process required to do this kind of thing. Do let me know if you manage to save and repurpose a keyboard!

    Kevin

    #keyboardMatrix

  2. Keyboard Matrix Decode

    I found an old keyboard, a “KSP970” which is a sub £20 keyboard with full size (non touch sensitive) keys, that was being thrown out as battery acid had corroded away most of the circuit board inside, but the keyboard itself was still intact so I thought it would make an interesting project to attempt to read the keyboard.

    These are the key Arduino tutorials for the main concepts used in this project:

    If you are new to Arduino, see the Getting Started pages.

    Parts list

    • Arduino Nano
    • 16x LEDs (optional)
    • 8x 220Ω resistors (to go with the LEDs – also optional)
    • Breadboard and jumper wires
    • Scrap keyboard!

    The Circuit

    Most keyboards and keypads use a “matrix” method of decoding keypresses.  I won’t go into the details here, but the Open Music Labs tutorial has an excellent description of the concept, but the general principle is that the keys are decoded as “rows” and “columns” even though on a music keyboard they are naturally sequential. If you are lucky, as I was, the keyboard will have two sets of connecting wires (see the two silvered ribbon cables in the photo) – one for “rows” and one for “columns” which can be linked up to your Arduino’s IO pins.

    The trick is to work out which keys are linked to which pins, which is where this simple circuit comes in.

    I have a 61 note keyboard and each of the ribbons is 8 wires wide.  I can see from the underside of the keyboard pcb that each key’s switch is linked via a diode to the “row” pins.  I’ve only shown one key circuit in the above, but the Open Music Lab’s tutorial shows the “matrix” in more detail and the same circuit seems to be used here.

    One simplification for me though was that I have several of these really cheap “8 bit” LED “bars” which make decoding the key matrix very easy – they contain 8 LEDs and associated resistors in a breadboard-friendly format.

    My final test circuit looks like this:

    I feed power into the two breadboard rails (5V) and then can work out which keys light up the second set of LEDs and thus map keys to combinations of “rows” and “columns”.  Note, in the above photo I’ve applied power to all of the “rows”, but in practice you’d only want power on one at a time to see which keys light up the other LEDs.

    Once I knew how the keys were decoded (it turned out pretty simple and logical) then I could hook it up to an Arduino and see if I could detect keypresses.

    I used a Nano which means I could relatively simply attach the two 8-way connectors to the two sides of the board, with one proviso.  I am using the analog ports as digital IO pins, which works fine for A0 to A5, but you can’t use A6 and A7 in that way so I had to re-route the last two pins over to D11 and D12.  As I had a new cheap Nano to use, I was able to desolder the pins on A6 and A7 leaving them unconnected from the 8-way headers.

    The Code

    Once the Nano was involved, I was able to install the Keypad library for the Arduino (search for Keypad and find the library simply called “Keypad” by Mark Stanley) and modify the MultiKey demo sketch as follows:

    • I need a 8×8 matrix decoding, so the array at the start of the sketch needs updating.
    • As the keys on my keyboard decoded sequentially in a straightforward manner, I didn’t have any complicated reordering of the keys to be done to match up with Arduino pin numbers.
    • I want to eventually output MIDI note numbers, so I changed the characters in the array to MIDI note numbers.
    • On printing, I forced the code to print the MIDI note number rather than the letter names.

    My first try had the rows and columns the wrong way round and yielding nothing.  The mapping I eventually used was as follows.

    const byte ROWS = 8;const byte COLS = 8;char keys[ROWS][COLS] = {{36,37,38,39,40,41,42,43},{44,45,46,47,48,49,50,51},{52,53,54,55,56,57,58,59},{60,61,62,63,64,65,66,67},{68,69,70,71,72,73,74,75},{76,77,78,79,80,81,82,83},{84,85,86,87,88,89,90,91},{92,93,94,95,96,97,98,99}};byte rowPins[ROWS] = {A0,A1,A2,A3,A4,A5,11,12}; //connect to the row pinouts of the kpdbyte colPins[COLS] = {2,3,4,5,6,7,8,9}; //connect to the column pinouts of the kpd

    This seems to work very nicely.  You can see it hooked up and the serial output from playing a “chord” below.

    Find it on GitHub here.

    Closing Thoughts

    Decoding the key presses ended up being relatively simple for this keyboard which was a bonus.  Next I will add the MIDI library and get it actually outputting MIDI messages.

    I’ve had a play with the tone generating module, just to get an idea for what it can do (I might write that up one day too), but it isn’t really salvageable.

    However the power supply and built-in amplifier and speakers seem fine so it would be really interesting to use this as a housing for some of my other Arduino music projects.

    Kevin

    #arduinoNano #keyboardMatrix #keyboardScanning #keypad #midi

  3. Keyboard Matrix Decode

    I found an old keyboard, a “KSP970” which is a sub £20 keyboard with full size (non touch sensitive) keys, that was being thrown out as battery acid had corroded away most of the circuit board inside, but the keyboard itself was still intact so I thought it would make an interesting project to attempt to read the keyboard.

    These are the key Arduino tutorials for the main concepts used in this project:

    If you are new to Arduino, see the Getting Started pages.

    Parts list

    • Arduino Nano
    • 16x LEDs (optional)
    • 8x 220Ω resistors (to go with the LEDs – also optional)
    • Breadboard and jumper wires
    • Scrap keyboard!

    The Circuit

    Most keyboards and keypads use a “matrix” method of decoding keypresses.  I won’t go into the details here, but the Open Music Labs tutorial has an excellent description of the concept, but the general principle is that the keys are decoded as “rows” and “columns” even though on a music keyboard they are naturally sequential. If you are lucky, as I was, the keyboard will have two sets of connecting wires (see the two silvered ribbon cables in the photo) – one for “rows” and one for “columns” which can be linked up to your Arduino’s IO pins.

    The trick is to work out which keys are linked to which pins, which is where this simple circuit comes in.

    I have a 61 note keyboard and each of the ribbons is 8 wires wide.  I can see from the underside of the keyboard pcb that each key’s switch is linked via a diode to the “row” pins.  I’ve only shown one key circuit in the above, but the Open Music Lab’s tutorial shows the “matrix” in more detail and the same circuit seems to be used here.

    One simplification for me though was that I have several of these really cheap “8 bit” LED “bars” which make decoding the key matrix very easy – they contain 8 LEDs and associated resistors in a breadboard-friendly format.

    My final test circuit looks like this:

    I feed power into the two breadboard rails (5V) and then can work out which keys light up the second set of LEDs and thus map keys to combinations of “rows” and “columns”.  Note, in the above photo I’ve applied power to all of the “rows”, but in practice you’d only want power on one at a time to see which keys light up the other LEDs.

    Once I knew how the keys were decoded (it turned out pretty simple and logical) then I could hook it up to an Arduino and see if I could detect keypresses.

    I used a Nano which means I could relatively simply attach the two 8-way connectors to the two sides of the board, with one proviso.  I am using the analog ports as digital IO pins, which works fine for A0 to A5, but you can’t use A6 and A7 in that way so I had to re-route the last two pins over to D11 and D12.  As I had a new cheap Nano to use, I was able to desolder the pins on A6 and A7 leaving them unconnected from the 8-way headers.

    The Code

    Once the Nano was involved, I was able to install the Keypad library for the Arduino (search for Keypad and find the library simply called “Keypad” by Mark Stanley) and modify the MultiKey demo sketch as follows:

    • I need a 8×8 matrix decoding, so the array at the start of the sketch needs updating.
    • As the keys on my keyboard decoded sequentially in a straightforward manner, I didn’t have any complicated reordering of the keys to be done to match up with Arduino pin numbers.
    • I want to eventually output MIDI note numbers, so I changed the characters in the array to MIDI note numbers.
    • On printing, I forced the code to print the MIDI note number rather than the letter names.

    My first try had the rows and columns the wrong way round and yielding nothing.  The mapping I eventually used was as follows.

    const byte ROWS = 8;const byte COLS = 8;char keys[ROWS][COLS] = {{36,37,38,39,40,41,42,43},{44,45,46,47,48,49,50,51},{52,53,54,55,56,57,58,59},{60,61,62,63,64,65,66,67},{68,69,70,71,72,73,74,75},{76,77,78,79,80,81,82,83},{84,85,86,87,88,89,90,91},{92,93,94,95,96,97,98,99}};byte rowPins[ROWS] = {A0,A1,A2,A3,A4,A5,11,12}; //connect to the row pinouts of the kpdbyte colPins[COLS] = {2,3,4,5,6,7,8,9}; //connect to the column pinouts of the kpd

    This seems to work very nicely.  You can see it hooked up and the serial output from playing a “chord” below.

    Find it on GitHub here.

    Closing Thoughts

    Decoding the key presses ended up being relatively simple for this keyboard which was a bonus.  Next I will add the MIDI library and get it actually outputting MIDI messages.

    I’ve had a play with the tone generating module, just to get an idea for what it can do (I might write that up one day too), but it isn’t really salvageable.

    However the power supply and built-in amplifier and speakers seem fine so it would be really interesting to use this as a housing for some of my other Arduino music projects.

    Kevin

    #arduinoNano #keyboardMatrix #keyboardScanning #keypad #midi