Friday 27 May 2022

DIY MIDI Keyboard

 13 note MIDI Keyboard with Octave buttons


Whilst working on the update of the next Helios Synth I've put together the code for a little MIDI controlled keyboard.  It might be useful to someone, so I've published it here.

What is it?

It's a simple 13 note MIDI-out keyboard with an up octave button and a down octave button.  An LED lights brighter the higher the chosen octave.

Is that it?

Well yes, but I'm planning on adding an arpeggiator soon.

DIY MIDI Keyboard

You'll need;


15x   Push buttons (push to make type)
15x   10k Ohm resistor
2x     220 Ohm resistor
1x     270 Ohm resistor
1x     LED
1x     Arduino Nano
1x     MIDI DIN Connector

Here's the Fritzing image;


The Code



You'll also need the 47 effects midi library installed in your Arduino library.


About The Code

MIDI:

The code is stuffed with comments, so go take a look at that, but essentially with a button press we send a midi message that looks like this;

MIDI.sendNoteOn(49, 127, 1); // "Note C sharp On"

To break that down, the 49 is the note number.  Here's a list of midi note numbers that we use;

MIDI Note numbers:
octave C C# D D# E F F# G G# A A# B         0   24  25  26  27 28 29 30 31 32 33 34 35
1 36 37 38 39 40 41 42 43 44 45 46 47
2 48 49 50 51 52 53 54 55 56 57 58 59
3 60 61 62 63 64 65 66 67 68 69 70 71
4 72 73 74 75 76 77 78 79 80 81 82 83
5 84 85 86 87 88 89 90 91 92 93 94 95

...so note number 49 is the same as a C# in the second octave.

The next number is 127, which is the velocity of the note.  127 is full loudness, so for a silent note we send a 0, (in other words: stop playing the note) like this;

MIDI.sendNoteOff(49, 0, 1); //"Note C Sharp Off"

And the last number: '1' is the MIDI channel number.  MIDI can send info on 16 different channels but we're just using channel 1.

Octave Buttons

The octave buttons count from 0 to 5, incrementing by +/- 1 with each press.  We then send this number into an array of midi note numbers. So here's the array, made up of 12 notes, over 6 octaves;

const int octaveSelected[][12] = {
{24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35}, // Oct 0
{36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, // Oct 1
{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59}, // Oct 2
{60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71}, // Oct 3
{72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83}, // Oct 4
{84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, // Oct 5
{96} // Oct 6
};


So now instead of sending a MIDI note number like '49';

MIDI.sendNoteOff(49, 0, 1); //"Note C Sharp Off"

We send the array with the button count number inside it;

MIDI.sendNoteOn(octaveSelected[octButtonPushCounter][1], 127, 1); // "Note C sharp On"


Btw, the [1] is representing the second note in the array (remember: computers start counting at 0).  So if you wanted the standard 'C' note, it would be the 1st item in the array which is [0]. Or if you wanted the last note it would be [12] etc.  These numbers are hard coded to our push buttons.

We also do a similar trick with the LED brightness.  That's stored in an array;

const int octLED_Brightness[] = {0, 10, 30, 50, 120, 255};

...and those numbers control the PWM of the LED.

That's basically how the code works.  

Enjoy! (and if you add an Arpeggiator to it please let me know)