Wednesday, December 4, 2013

How to build 64 keys MIDI keyboard

Hi folks.... Been a long time since I've promised to write a post on how to build 64 keys midi keyboard. In fact this post will be on what needs to be done to extend the original post where we built midi keyboard from 32 key piano toy. In this case, of course, you will require a toy with more keys... I have not seen much of the toy pianos with 64 keys (have you?) but I guess you could build a matrix from scratch yourself using push buttons (that is fun!)

You will need to slightly modify Schematics and Code to to increase number of keys.

On schematics you will have to replicate same 32 keys scan matrix and as already discussed you can organize it in several ways 8*8 or 16*4 scan matrix. Lets see how it is achieved in 8*8 rows/columns case

Assuming you already have a scan matrix with 64 keys or were able to extend 32 keys scan matrix to 64 keys you will have now 8 rows and 8 columns from original project 8*4. Now you need to do same extension on the Arduino side by adding 4 digital inputs to support extra 4 rows displayed as YELLOW group on the schematics. Hope this part does make sense. If it does not I suggest to read links that were posted on original blog post to understand how scan matrix works - that should make it clearer.

Below is modified code to handle the extra 4 rows. I've added comments to highlight the extra code added.

After modifying original sketch it should allow all 64 keys to be played at the same time (not that you would do it).

You may notice there is a lot repetition in the code. It was left intentionally that way to highlight parts that needs to be repeated. And in fact it could be made simpler by organizing rows and groupValue(s) in array. You can check the Velocity midi keyboard example as it makes use of array for rows and groupValue(s).

One of the steps you will need to do in the sketch is to Map keys to MIDI notes. You can use following resrouces:

Midi notes map (NOTE to NUMBER):

Generally good resource on MIDI:

Below is a sketch modification required to implement 8*8 matrix

Above we have discussed 8*8 martix to midi implementation.

I have not seen 16*4 matrix but if you got one, modifications will be more complex. We will not discuss details but roughly explain whats needed in this case and hopefully you will go for 8*8.

You will increase number of  columns (extra 8) and will have to add one more 74HC595 to implement 8 more columns. Number of rows will remain 4. Code modifications required will be following. You will need to add values to bits array will to increase scan loop to 16 (8 more scans for new 8 columns). Disadvantages of 16*4 matrix is it will be about twice slower than 8*8 as it will require 8 more serial scans (while in 8*8 it requires only 4 more). Schematic is also more complex in 16*4 implementation as it will require extra 74HC595 unless you use a better serial to parallel chip that has 16 outputs instead of 8. And overall from 16*4 to 8*8 you will economy couple of I/Os only - not much difference.

In fact 8*8 matrix could be implemented without 74HC595 if you ok to use all I/Os on the Arduino but make scan matrix faster... I might put a short post on it. That would make sense in case of Arduino Mega where you have plenty of I/Os and simplicity of schematics and implementation speed is what you want to achieve...

Hope that makes things a bit clearer... :)


  1. hi! need some info, i have a 48 keys keyboard, how can i program that? i assume that i have to erase two block of the text taking the 60 value (C4) as my center? or i need a totally new code? (in not good programer) thanks!

    1. Hi, if you use above code it should just work. You will have same code but schematics will not utilize 2 input pins. So 2 rows: 2*8=16 keys will be reduced (which is 64-16=48 what you need).

      But if you still want to remove redundant code then just remove code for 2 rows. You do not need following:

      int groupValue7 = digitalRead(row7);
      int groupValue8 = digitalRead(row8);

      and anything related in

      if (groupValue7 != 0 && !keyPressed[col + 48]) {
      keyPressed[col + 48] = true;
      noteOn(0x91, keyToMidiMap[col + 48], noteVelocity);

      if (groupValue8 != 0 && !keyPressed[col + 56]) {
      keyPressed[col + 56] = true;
      noteOn(0x91, keyToMidiMap[col + 56], noteVelocity);


      if (groupValue7 == 0 && keyPressed[col + 48]) {
      keyPressed[col + 48] = false;
      noteOn(0x91, keyToMidiMap[col + 48], 0);

      if (groupValue8 == 0 && keyPressed[col + 56]) {
      keyPressed[col + 56] = false;
      noteOn(0x91, keyToMidiMap[col + 56], 0);

      You just need to play with it (quite a basic code - easy to understand)

      By the way you could take a small course from Coursera? has number of courses on programming. I found it useful.

    2. thanks men!, i will report my results!

  2. Hello, thanks for the project, it's what i've been looking for ages.
    I got a little problem/question, the PCB for my keys are a little weird.
    It has 17 wires, the keys are divided in 10 groups with 6 keys each and a group with one key alone, what are the steps so i can wire and code it correctly?

  3. This comment has been removed by the author.

  4. Podrias subir una foto del circuito armado? Gracias

  5. Hi Jen, can you show how is the modification for 8x8 keymatrix , but without 74HC595 and using Arduino Mega?