PC keyboard to MIDI note controller adapter

Contributed by Alan Probandt (alan_probandt@yahoo.com)
November 10, 2003

This article will show you how to build a hardware adapter that will allow a computer (PC) keyboard to function as a standalone MIDI note controller (without needing the computer tower as well). A PC keyboard can be a useful alternative to a full-sized piano keyboard controller. The PC keyboard is light, very small, inexpensive, and standard throughout the world. This adapter will allow you to directly connect (with a MIDI cable) a PC keyboard to any MIDI sound module, and play that module.

By plugging a standard PC keyboard into this hardware adapter, the PC keyboard becomes a very simple MIDI controller. The PC keyboard connects to the hardware adapter, and some MIDI sound module also connects to the adapter. This makes for a direct MIDI connection between the PC keyboard and MIDI sound module as so:

PC Keyboard
Adapter
MIDI Sound Module

Pressing down a key on the PC keyboard sends a MIDI note-on message (to the connected sound module), and releasing the key sends the respective MIDI note-off message. The function keys can select a new voice (ie, patch) by sending a MIDI program change message. The space bar can send an All Notes Off message.

Certain keys on the PC keyboard map out to notes in the musical scale, physically arranged to resemble the layout of a real MIDI keyboard controller. Here is a diagram showing those notes:

PC Keyboard

Schematic

On a small piece of perfboard, build the following schematic:

Adapter schematic

Connections

There are three connectors on the adapter's circuit board. One connector is to attach a power supply (ie, usually a wall-wart AC adapter you can buy at Radio Shack or other electronics stores). The power supply should be a +8 to +15 volt DC source. This provides power for both the processor inside the PC keyboard and the AVR microcontroller on the adapter board. Most modern PS2 keyboards (with a Mini-DIN6 connector, same as a mouse) use less than 10 milliAmps of current and the AVR processor uses about 5 milliAmps. Therefore, the AC adapter does not have to be very big. Purchasing any AC adapter supplying 8 to 15 volts with at least 15 mA will suffice. Alternately, a 9V battery will work OK for at least four hours.

Note: The AC connector is not shown in the above schematic. This is because, depending upon your preference, you can use one any of a wide selection of connectors. Such a connector would need only two "pins": one for the +8 lead and one for the ground lead. All lines labeled VCC in the circuit should attach to the +8 lead of your connector, and all ground should attach to the ground lead. Also note that pin 20 of the AVR should be attached to +8, and pin 10 to ground. So, you mount a connector that will match the AC adapter you've bought, or mount a 9V battery clip if you prefer to use a battery.

The other two connectors are to attach the PC keyboard (P2 -- a standard PC keyboard female DIN6 jack), and an external sound module (a standard MIDI female DIN5 jack). The PC keyboard is plugged into the Mini DIN 6 connector. One end of a MIDI cable plugs into the larger DIN 5 connector on the adapter board. The other end of the MIDI cable goes into the MIDI IN jack of your sound module.

Operation

All the MIDI messages from the adapter are sent upon MIDI channel one.

The four rows of alphanumeric keys (1,2,... - Q,W,E,R,... - A,S,D.... - Z,X,C...) correspond to two octaves of piano keys. The 1,2,... row is the black keys (sharps and flats) and the Q,W,E,... row is the white keys of the first (lower octave) piano key row. The A,S,D... is the black key row and the Z,X... is the white key row of the second group of piano keys. There's three and a half octaves total range. Please see the above diagram the exact for the correspondence between keys and notes. Pressing the space bar sends the All Notes Off message (" B0 7B 00 ").

This range can be shifted up or down by octave using the Up-Arrow and Down-Arrow keys. The DOWN arrow (towards the front of the keyboard) shifts the range higher in frequency by one octave and the UP arrow (pointing towards the back of the keyboard) shifts the range lower in pitch by an octave.

Pressing the Caps-Lock key toggles the Sustain pedal on or off. (Initially, it is off).

Changing the Synthesizer voice

when the adapter is first powered on, the function keys F1 to F12 default to selecting the first twelve patches of the sound module. For example, pressing F2 sends a MIDI Program Change message to select patch 2 on MIDI Channel 1. (ie, The MIDI message C0 01 is sent to the module. These are hex numbers of the MIDI protocol).

You can also select the next, consecutive patch with the Right-Arrow key, and the previous patch with the Left-Arrow key. Pressing the Left-Arrow at patch 1 (hex 00) rolls the patch to number 128 (hex 7f), whereas pressing the Right-Arrow key will roll-over to patch 1 (hex 00) when at patch 128.

Alternately, you can select an individual patch number from 00 to 99 by entering two digits on the key pad. For example, to select patch 24, you press (and release) '2', and then press '3'. Since internal MIDI patch numbers start at 0 instead of 1, the patch listings in your manual will be one greater than the number you should type into the key pad.

Holding down the ALT key, while you press a function key, will assign the current patch number to the selected function key. Therefore, after you select a particular patch with the key pad, you can then assign it to a function key for quick access. But note that this setting is not saved after you turn off the adapter.

Limitations

There are some disadvantages to this adapter. First of all, a PC keyboard is not pressure sensitive. So, you will be unable to vary the volume of individual notes just by how forcefully you press a key. Even the most basic commercial controller keyboards offer at least this level of touch sensitivity. (And more expensive controllers typically offer an additional touch sensitivity). But this adapter offers no such ability to vary dynamics by touch.

