Using XBee to create a wireless bi-directional MIDI link

Introduction

If you have a modern musical instrument, there's a good chance it has a MIDI port. MIDI is an ancient serial protocol that runs at 31.25Kbs, often they come in pairs an Input and Output. Setting up MIDI gear usually requires lots of cabling, tying inputs to outputs across a studio or stage. In this XBee tutorial we'll show how to configure the XBee to talk at the MIDI baud rate, and then how to create a bi-directional wireless MIDI link.

Good for hooking up Processing/PureData/Max to a MIDI device.

For simplicity sake, I will assume you have a Windows machine. I assume that serial port baudrate aliasing can be done on Mac's and Linux but, well, I'll do it later!

You'll want to study the this great MIDI specification website with full MIDI protocol details so that when I say "Note on/off message" you know its a 3-byte packet of data and what each byte is made of!

In case you are wondering, the flight delay time is 6.0 milliseconds with default settings. By setting the packetization timeout to 1 instead of 3, the delay time goes down to 5.5ms

PLEASE NOTE! These instructions are for the Adafruit XBee adapter kit and the Series 1 XBees that are sold in the Adafruit Shop. They may not work for ANY OTHER KIND OF XBEE OR ADAPTER KIT!

Reinstalling FTDI drivers

This first part is pretty much a 'must' no matter what you're planning to do. We have to configure the XBee(s) to talk at 31,250 baud. That means we need to be able to talk to the XBee at that baud rate in order to configure it, say with our handy FTDI cable. Now unfortunately this isn't a default baud rate under Windows (a major oversight, it seems). Instead we have to do a little trick called baud rate aliasing where the driver is running at 31,250 but the computer thinks it's talking at 38,400 baud, a default baud rate. Note that this is very dumb but hey, that's Windows.

Unplug your FTDI cable and any FTDI devices like arduinos, USB-serial converters, GPS's, and the like.

Download FTClean, the driver-cleaner utility from FTDI. Uncompress and run FTClean.exe

Click the three windows to verify that you Really Want To Do This. The computer will hang for a few seconds while the drivers are uninstalled. It'll then look like this
Now you're ready to -reinstall- the FTDI driver that aliases 38,400 baud to 31,250 baud.

You can download the edited driver here note that it's called the MIDIfied driver. Uncompress it onto the Desktop.

Now plug in your FTDI cable again. The Windows driver detection window will pop up.
Select No, not this time and click Next
Select Install from a list or specific location (Advanced) and click Next
Select Search for best driver in these locations. Then uncheck Search removable media and check Include this location in the search: then click Browse and navigate to the uncompressed MIDIfied FTDI driver folder. Click Next
If it complains about Windows Logo testing, click Continue Anyways
OK! You may have to do this again, since there seems to be two parts of the driver. Just repeat, making sure to install the MIDIfied FTDI driver.

Update XBee firmware

If you haven't updated your XBee's firmware, you should do that now. Remember that you are still going to connect to the XBee at 9600 baud cause it's fresh-outta-the-box.

Changing XBee to 31250 baud

Now comes the magic. We're going to connect to the fresh XBee at 9600 through a terminal (I'm going to use the terminal built into XCTU but of course any terminal program is OK).

Then we're going to set the baud rate to a special value. By default the XBee can talk on the standard rates, like 2400, 9600, 57600, etc. However it is possible to make it talk a special rate by passing the hexidecimal value to ATBD. For example, if you want 31250 baud, convert that number to hex -> 7A12. Set the XBee by giving it the command ATBD7A12. Then check again to verify its 7A12! Finally, write it to flash with ATWR.

Note that once you write it to flash you can only talk to the XBee at that baud rate so you must be sure that you have the MIDIfied FTDI drivers installed because otherwise you won't be able to communicate with the XBee anymore!
OK now you have set the XBee to 31250 communication. You can hard reset the XBee by un/replugging the adapter. Test that you did things right by trying to connect to the XBee again, now at 31250. Remember that you have to select 38400 as the connection baud rate because that is aliased to 31250.

Build the MidiBee board

Label your MIDI XBee cause you'll need to keep track of it. Now it's time to build the hardware to connect DIN-5 MIDI jacks to the XBee.

components_done.jpg

Adafruit XBee Adapter

Adafruit

1

components_xbeemodule_MED.jpg

components_xbeemodule_MED.jpg

XBee module

Series 1 either 'standard' or 'pro

Adafruit

1

components_DINjack_t.jpg

MIDI connector

CUI SDS-50J
Digikey
Mouser
2

components_6n138.jpg

MIDI Optocoupler

6N138
Digikey
Mouser
1

components_OHM.gif

220 ohm resistor 5% 1/4W

Red - Red - Brown - Gold

Digikey
Mouser
4

components_74hc126_t.jpg

CMOS Hex inverter

74HC04N
Mouser
Digikey
1

You'll also need a source of ~5V. I suggest a 4AA battery holder. And something to build it on. I'm using a half-sized solderless breadboard.

We'll be building this, click for a larger version.

Note that a big chunk of it is on the XBee adapter. MIDI isn't that voltage sensitive (it uses current not voltage) so the 5V supply doesn't need to be precise. It can vary a bit which is why using 4 AA's (varies from 4.8V to 6V) and it's just fine.
We'll start by inserting the first jack into pins 30B 29C 28B 27C and 26B. It goes into the end of the breadboard. The little teeth in front should go into the blue - rail, you may have to twist it a little but it should snap in place.
The other jack goes into pins 30J 29I 28J 27I and 26J. The two front teeth go into the blue - rail as well.

Place the 6N138 optocoupler chip. This is what allows us to interface with the current-mode MIDI protocol. Pin 1 of the 6N138 goes into pin 19E

