This is only a preview of the May 2022 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
64-KEY
MATRIX
Part 2
by Tim Blythman
In the last issue we described low-cost hardware that you can build to work
with MIDI, including a comprehensive MIDI Encoder Shield and a MIDI Key
Matrix to drive it. We have developed some more software to make even
better use of this hardware, and we’ll show you how you can even use it with
Android smartphones and tablets.
T
he first part of this series
showed how to build a simple
MIDI Key Matrix, consisting of a
grid of 64 tactile switches. These are
scanned by an Arduino-compatible
Leonardo board, which acts as a MIDI
Encoder. It uses its USB peripheral to
generate MIDI messages which can
be received on a personal computer
running synthesiser or digital audio
workstation (DAW) software.
It’s also fitted with a pair
of MIDI-standard 5-pin DIN
sockets. These are configured as MIDI-out and
MIDI-in ports. MIDI-out
ports generate ‘hardware’
MIDI messages which can
be fed to the MIDI-in port on
a device such as a MIDI synthesiser.
Thus, the MIDI Key Matrix and MIDI
Encoder together form a generic MIDI
output device, suitable for triggering
sounds and notes on any number of
MIDI-capable devices that have either a
USB or 5-pin DIN MIDI-in connection.
It also has a very basic onboard synthesiser which delivers notes to a small
1W speaker as key events occur. On
its own, it forms a very simple musical instrument.
While it’s possible to wire up the
matrix of 64 keys by hand, we also
showed how to build a PCB-based
Switch Matrix, which simplifies this
greatly. The Switch Matrix provides
14
support for several differently-sized
tactile switches.
We expect some people will think of
new and interesting ways to use this
hardware. This might include custom
programming of the MIDI Encoder to
perform a specific role in a MIDI setup.
Or it might involve using
the Switch Matrix to simplify interfacing to hardware in an
unrelated project.
If you want to know more about
MIDI’s background and workings, see
the ‘What is MIDI?’ panel on page 19.
LED Matrix
While we intended to create a cheap
and useful MIDI input device, we’ve
used the ample space on the Switch
Matrix PCB to add extra features. In
particular, there are pads to allow illuminated switches to be fitted.
These are also connected in matrix
fashion back to a pair of 8-way connectors (CON3 and CON4), with a series resistor for LED current limiting
on each row.
We showed some basic code to drive
the LEDs in the first part, but the original hardware couldn’t sense keypresses and drive the LEDs at the same time.
We will now address that.
Leonardo limitations
The problem is that there aren’t
many pins spare on the Arduino Leonardo; certainly not
enough to drive the LEDs
and scan the switches simultaneously.
Indeed, there aren’t
many Arduino compatible boards around that
would allow that and still
provide a USB peripheral.
The Arduino Mega has enough
pins, but unfortunately, its USB support is limited to serial data through
a separate USB-serial IC.
In the last issue, we noted that if
you just want to light up all the LEDs,
you can simply connect power rails to
CON3 and CON4 on the Switch Matrix PCB.
Practical Electronics | May | 2022
But if you want independent control of the LEDs, the
simplest approach is to add a second microcontroller board.
You might want to light up each key as a prompt to indicate which one should be pressed next. This could be handy
as a learning tool, helping to learn a musical piece by rote.
Another example would be to light up the LEDs in time
with the keys that are being pressed. This is what we’ve
done with our example code.
A tale of two micros
To light up the keys in time with the keypresses requires
communication between the two micros, even though
we’ve already established that one of them doesn’t have
many pins to spare. Our trick is to borrow one that’s already being used.
The MIDI specification supports so-called SYSEX (System Exclusive) messages. These messages are intended
to allow manufacturers of MIDI equipment to send custom data that doesn’t fit into the standard messages. They
can be used (for example) to send audio sample data between devices.
Many manufacturers have specific identifiers, but the
0x7D identifier can be used for development purposes, and
that’s what we’re doing here. Using this identifier means our
data won’t be confused with another manufacturer’s signals.
The System Exclusive message that we send consists of
a status byte 0xF0, identifier 0x7D, followed by the ASCII
codes for ‘SC’. These extra characters reduce the chance
that the data could be misunderstood by other equipment.
We follow this with any number of data bytes in the
range 0x00 to 0x7F, which gives us 128 codes. Codes 0-63
turn off LEDs 0 to 63 respectively, while codes 64-127 turn
on one of them.
Any data byte greater than 0x7F (ie, with the most significant bit set) ends the SYSEX message, although we send
0xF7 as this is the defined ‘End SYSEX’ command. All MIDI
equipment understands this, and thus everything remains
in sync, ignoring data inside these packets.
We send this data out on the MIDI out port. Most MIDI
equipment will ignore these bytes, so it won’t interfere
with our note messages.
It’s then a simple case of receiving that data and displaying it on the LEDs. We’re using a second Leonardo
board to do this.
Because MIDI data is no more than a serial bitstream at
31,250 baud, we set up a state machine to monitor the incoming data.
Once it has received the bytes 0xF0, 0x7D, ‘S’ and ‘C’,
it assumes that any following data bytes are commands to
turn the LEDs off and on as described above, until it receives a byte above 0x7F, which resets the state machine
to wait for the sequence again.
The LEDs are multiplexed by a timer interrupt, which
ensures that each column receives an equal amount of
time and thus the LEDs are driven with even brightness.
The code scans through each column in turn, lighting up
the LEDs according to the previously received commands.
LED driving hardware
Since our LED Driver uses standard MIDI packets, the deluxe way to assemble this is to use two Leonardo boards,
each topped with a fully kitted-out MIDI Encoder.
A standard MIDI cable from the MIDI-out port of the unit
programmed as the MIDI Encoder is connected to the MIDI-in port of the unit programmed as the LED Driver; each
unit is supplied with power via its USB port.
The LED connections on CON3 and CON4 of the Switch
Matrix are connected to CON2 and CON1 on the MIDI
Shield, as described last month in Part 1. So the MIDI
Practical Electronics | May | 2022
Here’s the full rig, with an
Android phone hooked up
to the MIDI Encloder and
Matrix. A second Leonardo
drives the LEDs on the
Switch Matrix.
Encoder Shield only needs the MIDI-in, CON1 and CON2
headers to act as the LED Driver.
In line with our cheap-and-cheerful philosophy for this
project, we have a simpler solution.
Fig.5 shows the minimal wiring needed, with the MIDI
Encoder Shield at left and the LED Driver board (using the
same PCB) on the right.
Our photos show this minimal arrangement too. We have
soldered a 2-way female header to each PCB at the 5V/GND
connections, and these are connected by a pair of jumper
wires (red and blue).
The LED Driver PCB is simply our shield PCB from part
one fitted with headers to break out the connections. There
are also headers underneath to connect to the Leonardo.
The easiest way to solder these is to insert the headers
into the Leonardo’s sockets, slot the PCB onto the headers
and then solder them. The pins are thus square and aligned.
The Switch
Matrix fitted out with a
full complement of 3D-printed
key caps.
15
Fig.5: this wiring
bypasses the MIDI
connectors and allows
the MIDI Encoder to
both power and
communicate directly
with the LED Driver.
Naturally, there is no
isolation in this case!
Although two wires are
shown for data (green
and orange), only one
is needed as the pins at
each end are connected
by PCB traces.
We used socket headers for CON1 and
CON2 to allow simple plug-plug jumper
wires to be used, although these could
even be soldered in place. Make sure
to wire pin 1 of CON2 (Shield) to pin
1 of CON3 (Matrix) and pin 1 of CON1
(Shield) to pin1 of CON4 (Matrix).
The data line is connected at the
MIDI Encoder end by piggy-backing
it onto the TX jumper at JP1, while
the LED Driver is connected to the RX
header at JP1.
Parts l is t –
additio nal p arts f o r L E D Driv e r
1 double-sided PCB coded 23101211,
69 x 54mm from the PE PCB Service
1 Arduino Leonardo module
1 10-way pin header
(shield headers to Leonardo)
1 8-way pin header
(shield headers to Leonardo)
2 6-way pin headers
(shield headers to Leonardo)
2 8-way pin headers or sockets
(to connect to Matrix PCB)
2 8-way jumper wires
(to connect to Matrix PCB)
3 jumper wires
(to connect to MIDI Encoder)
If you have a bare Leonardo at either
end, you could wire from the TX pin
(pin 1) of the MIDI Encoder Leonardo
to the RX pin (pin 0) of the LED Driver Leonardo.
Don’t forget that you will need the
Switch Matrix variant with illuminated switches fitted; they should have
their anodes oriented towards the top
of the PCB.
Software
We’ve updated the MIDI Encoder software also to output the SYSEX LED
data, so upload the MIDI_ENCODER_
LED_SERIAL_OUT to the Leonardo
which is acting as the MIDI Encoder.
The LED Driver Leonardo should
similarly be programmed with the
MATRIX_LED_DRIVER_SERIAL sketch.
Once that is done, and both boards
are powered up, pressing any key
should cause the corresponding LED
to light up. If they don’t match up,
try swapping or rotating the connections to CON3 or CON4 of the
Switch Matrix.
If you wanted to do something fancy, like have the LEDs ‘radiate out’
from each key pressed, you just need
to make modifications to the MATRIX_
LED_DRIVER_SERIAL sketch.
3D printed keycaps
The 16mm spacing on the Switch Matrix is a good compromise between
compactness and usability. If the keys
were much closer, there’d be a higher
chance of pressing more than one key,
while a wider spacing would quickly
cause the PCB to blow up in size.
We made the off-hand comment
last issue that 3D-printed keycaps
would be an economical way to add
finishing touches to an illuminated
matrix. While 12mm tactile switches can be found with large buttons
which are easy on the fingers, there
are fewer options for the smaller illuminated parts.
Unfortunately, many small, illuminated switches only support small
keycaps (around 10mm), which would
look very odd on the 16mm spacing we
have used. So we designed a 3D-printed keycap to suit the ILS series switches that we used.
We printed some of these in a translucent PLA filament (Jaycar’s Cat
TL4274), and it helped diffuse the light
from the LEDs, although as they are so
small, they were a fussy fit.
We’ve made the 3D files available for
download if you want to 3D print your
own keycaps and try them for yourself.
MIDI glossary
Note-on and Note-off messages: The Note-on and Note-off messages correspond to events that occur during music playback
and are the only messages that the Encoder generates. They
include information about the channel, velocity (see below)
Channel: Each MIDI message can specify one of 16 channels, allowand note number.
ing data to be routed to or from different instruments while maintaining its source or identifying that it should be played back on Status: The first byte of a message is called the status byte and
is marked as such by having its most significant bit set. Typi
a particular instrument. ur software uses a single, fi ed channel
cally, the lower nybble (four bits) of the status byte contains a
which can be changed in the code.
four-bit value indicating the channel number.
Message: The smallest unit of MIDI data is a message and corresponds to a single event (like a piano key being pressed or Velocity: Velocity is how fast the key on a musical instrument is
pressed, but is usually manifested as how loud a note sounds
released) or a setting that is to be changed.
(which is related to how fast, for example, a piano key is pressed).
Our software uses velocity 64, which is the default for deNote number: Each musical note is associated with a number in
vices that can t detect key velocity. ike ote number, it can
the range 0-127. Middle C is assigned to number 60. Our dehave the value 0-127, with 0 also corresponding to Note-off
sign implements 64 of these (in an 8x8 matrix), with the range
for some devices.
start and end defined in the code.
16
Practical Electronics | May | 2022
Here’s the front
and rear PCB
photographs
which match the
overlay diagrams
at left. Obviously,
there’s not much
on the rear
side of the PCB
except sockets,
as shown. These
plug directly into
the ‘sandwiched’
Leonardo board.
The accompanying photos show what
the result looks like.
MIDI on Android
While testing our different MIDI hardware variants, including the MIDI Encoder PCB and the Switch Matrix PCB,
Screen1: the FluidSynth MIDI
Synthesiser App has few controls, but
that is part of what makes it easy to
use. Once the MIDI Encoder (or other
MIDI device) is plugged in, it becomes
available from the top of the screen.
Screen2: the MIDI Encoder appears
as an Arduino Leonardo device
when selected. The number shown
is different every time the Encoder is
reconnected, but this doesn’t seem to
cause any problems.
Practical Electronics | May | 2022
we wondered if there was a way to connect the MIDI Encoder to a smartphone.
With many people possibly having an
old mobile phone around that could
be repurposed, such an arrangement
would be a quick, cheap and easy way
to create a useful musical instrument.
We only looked at Android devices
because that is what most of us use.
Our minimal research suggests that
this might be possible on Apple devices (such as iPhones), but it’s not
something we’ve delved into.
Note also that there are many different types of Android phones, and we
can’t claim that this will work with
all of them.
If you can check that your phone
supports USB OTG (on-the-go) and has
a fairly recent Android version, then
you have a decent chance of success.
Android is not restricted to mobile
phones; some tablets run Android,
plus some other devices (for example,
smart TVs).
What you need
One thing that you’ll almost certainly
need is a USB on-the-go (OTG) adapter. This allows the USB socket on your
phone to behave as a host device instead of a peripheral device. Not all
phones support OTG, but it’s quite
common these days.
The OTG adapter will have a plug
that suits the USB socket on your
phone (micro-USB or USB-C) and a
USB-A socket (such as you might find
on a computer). For example, Jaycar
Cat WC7725 (micro-USB lead) or Cat
WC7709 (USB-C lead) should work. Or
use Altronics Cat P1921 for micro-USB
or Cat P1924 for USB-C.
Smaller adaptors are available
which do not have any leads; they connect the devices directly. We like them
because they’re small enough to carry around in your pocket or bag. You
can see one of these in our Android
phone photo.
Indeed, an OTG adapter is a handy
thing to have these days, with many
phones having support for USB key-
boards, mice and flash drives. We’ve
even seen an app that lets you use one
to program Arduino boards!
Screen3: we’ve only used the Settings
option from the menu. Recordings are
a paid premium feature that we did
not test as we figured it would be easy
enough to connect a 3.5mm stereo aux
lead to the phone’s jack to record audio.
Screen4: it’s only necessary to download
the SoundFont file during initial setup.
After this, you will just need to check
that the MIDI Encoder is selected on the
main screen before using it.
17
A pair of 2-pin header
sockets soldered back to
back and with their pins
bridged can be used as a
jumper that provides extra
sockets to tap into. We used this arrangement to break out
the MIDI signal from the MIDI Encoder board from JP1,
with an extra jumper wire leading to the LED Driver board.
Our basic setup allows access to myriad options through
musical instrument choices and SoundFont files. The
Encoder PCB here is only fitted with headers to connect to
the Switch Matrix.
Having said that, writing Arduino sketches on such a
small screen isn’t the easiest thing in the world.
Note that your phone and apps need to have support for
the devices you want to attach. Fortunately, MIDI is supported as a Device Class Definition by the USB standard,
meaning that any compliant USB MIDI device should work,
without needing specialised drivers.
MIDI Apps
We don’t have any affiliation to the following Android MIDI
Apps; we found them by searching the Google Play Store.
The first one we tried was called MIDI Keyboard, which
was able to recognise the attached Leonardo, but was limited to a single piano instrument. So we kept looking to
see what other options were available.
The second App, called FluidSynth MIDI Synthesizer,
gave a few more options. At the time of writing, it generally had positive reviews, and it also appeared to support
downloadable SoundFont (.SF2) files. There is a paid upgrade available to allow recording on your device, but
we were well entertained by simply playing sounds back
through our phone speaker. A connection using a 3.5mm
stereo aux cable should be sufficient to connect the audio
to other equipment for amplification or recording.
Installing the App was quite simple. It works with Android 6 and later. We found that it needed permission to
access device storage; this is required to access the downloaded SF2 files.
Screen1 shows its initial screen. When a MIDI device
(like the MIDI Encoder) is plugged in via an OTG adapter,
it appears in the dropdown menu at the top of the screen.
Screen2 is shown when such a device is selected, giving control over the MIDI device. From the menu icon
at top left, select Settings (Screen3) and choose ‘Download a SoundFont’. The Chaos V20 option (Screen4) is
the smallest of the download options, and gave many
different sounds.
Returning to the main screen (Screen2) then allows the
various channels and instruments to be configured. Having
done that, you can use the MIDI Encoder to play through
the phone’s speaker.
The Channel and Instrument dropdowns allow specific
instruments to be allocated to different channels. Once each
channel/instrument combination is set, press the ‘SEND
PROGRAM’ button to activate it.
By default, the MIDI Encoder only delivers data on channel 1, but we have created a software variant that outputs
on four channels instead.
The Arduino sketch is named MIDI_ENCODER_4_
CHANNEL, and when uploaded to the MIDI Encoder and
Switch Matrix hardware, will generate events on channel
1 when S1-S16 are pressed, channel 2 for S17-S32, channel 3 for S33-S48 and channel 4 for S49-S64.
You might find that this combination works very well
for triggering sound effects. Look near the bottom of the
instrument list, among the percussion instruments.
Conclusion
With the addition of a second Leonardo board and not
much else, we can add controllable LEDs to our MIDI Encoder and Matrix. And since our system uses the existing
MIDI hardware, our software could be modified to give
MIDI control over other things too.
Using our MIDI Encoder and Switch Matrix with an old
Android phone is a very economical way to access a full
range of playable sounds.
Fig.6: the arrangement of a typical ‘Note-on’ message. The first byte has its most-significant bit is set to flag that it is the
start of the message. The high nybble of 9 means that this is a Note-on message (8 would mean Note-off), with the low
nybble containing the channel number. The following bytes carry 7-bit values for note number and velocity.
18
Practical Electronics | May | 2022
What is MIDI?
MIDI stands for ‘Musical Instrument Digital Interface’. It’s a standardised
system for communicating between electronic musical instruments,
keyboards, controllers and sequencers (including PC-based sequencers). The original MIDI standard was agreed on by a group of musical
instrument makers in 1983, and has been used and extended since then.
The last time we looked at MIDI was before the Arduino phenomenon had fully developed. Arduino has made it very easy to interface
electronics to MIDI equipment. People have created an assortment of
controllers, instruments and even synthesisers with a variety of sounds
using Arduino.
A MIDI 2.0 standard was released in January 2020, and is intended
to be backwards compatible with the original I I specification. The
new standard is not yet in widespread use and is still undergoing testing.
Electrical protocol
The standard specifies events that occur such as the start or end of notes
being played), which are encapsulated in messages that are transmitted
between devices. The ‘original’ MIDI relies on serial data communication
at 31.25kb/s using asynchronous 5mA current-loop signalling, with the
current provided by the transmitting end.
This means that each byte of a MIDI message takes only 320µs to
be transmitted (counting start and stop bits). Since MIDI messages are
either one, two or three bytes in length, this means that over 1000 such
messages can be sent each second via a single MIDI cable.
Each MIDI cable carries only one signal, so for bidirectional communication, two cables must be used. The cables themselves use shielded
two-conductor wire.
ll I I cables are fitted with standard
pin I plugs at both
ends. However, only pins 4 and 5 are used for the actual current loop
signalling (wired 4-4 and 5-5). Pins 1 and 3 are left unconnected, while
the shield braid is connected to pin 2 at each plug.
Inside MIDI equipment, pin 2 is connected to earth only on MIDI OUT
sockets. This provides shielding via earthed cable shield braids without
creating earth loop problems.
nlike most other current loop signalling protocols, current only ows
in a MIDI link when data is being transmitted. This allows MIDI cables
to be ‘hot’ plugged and unplugged without any problems, as long as
they are not in active use.
All MIDI inputs are provided with 3kV of galvanic and electrostatic
isolation via an optocoupler to prevent equipment damage due to wiring
errors or component faults. For proper MIDI communication between
equipment, a MIDI OUT or MIDI THRU socket at one end must be connected to a I I I socket at the other.
That is what we call the ‘hardware’ MIDI implementation. There also
exists a USB implementation that allows for much faster communication. This is not merely a USB-serial type of translation (although software exists to use USB-serial converters for this purpose).
Jaycar’s Cat XC4934 USB MIDI Interface is one example of hardware
for translating between these two protocols for device interconnection.
Since MIDI is not much more than a series of data bytes, various other hardware transports have been used. These include ire ire,
and even wireless transmission methods.
Logical protocol
ach message starts with a byte that has its most significant bit set, and
the remaining bytes for practically all messages has the most significant bit cleared. This means it is very easy to serialise and packetise the
data for conversion over other transports.
Of course, you’re not limited to using our MIDI Encoder with this App.
You might want to modify the Arduino sketch to provide your own interface, or even plug in another USB
MIDI device.
Practical Electronics | May | 2022
There is a single main controller or sequencer in most MIDI systems
from which most of the MIDI messages originate (often the computer,
or perhaps a keyboard or digital audio workstation). When these messages must be sent to more than one instrument, they can be distributed in either ‘star’ or ‘daisy-chain’ manner, as desired.
It’s possible to combine two MIDI streams. However, a device to do
that is not trivial to implement, as it must handle the case when two
messages arrive at the same time and queue them for consecutive output without delaying them excessively.
There’s no need to worry much about the actual code messages sent
over the I I links. owadays, that is handled by a sequencer or other
software running in the and by firmware running in the other instruments and keyboards.
It’s probably enough to know that most MIDI messages are short
commands to allocate a particular instrument to a specific channel, to
tell it to start or stop playing a particular note, to change the instrument’s
attack/decay or other performance parameters, and so on.
As mentioned earlier, these commands are generally in the form of
three byte messages see ig. . owever, some configuration and or
system management messages are only one or two bytes long. Longer,
equipment specific configuration messages also e ist, for e ample, to
load digital audio samples into a sampler.
File format
Using a PC-based music editing and sequencer program, and perhaps
with a MIDI music keyboard to feed in the actual notes, you can assemble a complete sequence of MIDI commands to play a piece of music
– eg, on the ‘instruments’ in a synthesiser.
The synthesiser can then be made to ‘perform’ that piece of music
by merely sending the sequence to it, via the MIDI link. When you’re
happy with the result, you can save the sequence on disk as a MIDI
music file. These have a standardised format and are identified with a
‘.MID’ extension.
The . I file format is essentially a series of I I messages after
a header, stored in chunks in a similar fashion to .
file chunks
and intermingled with timing data to ensure that the messages can be
played back correctly.
It s important to realise that although a I I music file may look superficially similar to a .
file of a digital sound recording, it s really quite
different. It’s more like an electronic equivalent of sheet music – simply
a sequence of detailed instructions describing how to play the music.
In this case, they are instructions for electronic instruments rather
than for human players. And depending on the instrument that is providing playback, the sound that is output can vary substantially.
Software
The advent of Arduino-compatible boards with an integrated USB peripheral such as the Leonardo (based on the ATmega32u4 microcontroller)
makes it easy to implement a USB MIDI interface. And there is much
PC software around that can work with USB MIDI devices.
While we were testing our design, we experimented with MuseScore
(https://musescore.org/en), an open-source score-writing program
and Anvil Studio (www.anvilstudio.com), a digital audio workstation
program. Both can act as a synthesiser, producing sounds based on
incoming MIDI messages.
If you would like to delve further into the technicalities of MIDI,
then take a look at the MIDI Manufacturers Association website here:
www.midi.org/specifications
Links
SoundFont files
https://bit.ly/pe-may22-sfz
Reproduced by arrangement with
SILICON CHIP magazine 2022.
www.siliconchip.com.au
FluidSynth MIDI Synthesizer App
https://bit.ly/pe-may22-fsms
19
|