Also, the PC keyboard was engineered for typists who rarely have more than two keys pressed at a time. Therefore, the keys are scanned by the keyboard's internal CPU in rows and columns. This means that some key combinations don't get acknowledged at all. On my keyboard, there is a 'dead spot' near the 'P' key that only plays one note at a time. The net result is that it may be difficult to play chords in certain areas of the keyboard. If this causes a problem, try shifting the octave range to another part of the keyboard.

Additionally, most commercial controllers allow you to pick out what MIDI channel to send messages upon, and may support a split keyboard (where different note ranges send upon different MIDI channels). This adapter, as designed, does not.

Finally, most commercial controllers have a MOD and pitch wheel. This adapter, as designed, does not.

The circuit internals and 'PCkb.asm' source code

This adapter uses an Atmel AT90S2313 AVR microcontroller to read the serial pulses from the keyboard and generate the serial pulses for the MIDI interface. The source code for the AT90S2313 is in the file named pckb.asm. Although the schematic is designed and coded for the AT90S2313 version of the AVR, the circuit can be used on any chip of the AVR family. If you use the low-end AVR model AT90S1200, then use the source code file pckb1200.asm (instead of pckb.asm) because of the limitations of the low-end 1200 model. The 1200 code uses a different look-up table for the keypresses than the code for the other AVRs.

This design's AVR system clock is set by a standard 3.579MHz color-burst TV crystal. Using a different crystal or the internal AVR R-C clock requires adjusting the delay timing value for the MIDI output. This is the constant called 'b' located in the subroutine PUT_MIDI of the source code. The input from the keyboard triggers an interrupt with each logic transition and is not dependent on any system clock speed.

Running the ATMEL Windows assembler will give the message "Code Too Big". To use this assembler, split the source file into two parts called pckb.asm and pckb2.asm at line 565. Then uncomment the include directive located at that line. Other assemblers don't have this idiotic problem.

The files 2313def.inc and 1200def.inc provide reference definitions for the processors.

Parts resources

AVR microcontrollers are available at Digikey.com.

PDF data sheets, assemblers, and code simulators for the Atmel AVR microcontrollers are found at the Atmel website: www.atmel.com. The simulator is called AVRstudio.exe. Version 3.56 is easiest to use and learn.

The best programmer is either the Atmel STK500 board for $80, or the free PC parallel port programmer found at: "Programming a spider's brain"; a robotics site located at www.xs4all.nl/~sbolt. This program code is SP12.exe. I've built and used 'Ken's Dongle' to program AVRs with no problems. The command line interface is a little tricky, but be patient, make little tests, and re-read the documentation until everything comes together for you.

GNU AVR C compiliers and lots of information, tutorials, and help is found at: www.AVRfreaks.org.

The PC keyboard connector can be removed from an old, used PC motherboard. Or, you can search for some PC parts suppliers.

Keyboard interfacing information on the web is at: www.beyondlogic.org/keyboard/keybrd.htm.

Suggested improvements

There are some improvements that you could make to the circuit and source code. Some examples are:

  1. Use the UART of the AVR AT90S2313 to add PC serial port MIDI access.

  2. Split the PC keyboard top and bottom so that each half generates messages on its own MIDI channel, and therefore can more easily utilize a different patch. Perhaps holding down the CTRL key could determine whether the function keys, arrow keys, and keypad send a Program Change on the top or bottom half's MIDI channel.

  3. Add an EEPROM chip to store custom patch information that gets sent to the sound module. This could allow custom patches to be stored for modules like the Roland MT-32 that don't have an internal battery to store data. It could also be used to store patch numbers assigned to the function keys (or MIDI channel assignments if you implement improvement #2).

  4. Add a second MINI DIN6 connector to attach a mouse. Interpret mouse movements as MIDI pitch-bend and Mod wheel data.

Disclaimer

This circuit is provided "as is" without warranty of any kind either expressed or implied or tatooed in a place that only a few people have ever seen, including but not limited to the implied warranties of merchantability, fitness for a particular purpose, and the dubious assumption that the circuit has been created by a sane individual who would never do anything that may hurt you. The entire risk as to the results and performance of the circuit is assumed by you or someone who looks exactly like you. Jeff Glatt does not guarantee that the operation of this circuit will meet your requirements, especially if your requirements involve lots of latex and some docile, domesticated animal. Nor does Jeff Glatt warranty the circuit to be uninterruptable or error-free, although mercifully free of "Pompous Ass Copyright Holder" hassles. If you use said circuit, you can not say anything nasty about the author nor Jeff Glatt, even if the circuit inadvertently causes the erasure of your collection of X-rated GIFs of a conservative, overweight and overrated TV "personality" plooking himself vigorously with his royalty checks from some rancid paperback. Jeff Glatt is not responsible for any damages as a result of anything that he has done, or hasn't done, or was supposed to do but never got around to it, and furthermore, he doesn't even care so leave him alone, sewer mold. You may have more or less protections in certain states of the union, depending upon how far your local politician is willing to bend over for some bribe from a business lobbyist. Just remember that Jeff Glatt has no money, so don't bother suing him as a result of any damages caused by this circuit. Tell your greasy lawyer to go after Creative Labs, and make sure that you pick 12 really stupid pinheads for the jury. If swallowed, induce vomiting immediately by contemplating the performance of Sound Blaster MIDI.

Sound Blaster is a trademark of Creative Labs. They can have it.