In one of my previous posts I gave an overview on how we can connect a microcontroller to a MIDI Device using a physical serial connection with the help of my Arduino MIDI Library. This solution is very similar, but replaces the physical connection with Serial Bluetooth

This solution will work with any ESP32 and it will actually also work with any microcontroller if you connect a HC-05 Bluetooth Module!

The PC Software

We need some software which is running on our desktop which forwards the MIDI messages to the serial port. This time we use a Raspberry PI with some Python to set up the Serial Bluetooth scenario.

We need to install all necessary libraries first with

pip install --upgrade pip mido python-rtmidi 

The following python program forwards all MIDI Commands

import mido
import serial

serial_port = '/dev/rfcomm0'
midi_name = 'Q61:Q61 MIDI 1 20:0'

ser = serial.Serial(serial_port)  # open serial port
input = mido.open_input(midi_name)
for msg in input:
    print(msg)
    if not ser.is_open:
         ser.open()
    ser.write(msg.bin())

You might need to adapt the serial port name and the midi name as described in the steps that follow.

The Arduino Sketch

For the example I am using my Arduino MIDI library.

#include "Midi.h"
#include "BluetoothSerial.h"

BluetoothSerial SerialBT;
MidiCallbackAction action;
MidiStreamIn in(SerialBT, action);

void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
  Serial.print("onNoteOn: ");
  Serial.println(note);
}

void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) {
  Serial.print("onNoteOff: ");
  Serial.println(note);
}

void setup() {
  Serial.begin(119200);
  SerialBT.begin("AudioKitBT");
  action.setCallbacks(onNoteOn, onNoteOff);
}

void loop() {
  in.loop();
}

This is pretty much the same solution like in the last post. We just replaced Serial2 with SerialBT!

Deploy the sketch.

Setup Bluetooth Connection

Now you can set up a Bluetooth connection on the Raspberry PI with Setup new Device. Follow thru all steps. At the end it will display the port information that you need to use to update the serial_port variable in the Python script above.

Determine the MIDI Name

You can determine the correct midi_name with the following program.

import mido
for name in mido.get_input_names():
   print(name)

Update the midi_name variable in the Python copy program.

Running the Scenario

When you start the Python program and press any key on the keyboard, you should see the effect both in the Python output and in the Arduino Serial Console

Categories: ArduinoMachine Sound

0 Comments

Leave a Reply

Avatar placeholder

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