Then place the 74HC04 hex inverter. This chip takes the weak output of the XBee TX pin and buffers it so that it can drive the 5-10mA needed for the MIDI current loop. Pin 1 of the 74HC04 goes into pin 11E

Start laying down power lines. Connect the VCC pins of the 74HC04 and 6N138 using red wire to the red rail. Connect the ground pins using black wire to the blue rail. Connect the middle pin of the MIDI jack to ground as well.
Connect the MIDI in jack to the optocoupler: breadboard pin 25 connects to row 21. Breadboard row connects to a 220ohm resistor that then connects to row 20 as shown. This completes the current loop for MIDI data coming in.
Now we will do the MIDI out current loop. Row 25 connects to the red rail via a 220ohm resistor. Pin 6 (row 21) connects to the red rail as well, through another 220ohm resistor.
Now well add the buffer. Row 29 connects to pin 10 of the 74HC04 (row 15) through a 220ohm resistor. Make a wire jumper between row 13 and 14.

Finally we will get the wires ready for the XBee adapter. Connect Row 4 to the blue ground rail on both sides. Connect row 6 to the red power rail. Now we have the ground rails tied together and the XBee connected to power.

Next we will connect the MIDI in data. Connect a wire from pin 6 of the 6n138 (long yellow wire) to row 7 right next to the red power wire.

Connect another wire from 74HC04's pin 13 (row 12) using a green wire to row 8, right next to the yellow wire.

Get the 4xAA battery holder setup. Solder the red and black wires to a double-header. This will make it easy to plug into the breadboard. I used some heatshrink to keep the two from touching. It's easier if you place the header in the breadboard while soldering.
Your adapter should be all ready to go!

Connect the battery pack so that the red wire is attached to the red power rail and the black wire is attached to the blue ground rail. Marking the MIDI jacks with 'in' and 'out' stickers will help keep them apart.

To keep the board attached to the battery pack, use a rubberband or hotglue. You can also peel the backing off the breadboard and stick it right on.

Testing

We'll do a simple test to make sure that we have the XBee wired up correctly. Get your FTDI cable, and connect it to your other XBee and adapter.

This XBee should also be firmware updated but it doesn't have to be set to 31250 baud! That's because the XBee transmits wireless data in its own ZigBee protocol, not plain serial. You can have one XBee communicating at 31,250 baud and another at 9600 baud and they'll pass data back and forth just fine. Anyways. Connect up to XBee using X-CTU or use another terminal. I suggest a terminal program that lets you see hex values for transmitted data which is why X-CTU is pretty rad.

Make sure your MidiBee bread board is powered, the green LED on the xbee adapter should be blinking. Connect a MIDI out from a musical instrument, MIDI adapter, etc to the MIDI in on the breadboard. Send a MIDI message, say by pressing a key on the MIDI keyboard. You should see the RSSI (red) LED on the XBee connected to your computer light up as it is receiving a message!

If you look in your Terminal, you'll see the MIDI messages show up. These are NOTE ON and NOTE OFF messages.

Don't forget, you should connect to the computer-XBee at whatever baud rate that XBee is set to. If it's still at factory-default 9600, connect at 9600. If you also 'MIDIfied' it, connect at 38400 (aliased to 31250).

Now it's time to test sending MIDI data out via the XBee. You can use X-CTU terminal to connect to the XBee and click on Assemble Packet. Then select HEX display/entry and type in 90 32 64 this is a note on message. Click Send Data. You should see the RSSI (red) LED on the MidiBee breadboard light up indicating it received the packet. If you connect a synth to the MIDI out it will play a note (well, assuming it's set to MIDI address 0, you should craft the Note On message as necessary to set the address).

If you use software such as Max or PD or Processing (or even python, etc), you can also now send MIDI messages by opening the serial port at whatever baud rate you've set up the XBee. For example, here is a quick sketch. You may need to change the name of the Serial port from "COM3" to whatever.
import processing.serial.*;

Serial myPort;

// A little helper
void sendMIDI(int command, int data1, int data2) 
{
 myPort.write(command);
 myPort.write(data1);
 myPort.write(data2);
}

void setup() {
   // List all the available serial ports:  
  println(Serial.list());

  myPort = new Serial(this, "COM3", 9600); 

  // Send a Note On message, then a Note Off message
  sendMIDI(0x90, 0x32, 0x64);      // D note on, 64 velocity 
  delay(1000); // one second
  sendMIDI(0x80, 0x32, 0x64);      // D note off, 64 velocity 
}

Check that your MIDI synth is getting the messages.

Sweet! You now have a wireless MIDI setup for sending and receiving data from any computer!

Bonus Trick: Turning the FTDI cable into a MIDI device

OK sure you can send messages to your MIDI device via the serial port. But there's an awful lot of programs that are going to require that you use a real MIDI device. What now? Well luckily, the nice fellows at KORG have designed a Windows driver that turns a COM port into a proper MIDI device!
You can download it here, it's called Midi Driver PCIF-NT for Windows NT/2000/XP it seems to not work with Vista, sorry.

Follow the instructions in the MANUAL.HTM file included in the zip file.
Make sure the FTDI cable is set to COM2, 3 or 4 because the driver can't cope with higher numbers. You can set the FTDI cable's COM port in the Device Manager. Set the KORG driver to the correct COM port.
Now you should have the KORG MIDI driver device in the Device Manager.
Now you can use somthing like MIDIOX (or any other program that can talk to MIDI ports) to communicate with the XBee/MIDI link.

This guide was first published on Feb 16, 2015. It was last updated on Feb 16, 2015.

This page (MIDI link) was last updated on Mar 26, 2013.

Text editor powered by tinymce.