Main | Penguins | Projects | Travel | Photos | Publications | SensorWiki

Keyboard to MIDI interface

For a single dollar I was able to get an old Philips keyboard which was once used for music programs on MSX2 computers. Actually, it is nothing more than a row of keys arranged in a matrix with diodes. My idea was to give this keyboard a MIDI OUT option to connect it to a computer for sound generation.

Keyboard output

The connector of the keyboard outputs eight rows and eight columns. By pulling a single column line low, eight keys can be read out as a row. Electronically, the keyboard consists of switches and diodes and does not need a power supply.

The mapping of keys to rows and columns follows from the following schematic:

MIDI OUT for microcontrollers

It is quite easy to send MIDI messages from a microcontroller using the serial output. The rate must be 31.25kb/s using one parity and one stop bit. Electronically, some signal conditioning has to be done to meet the current driven MIDI protocol.

Schematics for the keyboard scanner

Although there are microcontrollers with internal program memory, I have used a 80C31 with external program memory because that was the cheapest controller I could buy.

Selecting the column from the keyboard is done using the 74HC138. The selection lines of the 74HC138 are connected as address lines to the microcontroller. Reading the pressed keys as rows of eight bits is done by reading external data memory with the MOVX instruction. The 74HC138 pulls a single column selection line low based on the selected address. Pull-up resistors are needed to get TTL signals from the row-lines.

You can download the complete schematics here. The PCB is shown below. I realise that it is a relatively large board, which is the result of the choice for external program memory. The use of software in an EPROM has the consequence that two additional buffers are needed. The hardware can be much smaller when using a microcontroller with internal ROM and a smaller package size. Nevertheless, the PCB fits into the keyboard which does need a power supply now.

Code for de keyboard scanner

The code is splitted over three modules. The first one sets the interrupt vectors and jumps to the scanning loop which is in the second module. The third module contains the MIDI routines (and can therefore easily be replaced by a module sending information over an RS232 line for debugging purposes).

Interrupt vectors

Mainloop module

MIDI routines

The structure is quite straightforward. Scanned columns are stored in internal RAM. When a new column is scanned, it is compared to the stored one. In case this row has changed (a key is pressed or released), a MIDI message is generated: either a MIDI ON (status=$90) or MIDI OFF (status=$80) message. The program does not use running status yet. Running status means that the status byte in the voice messages (90H “channel 1 note on” by default) is sent only once when starting to scan the complete keyboard from left to right.

Getting sound out of your PC

You can attach the MIDI keyboard scanner to any MIDI sound module or MIDI IN of a synthesizer. Because most of us do not have MIDI sound modules, an alternative is to use your PC as a synthesizer. Common soundcards have a MIDI IN and OUT option on the game port. An adapter is constructed easily and can be implemented in a 15-pin game port connector (see below).

As a sound generating device in a PC, you can use either the FM synthesis option of the soundcard or the wave table option. However, even on a fast PC, the delay between attaching a key and audible response is annoyingly large when using wave tables. FM Synthesis gives immediate sounds from your PC for real time playing but gives sounds of a cheap toy piano.

To connect the MIDI IN of your PC to the sound generating parts of your soundcard, you must load some software. A free and simple option is MIDI Thruway. Another nice software tool is Keyview to simply view the pressed keys on the screen.

On my first soundcard (an ISA Trouper 4x4 which should be Soundblaster compatible), this did not work. The MIDI in events were only transported to the synthesizer when software generated MIDI events (for example metronome clicks) were send to the synthesizer. Whether this was a bug or a damaged soundcard I do not know because I simply bought a new soundcard (the PCI Typhoon Acoustic 4).

MIDI adapter for PC port

The implementation of a MIDI IN connector for your PC follows directly from the MIDI spec and the soundcard spec. For MIDI compatible interfacing, the MIDI IN port must be optically isolated from the MIDI generating device. The needed 5 Volt supply for the optocoupler can be taken from pin 1 of the game port.

Additional features: display and selection keys

In the schematics above there is an expansion connector with twelve data lines. I have a proposal for attaching a display, eight preset keys and a special-feature button. An LCD display can simply be connected to 4 data lines and 3 control lines. So, five lines are left for extra buttons. In the schematic shown below, 8 buttons are multiplexed onto three lines. When one of the buttons is pressed, the INT0 line is informed. These buttons can be used to select eight presets. To define the presets, the INT1 line is still free. When pressing the key attached to INT1, the system enters another mode and the eight preset keys can be defined to have a second functionality. The user can be guided by information on the display.

The keyboard is not sensitive to pressure. In the future I want to add the facility of channel-pressure by adding a sensor in the beam to which all the keys are attached.