Silicon Chip64-KEY MIDI MATRIX - April 2022 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Subscriptions: PICOLOG
  5. Publisher's Letter
  6. Feature: Go eco, get ethical! by Mark Nelson
  7. Feature: The Fox Report by Barry Fox
  8. Feature: Net Work by Alan Winstanley
  9. Back Issues: Digital FX Unit by John Clarke
  10. Project: 64-KEY MIDI MATRIX by Tim Blythman
  11. Project: Digital FX Unit by John Clarke
  12. Feature: P955H PIC Training Circuit by Peter Brunning
  13. Project: High-current Battery Balancer by Duraid Madina
  14. Feature: Circuit Surgery by Ian Bell
  15. Feature: Flowcode Graphical Programming by Martin Whitlock
  16. Feature: AUDIO OUT by Jake Rothman
  17. Feature: Max’s Cool Beans by Max the Magnifi cent
  18. Feature: Make it with Micromite by Phil Boyce
  19. PCB Order Form
  20. Advertising Index

This is only a preview of the April 2022 issue of Practical Electronics.

You can view 0 of the 72 pages in the full issue.

Articles in this series:
  • (November 2020)
  • Techno Talk (December 2020)
  • Techno Talk (January 2021)
  • Techno Talk (February 2021)
  • Techno Talk (March 2021)
  • Techno Talk (April 2021)
  • Techno Talk (May 2021)
  • Techno Talk (June 2021)
  • Techno Talk (July 2021)
  • Techno Talk (August 2021)
  • Techno Talk (September 2021)
  • Techno Talk (October 2021)
  • Techno Talk (November 2021)
  • Techno Talk (December 2021)
  • Communing with nature (January 2022)
  • Should we be worried? (February 2022)
  • How resilient is your lifeline? (March 2022)
  • Go eco, get ethical! (April 2022)
  • From nano to bio (May 2022)
  • Positivity follows the gloom (June 2022)
  • Mixed menu (July 2022)
  • Time for a total rethink? (August 2022)
  • What’s in a name? (September 2022)
  • Forget leaves on the line! (October 2022)
  • Giant Boost for Batteries (December 2022)
  • Raudive Voices Revisited (January 2023)
  • A thousand words (February 2023)
  • It’s handover time (March 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • Prophecy can be perplexing (May 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • How long until we’re all out of work? (August 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • Holy Spheres, Batman! (October 2023)
  • Where’s my pneumatic car? (November 2023)
  • Good grief! (December 2023)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (February 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk (July 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk (November 2024)
  • Techno Talk (December 2024)
  • Techno Talk (January 2025)
  • Techno Talk (February 2025)
  • Techno Talk (March 2025)
  • Techno Talk (April 2025)
  • Techno Talk (May 2025)
  • Techno Talk (June 2025)
64-KEY MATRIX Part 1 by T im Blythman 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 pounds. 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 2021, 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 (eg, a keyboard) and converts them into MIDI format. Such a device could be connected to a computer to record playing, or 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. Working with Arduino 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 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 16 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 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 Encoder Shield, described below, to convert it to the correct electrical format to go to a synthesiser. 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, since 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 response to lower frequencies. Hence, our MIDI Encoder 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 MIDI Encoder 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. Practical Electronics | April | 2022 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. So, 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 receiver. 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.1-inch) 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 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 December 2021. It provides a push-pull output at up to 1.5W into 4W, 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 1kW 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. 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 1MW resistor, while the filtered audio comes into the inverting input via a 10kW 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 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 0W) to fullrail (at 10kW). 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 terminal CON5, for wiring up a 4W or 8W speaker. The SSM2211 can deliver up to 350mA, giving up to 1.5W into a 4W load or about 1W into an 8W 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) Use this photo to guide your wiring between the MIDI Encoder 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. Practical Electronics | April | 2022 17 Reproduced by arrangement with SILICON CHIP magazine 2022. www.siliconchip.com.au SC MIDI Shield S for Arduino Ó 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 220W resistors to connect the 5V supply to pin 4 of 5-pin DIN socket CON3, and the MIDI signal to pin 5. At the receiving end, we expect another 220W 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 and 4. Note the use of the 5V rail for the return signal. Since the serial idle state 18 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). 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 220W 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, 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 1kW resistor and is pulled to GND whenever the opto’s LED is forward-biased. This output signal is fed via pins 1 and 2 of JP1 to the Leonardo’s UART RX pin, D1. This can also be patched into another pin if necessary. JP1 also offers the possibility of using the MIDI Encoder Shield as a MIDI bridge, by placing the jumper across the middle two pins (pin 2 and pin 3). This will connect the output of OPTO1 to the transmitter at CON3, thereby passing any signal straight through. Practical Electronics | April | 2022 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 and LED Matrix We imagine that most people using our MIDI Encoder Shield 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 Encoder 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 (for example, 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 eightway 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 Shield 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 Encoder Shield’s CON2 and CON1 respectively (all pin 1 to pin 1), pressing S1 will 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 Encoder 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. Practical Electronics | April | 2022 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 16 x 16mm. 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 (S1-S8) 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 Shield 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 parts later, especially after the headers have been 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 SMD (surface-mounted device). We chose the SOIC (small outline IC) version as the alternative is a DFN (dual flat no-lead) package, which is considerably harder to solder. We recommend that you have some solder flux paste, tweezers, a magnifier and solder wicking braid on hand when soldering 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, and match them to the values printed on the silkscreen. Next, fit the capacitors, as shown in Fig.2. There is only one diode to mount, 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 oriented 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 19 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 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 oriented 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 and 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. 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 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. 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 20 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 throughhole 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. 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 Encoder Shield 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 Shield PCB; that is the cheapest way to connect the two boards. But headers make the wiring removable, which can be handy. If you are soldering wires to the board, then 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 Encoder Shield PCB goes to pin 1 of CON2 on the Switch Matrix PCB, and pin 1 of CON2 on the MIDI Encoder Shield 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). If you have built the MIDI Encoder 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). Practical Electronics | April | 2022 SC MIDI Shield S Switch S Matrix C Ó If you already have the IDE, check that you are using a recent version (at least 1.8.x). While developing this project we were using version 1.8.12. Practical Electronics | April | 2022 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. 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. 21 Parts list – MIDI Shield 1 double-sided PCB coded 23101211, 69 x 54mm, available from the PE PCB Service 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, available from the PE PCB Service 64 tactile pushbutton switches* (S1-S64) – see text 2 8-way pin headers (CON1,CON2) 16 female-female DuPont jumper leads (to connect CON1 and 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 and CON4) 8 1/4W axial resistors to suit LEDs The second library we need is called MIDIUSB and can be found by searching for ‘MIDIUSB’ in the Library Manager. The final library is 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’ve included the zipped versions of all three libraries in our download package; installed via the Sketch -> Include Library -> Add Zip Library menu option. (Download from the April 2022 page of the PE website.) MIDI Library is set up to use the hardware serial port on the Leonardo’s pins 22 D0 and D1, with the MIDI data 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 (this is done to minimise any 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 Shield 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 matrix size, but changing 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 Switch 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. 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 Muse-Score (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 Practical Electronics | April | 2022 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. synthesised piano sounds, although it can also transcribe to played notes among other features. 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 a cheap membrane matrix keypad 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 (Left) The non-illuminated version of the Switch Matrix. (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 70% life size. The actual PCB size is 131 x 140mm. Practical Electronics | April | 2022 23 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 nearly all of the Leonardo’s pins are used up. 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 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 Shield. 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 Shield. Then connect CON4 of the Switch Matrix PCB to CON1 on the MIDI Encoder Shield. 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 Switch Matrix is powered. You can find out more at: https://bit.ly/pe-ape22-cgol 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 connect it up, and most speakers have tabs that suit soldering or quick-connect spade 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 Encoder Shield 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 Encoder Shield 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 Shield software, we think that this Shield will be handy for anyone who wants to dabble in custom MIDI hardware. It could, for example, be used as a MIDI synthesiser by using the hardware MIDI Encoder Shield 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 tablet running Android, and install a MIDI synthesiser app which can then be controlled using the Key Matrix. 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 which can be downloaded from the April 2022 page of the PE website. 24 Practical Electronics | April | 2022 This is the deluxe version of the Switch Matrix PCB, with illuminated switches, although you’ll have to provide your own keycaps – a good use for your 3D printer! (3D Keycap files can be downloaded from the April 2022 page of the PE website.). 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. 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 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, or show which keys are valid inputs, for example. Matrix Job A4 Advert B 02_2022.pdf 1 09/02/2022 14:38:20 01422 252 380 www.matrixtsl.com We are looking for an experienced electrical design engineer in West Yorkshire. Who are we? C M Y CM MY At Matrix, we develop industrial-standard training equipment for education providers around the world. Our solutions cover Science, Engineering and Technology disciplines from Robotics, Electrical engineering and Automotive learning to Manufacturing and Mechanical Engineering. The role: CY CMY K If you think this could be you, please email a covering letter and CV to recruitment<at>matrixtsl.com Practical Electronics | April | 2022 25