This is only a preview of the April 2021 issue of Silicon Chip. You can view 41 of the 112 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "Digital FX (Effects) Pedal - Part 1":
Items relevant to "Refined Full-Wave Motor Speed Controller":
Articles in this series:
Items relevant to "High-Current Four Battery/Cell Balancer - Part 2":
Items relevant to "Arduino-based MIDI Soundboard - Part 1":
Purchase a printed copy of this issue for $10.00. |
64-KEY
This simple project turns an Arduino into a MIDI key matrix. These are
popular with musicians for triggering samples, but commercial versions cost
hundreds of dollars. Ours costs a fraction of that, and you can customise it by
changing the Arduino software. It supports regular or illuminated buttons and
can also be programmed to act as a MIDI pass-through, among other roles.
T
his project was inspired by a
reader request to create something similar to the Infra-red
Remote Control Assistant project
from July 2020 (siliconchip.com.au/
Article/14505) but for MIDI.
MIDI is a standard that allows musical instruments and computers to
communicate. Just in case you didn’t
know, MIDI is an acronym for Musical
Instrument Digital Interface.
A MIDI encoder takes inputs from
a musical instrument (such as a keyboard) and converts them into MIDI
format. Such a device could be connected to a computer to record playing, or
to a synthesiser, to turn the MIDI data
back into music.
Such devices commonly utilise 8x8
switch matrices to generate up to 64
different MIDI messages; they effectively emulate a five-octave keyboard with
some keys to spare. This allows you
to easily interface with a synthesiser
or digital audio workstation (DAW) to
generate music from real-world inputs.
The Arduino community has done a
lot of the work for this already, creating libraries which can generate MIDI
messages both in hardware (as serial
88
Silicon Chip
data) and also as a virtual USB MIDI
device (which many DAW PC applications can read).
The basic system can be implemented with not much more than
an Arduino Leonardo development
board. The Leonardo is based on an
ATmega32U4 microcontroller, which
has a USB peripheral. Along with an
Arduino library, that makes this job
much easier.
To do this, the Leonardo scans columns assigned to eight of its I/O pins
and checks if they have been shorted
against any of eight other I/O pins, assigned to the button rows, thus giving
up to 64 combinations. These 16 I/O
pins are then wired to an array of tactile switches or pushbuttons which
form the keys.
This simple system cannot detect
more than one ’closure’ at a time, so any
state that is identified as having more
than one button pressed is reported as
‘nothing pressed’. Some, but not all,
combinations of multiple keys could
be identified, but we have erred on the
side of keeping this simple.
To be able to detect simultaneous
keypresses correctly would require a
Australia’s electronics magazine
diode to be fitted to each switch (and
would also make our simple device
considerably more complicated).
Each key (close or release) event results in a MIDI event being sent over
USB. We are using the Leonardo’s hardware serial port to generate a hardware MIDI signal. This can then be fed
through our MIDI Shield, described
below, to convert it to the correct electrical format to go to a synthesiser etc.
Note that if all you want to do is send
MIDI events to a computer over USB,
you don’t even need to build the Shield.
But you probably will want to assemble
our Switch Matrix PCB, also described
later, as wiring up the switches manually would be a lot of work!
To help make this project more useful, we’ve also added a very basic synthesiser to the Leonardo. A PWM signal
is produced from pin 13, approximating a sinewave at the frequency of the
note being played. The waveform shape
is defined in an array, so it could be
changed to produce a different sound.
This sound can be heard by connecting a piezo transducer between pin 13
and GND of the Leonardo, although
these devices don’t have a great resiliconchip.com.au
Part 1 –
by Tim Blythman
MATRIX
Use this photo
to help guide your
wiring between the
MIDI Shield and the
Switch Matrix PCB.
In all cases, pin 1 goes
to pin 1 (the green
wire), but CON1 on one
PCB goes to CON2 on the
other and vice versa.
sponse to lower frequencies. Hence,
our MIDI shield also provides an audio amplifier which can drive a speaker
(the larger the better – they’re usually
more efficient) for better audio quality.
While we were at it, we thought we’d
also add a MIDI Input to the Shield. As
presented here, all you can use that for
is to replicate the received data directly
to the MIDI Output, allowing this device to act as a basic extender. However, the hardware is set up to allow the
micro on the Leonardo to receive and
decode the incoming MIDI data, so with
appropriate software, it could do a variety of other jobs.
The MIDI Encoder Shield
This small PCB is an Arduino ‘shield’
(aka daughterboard) which adds some
useful hardware for interfacing with
MIDI equipment.
The board effectively combines four
different ‘modules’ which operate independently. That means that, if you
don’t need all of the functions, you can
leave off some of the parts.
These four parts are the interface to
the switch matrix, an audio amplifier,
a MIDI transmitter and a MIDI receivsiliconchip.com.au
er. The circuit diagram for the whole
Shield, incorporating those four sections, is shown in Fig.1.
Switch matrix
Since the switches are intended to
be mounted off-board, we have just
provided some convenient connection
points on the PCB.
CON1 and CON2 are standard
2.54mm (0.1in) pitch headers, and
could be fitted with pin headers or sockets. For prototyping, we recommend
header sockets, as these allow jumper
wires to be plugged in.
CON1A and CON2A have a 3.5mm
pitch and are sized to fit smaller screw
terminals such as Altronics’ P2028.
This is a good way to rig up something
more permanent. You could also solder wires directly to any of these pads.
Note that the pins marked with the
arrows correspond to the ‘lowest’ ends
of each row and column. Thus, shorting the two pins marked with arrows
will give the lowest note. Shorting the
two pads at the opposite ends will give
the highest note.
You will probably not be able to
install both of CON1 and CON1A or
Australia’s electronics magazine
CON2 and CON2A, as the headers will
foul the cable entries for the screw terminals. Thus, you should choose which
of the two you will fit before starting
construction.
Audio amplifier
The amplifier circuit is based around
IC1, an SSM2211 class-AB amplifier
IC, which we previously used in the
AM/FM/SW Radio published in January 2021. It provides a push-pull output at up to 1.5W into 4Ω, so it is a
good choice for low supply voltages.
A 100nF capacitor bypasses its supply
rails at pins 6 and 7.
Jumper JP2 can be used to connect
Arduino pin D13 to the amplifier. If you
want to use another I/O pin to feed the
amplifier, it can be patched into JP2.
IC1 is surrounded by components to
condition the input signal (including
filtering out any high-frequency PWM
artefacts) and to set the gain.
The 1kΩ resistor and 100nF capacitor provide low-pass filtering to remove
PWM switching harmonics from the
generated audio signal. This results in
the 180kHz PWM frequencies being attenuated by around 40dB.
April 2021 89
l
SC
Ó
MIDI SHIELD FOR ARDUINO
The 10µF capacitor provides AC-coupling into the amplifier, while the other
100nF capacitor provides bypassing of
the internal mid-rail reference output
on pin 2 of IC1. This reference rail is
fed directly to pin 3, the non-inverting input, as we are supplying a single-ended signal.
The half-rail reference also biases
the inverting input via the 1MΩ resistor, while the filtered audio comes into
the inverting input via a 10kΩ resistor.
The amplifier’s A output (at pin 5) is
also fed back in to pin 4 via VR1. This
is used to set the gain, and thus the resulting sound volume.
The B output at pin 8 is derived from
the A output using an internal inverting
stage referenced to the mid-rail voltage.
Thus, the overall gain of the circuit is
90
Silicon Chip
Fig.1: the circuit of our MIDI Shield. It is broken
up into four modules: the MIDI input, MIDI output,
audio amplifier and pushbutton matrix interface.
You only need to install parts corresponding to the
parts you wish to use. Note that our sample software
does not make use of the MIDI input (CON4).
double the value of the feedback resistor divided by the value of the input
resistor. The factor of two is due to the
outputs being bridged.
VR1 can be used to linearly set the
output level from zero (at 0Ω) to fullrail (at 10kΩ).
But note that this is limited by the
fact that the outputs can only get within 400mV of the power rails. The pushpull outputs mean that the total maximum swing is around 8V peak-to-peak.
The complementary push-pull outputs also mean that the quiescent state
has both outputs near the mid-rail level, so little (if any) direct current flows
through the speaker, and thus no output
coupling capacitor is needed.
The complementary outputs at pins
5 and 8 are connected to screw termiAustralia’s electronics magazine
nal CON5, for wiring up a 4Ω or 8Ω
speaker. The SSM2211 can deliver up
to 350mA, giving up to 1.5W into a 4Ω
load or about 1W into an 8Ω load.
MIDI transmitter
While the Arduino Leonardo can
generate the MIDI signal in software,
we need some hardware to feed this
to a standard MIDI device, like a synthesiser (which is likely to sound better than our little speaker) or even a
USB-MIDI converter for feeding the
data to a computer.
This is quite simple, as the MIDI interface uses optoisolated connections
at the receiver end.
We use two 220Ω resistors to connect
the 5V supply to pin 4 of 5-pin DIN
socket CON3, and the MIDI signal to
siliconchip.com.au
pin 5. At the receiving end, we expect
another 220Ω resistor and an optoisolator with a forward voltage of around
1.5V, giving a nominal 5mA current
flow when our micro pin is low during
data transmission.
Note that the signal from the micro
must jump from pin 4 of JP1 to pin 3
to reach CON3. The signal itself is just
31,250 baud serial, easily generated by
the Leonardo’s UART peripheral.
JP1 allows the signal to be patched in
from another pin, if you wish to use the
Shield for some other MIDI application.
Otherwise, you’d just leave a jumper
shorting pins 3 & 4.
Note the use of the 5V rail for the return signal. Since the serial idle state
is a high level, this means that no current will flow in the loop when data is
not being transmitted; the same as in
the disconnected state.
CON3’s pin 2 and its DIN shield are
connected to ground at the transmitter
end only, to avoid ground loops.
MIDI receiver
As alluded to above, the MIDI receiver consists of a 220Ω resistor and
an optoisolator connected between
pins 4 and 5 of CON4. Diode D1 protects against a reverse voltage which
another device might apply. So when
pin 4 is a couple of volts higher than
pin 5, the internal LED in OPTO1 is
forward-biased, and thus its output
transistor conducts.
The specifications for the 6N138 suggest that under adverse conditions, the
propagation delay of the 6N138 could
violate the timing requirements of the
MIDI signal. But most MIDI designs
appear to use this device without any
problems.
The circuit is compatible with the
6N137 optoisolator, which, as we noted in our Digital Lighting Controller
article (October 2020; siliconchip.com.
au/Series/351), requires more current to
operate, but is much faster. The nominal 5mA loop current is close to the
minimum recommended for the 6N137,
but should be sufficient under most
conditions.
In either case, the output side of
OPTO1 has power supplied at pins 8
(5V) and 5 (GND), bypassed by a 100nF
capacitor. The output, pin 6, is pulled
up to 5V by a 1kΩ resistor and is pulled
to GND whenever the opto’s LED is forward-biased.
This output signal is fed via pins 1
& 2 of JP1 to the Leonardo’s UART RX
siliconchip.com.au
pin, D1. This can also be patched into
another pin if necessary.
JP1 also offers the possibility of using
the Shield as a MIDI bridge, by placing the jumper across the middle two
pins (pins 2 & 3). This will connect
the output of OPTO1 to the transmitter at CON3, passing any signal straight
through.
This might not be much use on its
own, but could be used in combination
with a connection to the Leonardo’s
RX as a MIDI signal monitor or sniffer.
Switch & LED Matrix
We imagine that most people using
our MIDI Encoder will hook it up to a
bunch of tactile switches in a matrix
to trigger the various notes. You could
do this manually, which is the cheapest option, but it would be a lot of repetitive work.
So we’ve designed a PCB which
breaks out 64 tactile switches to a pair
of eight-way headers, which can be directly connected to the headers on the
MIDI Shield (or even straight to the
Leonardo). We have even incorporated
LED wiring so that you can use illuminated switches.
We’ve designed the Switch Matrix to
fit the larger 12mm footprint switches,
as some of these have nice big buttons
that are easy on the fingers.
We also added footprints to suit
small illuminated tactile switches like
Jaycar’s SP0620/SP0622 or Altronics’
S1101/S1103. These also suit the typical 6mm tactile switches, for which
you might like to add keycaps (eg, 3D
printed ones) for a larger key area.
If you fit illuminated switches, you
can use the separate bank of eight-way
headers to interface their internal LEDs.
Current-limiting resistors are included
for each row.
All these embellishments are optional. Since the original aim was to create
a MIDI Encoder at minimal cost, nothing is stopping you from buying a bulk
lot of simple switches to populate the
Switch Matrix.
Fig.3 shows the circuit diagram for
the Switch Matrix with all parts fitted.
The resistors are only needed if you are
using illuminated switches. The LED
polarity is not fixed by the PCB, but can
be changed by rotating the buttons 180°
on installation.
When the Switch Matrix’s CON1
and CON2 are connected to the MIDI
shield’s CON2 and CON1 respectively (all pin 1 to pin 1), pressing S1 will
Australia’s electronics magazine
trigger the lowest note, S2 the next note
and so on. You can swap or reverse the
connectors to change this order.
The LEDs are similarly wired to
CON3 and CON4, although there is
no corresponding output on the MIDI
shield or Leonardo (since we’ve already
used all the Leonardo’s pins). Thus,
if you want individual LED control,
you’ll need a separate circuit.
Later, we’ll describe some sample
Arduino code to light up the LEDs using simple timer-based multiplexing.
Alternatively, if you just want the
LEDs to light up, you could connect
all of CON3’s pins to a 5V supply and
CON4’s pins to ground (assuming the
LED cathodes are towards the top of
the PCB).
Switch options
If you wish to use non-illuminated
switches, then you should ensure that
they suit the footprints we have used,
which measure 6.5mm x 4.5mm for the
smaller parts and 12.5mm x 5mm for
the larger parts.
We recommend using a larger switch
with a large actuator surface for easeof-use.
The switches are installed on a
16mm pitch, so if you are using separate keycaps, make sure they are smaller than 16x16mm.
For illuminated parts, the footprints
suit some smaller switches. One critical
factor here is to check the LED polarity
before fitting. This will depend on the
design of your drive circuitry. For our
examples, we have assumed that the
LED anodes are towards the top (S1S8) of the PCB.
Our design assumes that the pins in
the corners of the switch are shorted
when the button is pressed, and open
the rest of the time. Since most switches have pairs of pads connected internally lengthwise, that will typically
be the case.
Shield construction
Before assembling the Shield, decide
which set of the four sections you will
need. If you are unsure, it’s probably
safest to build them all.
Note that the MIDI receiver section
is not used in our MIDI Encoder software, although we would be inclined
to build it anyway, as we think the
Shield will be a great way to tinker
with MIDI, which you might want to
do in the future.
Also, you will find it is harder to fit
April 2021 91
parts later, especially after the headers are fitted.
We will explain the construction
procedure as though all parts are to be
fitted, but you can omit any you don’t
need. Refer now to the Shield PCB
overlay diagram, Fig.2, along with the
same-size photo, which show which
parts go where.
Start with IC1, as it is the only surface-mounted part. We chose the SOIC
(small outline IC) version as the alternative is a DFN (dual flat no-lead) package, which is a lot harder to solder. We
recommend that you have some solder
flux paste, tweezers, a magnifier and
solder wicking braid on hand for fitting this chip.
Check the orientation of the chip; pin
1 goes to the pad nearest the notch on
the silkscreen. The chip itself will be
marked with a bevel along one edge,
which corresponds to the stripe shown
on the PCB (best seen from end-on), and
also with a dot near pin 1.
Apply some flux to IC1’s pads on the
PCB and rest the chip in place. Apply
a small amount of solder to the tip of
the soldering iron and touch it to one
pin to tack the IC in place.
Check that the IC is flat and square
with all pins within their pads. If not,
adjust the IC’s position with tweezers
while melting the solder on the pin.
Once you are happy that it is correctly
placed, carefully solder the remaining
pins. This can be done by applying a
little more flux to the top of the pins
and adding some solder to the iron’s tip.
Touch the tip of the iron against
where each pin meets its pad and the
flux should induce a small amount of
solder to run into the joint.
Don’t worry about solder bridges
between pins as long as the IC is correctly placed.
Once all the pins are secured, check
for bridges with a magnifier. Apply
more flux and rest the braid on top of
the affected pins. Gently rest the iron
on the braid until the solder melts and
carefully pull it away from the IC once
it draws up excess solder.
Clean up any excess flux using the
recommended solvent. Isopropyl alcohol works well for most fluxes, although you should take care as it is
flammable.
Through-hole parts
Now you can mount the resistors.
Check their values with a multimeter
if you are unsure of the colour codes,
92
Silicon Chip
Fig.2: use this overlay diagram
and the photo above as a
guide when assembling the
MIDI Shield PCB. Apart from
IC1 (which is the amplifier
for the speaker), all parts are
common through-hole types.
While IC1 is an SMD, it can be
soldered without any special
tools, although we recommend
using tweezers and a magnifier.
Solder it first so that you aren’t
restricted by nearby parts.
and match them to the values printed
on the silkscreen. Next, fit the capacitors, as shown in Fig.2.
There is only one diode, and it must
be soldered with its cathode band
aligned with the mark on the PCB silkscreen.
Trimpot VR1 will only fit in one orientation, but you might need to bend
its leads slightly, after which it should
snap into place.
After soldering its leads, check that
it is set near its mid-point, which is a
safe default.
OPTO1, like IC1, must be orientated correctly. The notch in its body
should face towards the centre of the
PCB, with pin 1 on the side nearest the
(currently vacant) DIN sockets. We used
an IC socket so that we could test out
a few different optoisolators, but we
recommend that you solder it directly
to the PCB.
Install JP1 and JP2 next, with the
jumper shunts inserted to hold the pins
in place. They also provide a bit of thermal insulation if you need to manipulate the jumpers while soldering them,
although this should be done with care
as they can get quite hot.
Solder the headers in place, ensuring
that they are flat against the PCB and
Australia’s electronics magazine
straight, then move the jumpers to the
default positions shown in Fig.2.
Now fit either CON1 or CON1A, and
CON2 or CON2A. If you are fitting the
screw terminal headers (CON1A and
CON2A), ensure that these are orientated with the wire entry holes facing
out of the PCB.
If you have a collection of shorter terminals, slot them together into a single
block using tabs on their ends before
soldering them.
Mount CON5 next. Like CON1A and
CON2A, make sure that the wire entries face the edge of the PCB. Follow
with the DIN sockets (CON3 & CON4).
They should only fit in one way. Solder one pin and check that they are
sitting correctly before soldering the
remaining pins.
The final parts are the headers used
to attach the Shield to the Leonardo
board, which are mounted on the underside of the board.
The easiest way to manage this is to
fit the headers to the Leonardo board,
then slot the Shield onto the headers.
Check that the PCB is flat and if necessary, trim any long leads on the underside of the Shield that may be preventing it from sitting flat. Then solder
each pin from the top side of the PCB.
siliconchip.com.au
SC
Ó
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
l
MIDI SHIELD SWITCH MATRIX
siliconchip.com.au
Fig.3: there isn’t much to the Switch Matrix circuit. Each pin
of each connector goes to either a row or column of contacts
on the switches or LEDs.
Australia’s electronics magazine
April 2021 93
Parts list – MIDI Shield
1 double-sided PCB coded 23101211, 69 x 54mm
1 Arduino Leonardo module
2 6-way pin headers (part of the Arduino shield headers)
1 8-way pin header (part of the Arduino shield headers)
1 10-way pin header (part of the Arduino shield headers)
Switch matrix interface parts
2 8-way pin headers or sockets (CON1,CON2) OR
2 8-way 3.5mm screw terminals (CON1A,CON2A) [eg, 8 x Altronics P2028]
wiring to switch matrix
Audio amplifier parts
1 4-8Ω 1W loudspeaker
1 2-way 5/5.08mm-pitch screw terminal (CON5)
1 2-pin header and jumper shunt (JP2)
1 SSM2211 audio amplifier, SOIC-8 (IC1)
3 100nF 63V MKT capacitors
1 10µF through-hole ceramic capacitor (ideally 5.08mm lead pitch)
1 1MΩ 1% 1/4W metal film resistor
1 10kΩ 1% 1/4W metal film resistor
1 1kΩ 1% 1/4W metal film resistor
1 10kΩ mini horizontal trimpot (VR1)
MIDI output parts
1 5-pin, 180° DIN socket, right-angle PCB mount (CON3)
[eg Jaycar PS0350, Altronics P1188B]
1 4-pin header and jumper shunt (JP1)
2 220Ω 1% 1/4W metal film resistor
MIDI input parts
1 5-pin, 180° DIN socket, right-angle PCB mount (CON4)
[eg Jaycar PS0350, Altronics P1188B)
1 4-pin header and jumper shunt (JP1)
1 6N138 optoisolator, DIP-8 (OPTO1)
1 1N4148 small signal diode (D1)
1 100nF 63V MKT capacitor
1 1kΩ 1% 1/4W metal film resistor
2 220Ω 1% 1/4W metal film resistors
Parts list – 8x8 Switch Matrix
1 double-sided PCB coded 23101212, 131 x 140mm
64 tactile pushbutton switches* (S1-S64) – see text
2 8-way pin headers (CON1,CON2)
16 female-female DuPont jumper leads (to connect CON1 & CON2 to the MIDI Shield)
M3 mounting screws and spacers to suit your application (optional)
* we used Diptronics DTS-21N-V (non-illuminated, from Mouser) and C&K ILSTA250
30 (illuminated, from Digi-Key). Jaycar SP0620/SP0622 and Altronics S1101/
S1103 are also suitable alternatives.
Extra parts for illuminated switches
2 8-way pin headers (CON3,CON4)
16 female-female DuPont jumper leads (for CON3 & CON4)
8 1/4W axial resistors to suit LEDs
That completes the construction of
the Shield.
Switch Matrix construction
We recommend fitting the resistors
first as they sit lower than the switches, although if you are not using illuminated switches, they are not required.
Follow by mounting the switches. If
they are illuminated types, choose the
94
Silicon Chip
orientation based on your LED wiring
needs, and ensure that all LEDs face
the same way.
If you don’t have illuminated switches, then their orientations won’t matter.
Push each switch in place and ensure
it is sitting flat before soldering. We’ve
slightly oversized the holes to allow for
some variation in parts, but the switches should still snap into place.
Australia’s electronics magazine
One good way of ensuring that they
are all aligned is to insert all the buttons, then rest a flat board on top, hold
onto this board and the PCB, then flip
the assembly over. The flat board will
align the tops of the switches.
Solder all the terminals to the PCB
and trim them if they are long.
Finally, fit headers CON1, CON2,
CON3 and CON4 as needed. We used
socket strips on our prototypes, as we
had a handful of pre-made eight-way
cables that we could run directly to the
headers on the MIDI PCB. We suggest
that you figure out how you will be
mounting the board (see below) before
soldering these, and test-fit the headers/
cables, as that might affect what connectors you need.
You could solder ribbon cable
straight to the pads on the Switch Matrix PCB and then to the MIDI PCB;
that is the cheapest way to connect the
two boards.
But headers make the wiring removable, which can be handy.
If soldering wires to the board, you
could run them to the underside of
the PCB if you will be mounting it on
spacers.
If you need those wires to be pluggable, you could mount right-angle pin
headers on the underside.
Note on our photos that the wire
from pin 1 of CON1 on the MIDI PCB
goes to pin 1 of CON2 on the Switch
Matrix PCB, and pin 1 of CON2 on the
MIDI PCB goes to pin 1 of CON1 on the
Switch Matrix PCB.
Mounting the Switch Matrix
Despite the small space available,
we’ve squeezed seven M3 mounting
holes into the design.
Some of these might not be usable
depending on the switches you have
chosen, although an M3 screw should
still fit in the central hole, even with
12mm switches fitted.
The PCB material is strong, but repeated flexing from enthusiastic keypresses could fatigue it, so we recommend mounting it to something solid,
like a piece of plywood.
Use some short spacers or a stack of
washers to provide clearance for the
component leads under the PCB.
Wiring up switches manually
If you really want to do it this way,
you can. Wire up the switches in rows
and columns like in our circuit (see
Fig.3).
siliconchip.com.au
Fig.4: overlay diagram for the
Switch Matrix PCB. This is
shown with 12mm large nonilluminated tactile switches
in place; they fit to the four
pads just outside the switch
footprint. The next set of four
pads are for smaller 6mm
switches, while the innermost
two pads are for the LEDs of
illuminated switches. Most
illuminated switches are
reversible, so that the LEDs
can be installed with either
polarity.
If you have built the MIDI Shield,
connect the rows and columns to CON1
and CON2 respectively, with the ends
going to the lowest-numbered switch
at pin 1 in each case.
If you’re using a bare Leonardo to
pass MIDI messages to a computer via
USB, you can instead use Fig.1 as a
guide for the wiring, as it shows how
rows and columns connect to the Arduino pins.
You can connect a piezo buzzer between pin D13 and the adjacent GND
for sound output, and raw MIDI data
is available at pin D1 (TX), referenced
to one of the GND pins.
Software
If you don’t already have the Arduino IDE (integrated development environment) installed on your computer,
download it from www.arduino.cc/
en/software (it’s free and available for
Windows, Mac and Linux).
If you already have the IDE, check
that you are using a recent version (at
siliconchip.com.au
least 1.8.x). We are using version 1.8.12.
Launch the IDE and open the Library
Manager (Sketch -> Include Libraries ->
Manage Libraries) and search to “TimerOne”. This library is used to provide
regular timer interrupts to produce the
audio waveform. Install it now, if you
don’t already have it on your system.
The second library we need is called
MIDIUSB and can be found by searching for “MIDIUSB” in the Library Manager.
The final library is simply called
“MIDI Library”. Several different libraries are found in a search for “midi”,
so you should see our screenshot
(Screen1) to verify that you have found
the correct library.
We have also included the zipped
versions of all three libraries in our
download package, which you can install via the Sketch -> Include Library
-> Add Zip Library menu option.
MIDI Library is set up to use the
hardware serial port on the Leonardo’s
pins D0 and D1, with the MIDI data
Australia’s electronics magazine
being produced at the TX pin, D1. We
tested this with an Arduino synthesiser sketch, and it worked as expected.
Once all the libraries are installed,
open the ‘MIDI_ENCODER’ sketch. Select the serial port of the Leonardo and
upload the sketch to the Leonardo.
In our sketch, the SAMPLE_RATE
define is set to an integer number of
microseconds between interrupts (to
minimise rounding errors).
This is followed by the sinewave
data, as 256 unsigned integer bytes
(0-255).
The matrix pin definitions follow
this. The rows each contribute a multiple of +8 to the key number, while the
columns contribute +1. The key count
thus spans zero to 63, and is offset by
the START_NOTE value, which we’ve
set to 28. That means that the MIDI
Encoder will produce notes from E1
(about 41Hz) up to G6 (1568Hz), centred near middle C (262Hz).
The range is limited to 64 notes by
the size of the matrix, but changing
April 2021 95
At left is the non-illuminated version of the Switch Matrix, at right the rear of the illuminated version; note the extra
leads, tapped standoffs and CON3 and CON4 fitted at the bottom. These are shown about 3/4 life size. The actual PCB size
is 131 x 140mm.
the start note changes where that range
spans.
The notes[] array sets the frequencies that are produced on pin D13.
You might want to tweak these if they
don’t sound right or you prefer a different scale.
Some parameters associated with the
library follow. These set the channel
and velocity that are used in the data
that is sent. The defaults should work
with most software, although some
programs might map channel 0 (in the
Arduino code) to channel 1.
Testing the Shield
At this stage, you should have built
the Shield, plugged it into the already
programmed Arduino Leonardo and attached the Switch Matrix (or whatever
switches you will be using).
Plug the Leonardo into a USB port,
launch the Arduino IDE (if it isn’t already running), make sure the correct
COM port is still selected, then open
the Arduino Serial Monitor.
Start pressing buttons in the matrix,
one at a time. You should see the Serial Monitor report that S1 causes UP/
DOWN actions on MIDI note 0. This
is because we’ve started the switch
numbering at 1, but the MIDI notes
begin at 0.
96
Silicon Chip
If you check that the four corner keys
are correct (the switch number is one
more than the note number), then the
remaining keys are probably correct. If
you find that you get incorrect notes,
try flipping the connections end for end
at CON1 or CON2 on the Switch Matrix PCB, or swapping CON1 for CON2.
Test all the keys; if you see any single
keypresses not being detected, check
the PCB for bad solder joins on the corresponding switch.
Usage
After the sketch is uploaded, the
Leonardo appears as a native USB-MIDI device to a computer.
As well as the audio and USB and
hardware MIDI outputs, the Leonardo also prints information to the serial monitor (accessible from the Leonardo’s native USB-serial port) about
which note is being played. This can
be handy for testing.
We used a program called MuseScore (https://musescore.org/en) to test
that the computer was correctly receiving MIDI data.
It automatically detected that a MIDI
device was present and played synthesised piano sounds, although it can
also transcribe to played notes among
other features.
Australia’s electronics magazine
Without a computer, you will have
to connect something to the audio output (on pin 13) or the hardware MIDI
data (on pin 1).
As we noted, we tested another Arduino sketch which worked as a synthesiser (this sketch expects MIDI data
on the Arduino’s serial port; typically
pin 0). This might be a better option
if you would like to get better sounds
without much expense.
Note that it is possible to trigger
sounds from the Shield using simple
jumper wires. Anything that connects
one of the row wires to one of the column wires will trigger a sound.
Using something like the cheap
membrane matrix keypads could be a
simple way of adding an input device,
especially if you want to create percussion sounds. Four of the 4x4 matrixes
could be connected to give the full complement of 64 inputs. Just make sure
to wire up each matrix to a different
combination of row and column wires.
Of course, since we’ve included the
Arduino source, code, you can use it as
a starting point for creating your own
MIDI-based project.
LED test sketch
We have created a test Arduino
sketch to light the LEDs if you have
siliconchip.com.au
fitted illuminated switches. We’re assuming that you’ve fitted them with
the anodes to the top, as we did on our
prototype.
We also assume that you’ve got the
Arduino IDE installed, including the
libraries for the MIDI Encoder. We
only need the TimerOne library for
this sketch.
Open the “MATRIX_LED_DRIVER”
sketch and upload it to the Leonardo.
Connect CON3 of the Switch Matrix
PCB to CON2 on the MIDI Encoder.
Then connect CON4 of the Switch Matrix PCB to CON1 on the MIDI Encoder.
You should see a diagonal row of
LEDs light up. You can change the
starting state with the LED[] array, and
manipulate this in the loop() function
to animate.
We’ve also created a self animating
version ‘MATRIX_LED_DRIVER_GOL’,
which implements a simple “Conway’s
Game of Life” simulation on the 8x8
matrix. The array is loaded with a pair
of ‘gliders’, which move as long the Matrix is powered.
You can find out more at https://
en.wikipedia.org/wiki/Conway%27s_
Game_of_Life
Using it
If you’ve built the amplifier section,
now would be a good time to wire up
a speaker. Generally, a short length of
twin-core cable is all you need to wire
it up, and most speakers have tabs that
suit soldering or quick-connect spade
The MIDI Encoder Shield
PCB simply slots onto the
Leonardo board using header
pins. Our build shows all
parts fitted except for the
headers CON1 and CON2.
This is because screw
terminals CON1A and
CON2A are fitted
instead. There’s
no point fitting
stackable
headers as
practically all
of the
Leonardo’s pins
are used up.
lugs. The other ends of the wires can
then be screwed into CON5. The polarity doesn’t matter much as the output
at CON5 is AC.
With the MIDI Encoder sketch uploaded to your Leonardo, you should
be able to get a tone from the speaker by
connecting any of CON1’s pads to any
of CON2’s pads (eg, by pressing a button on the attached key matrix). However, we found that our small speaker
was not able to render the lower notes
too loudly.
If the audio is distorted, reduce the
volume by turning VR1 anti-clockwise.
The mid-point should be audible for
practically all speakers, so if you can’t
hear anything, check your construction
before increasing the volume.
The MIDI output socket (CON3) can
be connected to the MIDI input port of
another device, such as an electronic
piano or DAW (digital audio workstation).
Similarly, the MIDI input connection
(CON4) can be driven from another device’s MIDI output port.
Note that CON4 does not do anything with our default software, as it is
not programmed to have any function.
Conclusion
While originally intended as a simple bit of hardware to make better use
of the MIDI Encoder software, we think
that this Shield will be handy for anyone who wants to dabble in custom
MIDI hardware.
Screen1: the Arduino Library Manager will give a lot of results for a ‘midi’ search, so use the one highlighted here or use
the zip version.
siliconchip.com.au
Australia’s electronics magazine
April 2021 97
This is the deluxe version of the matrix PCB, with illuminated switches, although you’ll have to provide your own keycaps
(this might be a good use for that 3D printer!). We’ve fitted it with standoffs to prevent the pointy leads from damaging the
surface it’s on or, conversely, shorting out on any conductive surface. Note the Shield PCB at left fitted with only headers
to allow it to be used as a USB MIDI device only.
It could, for example, be used as a
MIDI synthesiser by using the hardware MIDI input (CON4) or USB MIDI
input (in software) to receive MIDI
messages and turn them into sounds
from the speaker.
In a follow-up article, we will show
how to control illuminated pushbuttons from our MIDI sketch. This requires some extra hardware, as the
Leonardo doesn’t have enough pins
free to do this by itself.
We will also describe how to connect this device to a smartphone or
98
Silicon Chip
tablet running Android, and install a
MIDI synthesiser app which can then
be controlled using the Key Matrix.
We also intend for this article to
contain some more detailed information on the MIDI protocol, for those
who wish to expand upon our software, or are just interested to learn
how it works.
Note that the Switch Matrix presented here could be useful in many other contexts; it doesn’t have to be used
for MIDI.
It can serve as a general-purpose
Australia’s electronics magazine
switch array with up to eight rows and
eight columns; you don’t even need to
populate all the switches.
For example, you could wire up the
row and column headers to an Arduino Mega board (or similar) and use it
as a general keyboard, to type in letters and numbers etc (with suitably
labelled keycaps).
And as the Mega has many more pins
than the Leonardo, it could also easily
drive the LED matrix to light up keys
as they are typed, show which keys are
SC
valid inputs etc.
siliconchip.com.au
|