John Harvey
Skip Navigation Links

Arduino 101

The Hauptwerk Project 1 and Hauptwerk Project 2 pages describe the conversion of specific organ consoles, and the circuit diagrams and software code are in part specific to the existing console hardware. In particular the Ahlborn keyboards in Project 1 have the commonly used 8x8 diode switch matrix while the Makin keyboards in Project 2 have a more unusual serial shift register scanning arrangement. Also the Ahlborn keyboards have a 74138 TTL three to eight way decoder chip on board, so it is only necessary to use three digital outputs on the Arduino board to drive the matrix rows. These three outputs can be paralleled across two or more keyboards and the 74138 chip-enable input driven from further digital outputs on the Arduino to determine which keyboard is actually scanned. In this way the eight output lines from the 8x8 switch matrices can be paralleled and the whole thing fits within the twenty I/O pins available on an Arduino (fourteen plus the six analogue lines that can also function as digital I/O).

By wiring a 74138 into the pedalboard reed switch 4x8 matrix it would have been possible to interface both manuals and the pedals using a single Arduino and still scan all 154 contacts fast enough to avoid any perceptible lag, and I did try this just to prove the point.

Why the Arduino Leonardo

Initially I MIDI-fied the keyboards with an Arduino Uno, the original board in the Arduino family. However this required an external MIDI to USB adapter cable costing more than the Arduino and tied up two of the Arduino's digital lines (A0, A1) for TX/RX, although only TX was used here.

I then discovered the Arduino Leonardo, a variant of the original Arduino Uno that allows the keyboard to appear at the USB port as a USB device without any programming required, while not impeding the normal operation of the Arduino IDE compile/upload process via the USB port, something that is not easily achievable with the Arduino Uno. The MIDIUSB library from Gary Grewal (install from the Arduino IDE Library Manager) takes care of MIDI communication via the USB port. Note that the USB communication protocol is completely different to traditional serial communication, with all data transfer initiated by the host computer on a polling basis. This is why is is possible to connect multiple devices in parallel using a simple USB hub, very useful when converting old organs to Hauptwerk and something one cannot do with serial ports.

Why use multiple Arduino Leonardos

I have described how to use the Arduino in specific cases, where the organ hardware lends itself to interfacing more than one keyboard to a single Arduino. However when starting from scratch with keyboards and pedalboards that just have electrical contacts and nothing else, or perhaps already wired up in an 8x8 diode matrix but nothing else, then the lowest cost and most practical route is to wire up the 8x8 matrix if not already created and then simply connect the sixteen lines directly to the Arduino. If Arduinos costed $100 or more then it would be cost-effective to use just one and implement external circuitry using 74138s (as with the Ahlborn example). However Chinese copies of the Arduino cost $10 and it is cheaper and simpler to use one for each keyboard and pedals (and another for stops and thumb/toe pistons if you have them on the console) than to create an expansion board with 74138s. Each Arduino can be mounted close to each keyboard and the pedals, avoiding inter-cable capacitative problems if long cable runs were used.

Simple Scan

The keyboard contacts are wired up as an 8x8 diode matrix and connected directly to an Arduino Leonardo, using pins D8-D11 plus A0-A3 to drive the rows and D0-D7 to sense the key presses. The Arduino is powered from the computer so no external power supply is required (see circuit).

This code example sets up the pins on the Arduino Leonardo as inputs with pull-up resistors or outputs and then loops through the matrix scan, using a register to save the state of the previous scan and compare it with the current scan so that MIDI signals are only sent when a key state changes, not when it is held in either state (see code).

As a visual aid to correct operation the on-board LED associated with pin 13 lights when bottom C on the keyboard is pressed. In addition a short pulse is generated on pin 12 at the start of each loop to synchronise an oscilloscope, both to discover how fast the keyboard is being scanned and as an aid to troubleshooting.

The same arrangement can be used to scan a 4x8 matrix on a pedalboard. Either modify the code for four rows instead of eight, or just use the board and code unchanged - it won't find the non-existing rows but will work anyway.

The operation of the Arduino board and scanning code can be checked in isolation, without the need to conect it up to a keyboard. Just select the board in Hauptwerk Organ Settings >> Keyboards and then use a jumper lead to connect any row to any column and the appropriate note will sound. Since only one note is being requested at a time it does not matter that no diodes are present.


Cable Capacitance

The correct operation of the diode matrix scanning method relies on each row returning high before the following row is brought low, otherwise unwanted notes will sound. The rows are pulled high through internal resistors (20-50K) on the Atmel microprocessor, however capacitance between the row wires and ground will slow up the voltage rise, to the point that one row is not high before the next is set low, as this oscilloscope trace shows:


This was demonstrated with an unusually long length of cable by way of experiment, and fixed (if it had to be) by adding 1K pull-up resistors:


A 100 microsecond delay is programmed immediately after each row has been set to give the row voltages a little more time to settle down. Each scanning loop takes around 1.6 milliseconds, so the keyboard is scanned over 600 times a second, which is considerably more than fast enough.