Step 1: All USB devices are not the same
Many people tend to think you can plug any USB device into any other, but this is not strictly true. The sort of USB connector on a Raspberry Pi computer is USB-A. This connector is always used by a USB host device, like a computer. A host device supplies power and is in control of the process of communicating with the other sort of device, a USB client device. If a client wants to pass information to a host, it has to wait until it is asked by the host if it has anything to say.
Step 2 : Interconnectivity
You can't plug a host into a host, nor can you plug a client into a client. Quite simply, the connectors on the USB cable will stop you doing this – even if you make up a special cable. it simply would not work. So while it is fine for a Raspberry Pi computer to connect with a MIDI sound module, you can’t connect it to a DAW (digital audio workstation) like Reaper or Ableton running on an other Raspberry Pi computer or other laptop. This limits the role your Raspberry Pi computer can play in a MIDI system.
Step 3 : Solving the problem
You could use a MIDI interface cable on both computers and connect the MIDI sockets up with a couple of MIDI leads, but we have found this seldom works because of the accuracy of the baud rate produced by these interface leads. So, we have come up with a solution to get round this by enlisting the help of an Arduino, like we have done in the past. But this time we will be using a different sort of Arduino, the Pro Micro, based on the ATmega32U4 chip. We get the bonus of using the Arduino’s peripherals as well.
Step 4 : How does this work?
The idea is that the Arduino presents a USB MIDI client interface to a host and the we can communicate with it from the Raspberry Pi computer using its serial port. This can be at a fast rate, and we are not faced with the problem of trying to get the serial port to use MIDI speeds, which are not directly supported. The block diagram of this project is shown in Figure 1. While we could wire the Arduino to the same cable as our Tap-An-LED box, we did not want two boxes permanently joined together with a common flying lead.
Step 5 : Making the GPIO connector extension board
To get round this, we made a little 12-pin GPIO extender board so that we could use the two boxes together. This is simply a piece of stripboard 16 hole by 6 strips – Figure 2 shows the track cuts on the underside. To the copper side, solder a 12-pin 2-row surface-mount socket; to the front, solder two 12-pin 2-row header pins. Figure 3 shows this, along with the way in which it is wired up. To save it looking too messy, the two stages of the wiring are shown separately, and a photograph of the finished item is shown in Figure 4.
Step 6 : The Arduino board
There are two types of Pro Micro Arduino: one with a mini USB connector, and one with a micro USB connector. We prefer the mini connector, as it tends to be more solidly attached to the board. It is slightly wider than the one with the micro connector, but either would fit on the stripboard shown in Figure 5. On the reverse, each strip row is broken in the middle to prevent the board from shorting out. The board has straight header pins soldered to it and there is a matching socket soldered to the stripboard.
Step 7 : Powering the Arduino
The link on the top corner of the Pro Micro is left open. This means the board runs at 3V3 (3.3V) and is only powered when it is plugged into a USB host. To stop the Raspberry Pi damaging the board with a serial signal when the Arduino is unpowered, a 3K3 (3.3kΩ) series resistor is inserted in Raspberry Pi’s TX signal line. On the front panel is a 20-pin 2-row socket to access the peripherals of the Arduino. For clarity, the diagram shows this connector as being split into top and bottom halves, but it is just one socket.
Step 8 : The box
We made the box using our normal method of 30mm pillars holding the top and bottom apart, with the sides glued together as a ring. The TX and RX LEDs on the Arduino are useful in seeing if MIDI messages being sent, and received. So that we could see the LEDs blink, we used two 13mm long, 6mm diameter, clear acrylic rods glued in the top to act as light pipes,. A message is being passed when the LEDs are off. Figure 6 shows the assembled box. The layout drawing for this box, and more construction photographs, can be found on our GitHub site (magpi.cc/pibakery).
Step 9 : Bonus I/O
As we had an Arduino, with all its peripheral goodies doing nothing, we brought them out to a 20-way socket on the box lid. The pinout of this is shown in Figure 7. We have access to 16 different pins and over half of them are dual function. For example, we can use pin 13 of this connector as a PWM-capable digital pin 6, or as analogue input A7. You can only choose one of these two functions to use at any one time. The API for communicating with these peripherals is shown in Figure 8.
Step 10 : The Arduino software
The Arduino software uses the MIDIUSB library, to present itself as a MIDI HID (human interface device) client device when plugged into a host. This communicates with the host by using four numbers to define a MIDI message; we call this a ‘frame’. These frames can be transferred to and from the host by the Arduino and also passed to and from Raspberry Pi. The Python software then uses these frames to deal with messages sent to it, or generate messages to send to the host or communicate with the Arduino peripherals.
Step 11: Applications
We have written several applications to demonstrate the capability, and workings, of our client interface, both with and without using the Tap-An-LED interface. Many of these look a lot better as a movie, rather than a still. For example, we have four midi_note_display programs. These take a stream of MIDI note messages, sent by the host, while playing a MIDI file – and LEDs or Raspberry Pi’s screen change accordingly. The colours used are changed according to the velocity of the note. LEDs and the screen can display keyboards, or a unique LED for each note – see Figure 9 for one example.
Step 12 : More applications
There is xy_pad.py, an example of using the raw data from the touch sensor screen to control two parameters as an X-Y pad in Ableton. In addition, midi_duet.py fires random notes, in the key of C, into Ableton for you to play along and improvise against. Then there is peripheral_request.py for getting and setting data from the Arduino’s peripheral pins. To go along with all this, we have all the Ableton Live sets you need to work along with these examples. Many musical interfaces you can buy come with a licence for a cut-down version of Ableton Live.
Step 13 : Other DAWs
There are many more DAWs (digital audio workstations) other than Ableton. There are top-end applications like Apple’s Logic Pro X or FL studio, as well as free ones like the Windows-only Cakewalk, or the Apple-only Garage Band. Also, there are budget DAWs; perhaps one of the best of these is Reaper. Garage Band has very good-quality sounds, but the big problem we see with it is that the MIDI works for only one instrument and the implementation of MIDI controls is limited. There are many more DAWs if you search for ‘Free DAW’.
Make sure you catch up with Part 1 of this Tap-an-LED series in issue #97 of The MagPi Magazine, and Part 2 in issue #98.