Arduino, C#, Hardware, Root, Windows 8, WinRT

Arduino as a MIDI/Bluetooth Relay for Windows 8.1 Apps

In my last post I described how a Bluetooth connection between Arduino and a Windows 8.1 device can be established. The next step for me was to connect the Arduino to my electronic drum kit which has both, a MIDI-IN and a MIDI-OUT jack, but any other electronical instrument will do as well. The wiring diagram for an Arduino Uno R3 with MIDI-IN/OUT and the JY-MCU Bluetooth module is shown in Fig.1. NOTE: Occasionally there are MIDI shields available for Arduino, so you might not have to build it on your own.

Fig.1: You'll need a few resistors, a diod, an optocoupler and preferably one or two DIN-jacks
Fig.1: You’ll need a few resistors, a diod, an optocoupler and preferably one or two DIN-jacks

The Arduino code to relay MIDI>Bluetooth and Bluetooth>MIDI is actually quite simple.

//======================================================authorship
//by Michael Osthege (2013)
//======================================================includes
#include "SoftwareSerial.h"
//======================================================constants
const int TX_BT = 10;
const int RX_BT = 11;
const int MIDI_TX = 1;
const int MIDI_RX = 0;
//======================================================bluetooth setup
SoftwareSerial btSerial(TX_BT, RX_BT);
//======================================================initialization
void setup()
{
    Serial.begin(31250);
    btSerial.begin(9600);
    Serial.println("Bluetooth initialized");
}
//======================================================do work
void loop()
{
    ReadMIDI();//listen on the MIDI-IN
    ReadBluetooth();//listen for Bluetooth
}
void ReadMIDI()
{
    if (Serial.available() > 0)//there's something in the buffer
    {
        char buffer[3];
        Serial.readBytes(buffer, 3);//receive it
        btSerial.write(buffer[0]);//relay it
        btSerial.write(buffer[1]);
        btSerial.write(buffer[2]);
    }
}
void ReadBluetooth()
{
    if (btSerial.available() > 0)//there's something in the buffer
    {
        char buffer[3];
        btSerial.readBytes(buffer, 3);//receive it
        Serial.write(buffer[0]);//relay it
        Serial.write(buffer[1]);
        Serial.write(buffer[2]);
    }
}

As I understand it, the MIDI protocol communicates with commands of three bytes. Therefore I decided to relay all incoming serial messages in chunks of three bytes. To test the relay, I modified the BluetoothConnectionManager of my previous example to send/receive chunks of 3 bytes as MIDI commands too. Incoming commands are printed into lines of text and simple sounds can be sent as well. You can try it out yourself in this sample application: http://code.msdn.microsoft.com/Arduino-as-a-MIDIBluetooth-1c38384a Have a nice weekend =)

5 comments

  1. YuanHou Ho

    Hi,
    This project is cool, I have Arduino
    and the bluetooth module.
    However,I don’t have a Win 8 Tablet,
    Can I run this sample code on Win7 laptop?

    Thanks

    1. thecake

      No, this particular code won’t run on Windows 7, however you should be able to find examples how to use Bluetooth from a WPF application. You can then adapt the communication principle to work with your WPF app, probably even without altering the Arduino code.

  2. YuanHou Ho

    Hi,

    Thank you for the reply, Is there a sample showing people
    how to direct the midi message to Bluetooth interface on WIN8 tablet?
    (Bluetooth midi out for WIn 8)

    Yuanhou

    1. kuchenzeit Post author

      The code sample at MSDN shows the communication in both directions: http://code.msdn.microsoft.com/Arduino-as-a-MIDIBluetooth-1c38384a

  3. YuanHou Ho

    Thanks, I’ll get a Win 8 tablet and try

Leave a Reply

Your email address will not be published. Required fields are marked *