Release 2016.11.26 / Update 2019.1.20

make MIDI device by Arduino

There was no environment that can be finished compose all in one computer When I started it. Everybody depended on MIDI which is very very useful. MIDI makes it accomplished complicated work by dividing heavy task into sequencer and sound module. And now almost every digital music instruments supports MIDI, even if it doesn’t have MIDI port.  This let it be easier to compose music. I couldn’t imagine the world would be like this.

So I will write how to send MIDI data by using Arduino.

I wrote a chattering-less switch library with “gauging” method.
(26.6.2017)

Watch this first.

Thanks to this article. This helped me to understand physical connection and protocol of MIDI.

Ready

Necessary parts

Additional elements

  • LED&Resistor
  • OLED display

about DIN connector

There are five pins on MIDI connector. But you don’t have to wire all pins. Just 3 pin.

midi_connector_illust

about MIDI interface・MIDI sound module

You need MIDI device to confirm if it were connected. You can simply connect to MIDI, if you have device like synthesizer, digital piano. In other way, there are some free music software on web.

Studio One Prime

This free version of DAW is available both Mac and Win. I used it on my Youtube demo. But in this way, you need MIDI interface.

Wiring

midi_5sw_breadboard

MIDI

Inside of MIDI data

The minimum necessary data to produce MIDI message is,

  • channel(1~16 CH)
  • note on, note off
  • musical scale(note number)
  • velocity

All you need is send those information as set.

MIDI message

MIDI data protocol is,

Asynchronous serial transfer of 31.25 Kbps (± 1%)

You can control MIDI device by just sending few byte. It’s same as Arduino Serial communication. So, I use ‘Software Serial’ to do it.

‘MIDI message’ is consisted by two type of data, ‘Status’ and ‘DATA’. At first is a ‘Status’ byte, this roles as header information. Then it follow to ‘DATA’ byte, containing values. This means you need 2 byte at least for each MIDI message.

Also highest bit of ‘Status’ byte must be HIGH. And its of ‘DATA’ byte is LOW.

So range of value are,

128 to 255 for value of ‘Status’ byte
0 to 127 for value of ‘DATA’ byte

midi_triger_status_and_data_en

In ‘Status’ byte, lower 4 bits indicates MIDI channel and remaining 3 bits indicates type of message.

‘Note number’ means musical scale. Check down below, if you want to know array.

midi_triger_keyboard

You have to send ‘note on’ and ‘note off’ message, if you want to play sound like do it on piano.

  first byte ‘Status’ ‘DATA’, following the bytes
data type higher 3bits
8 ~ 15
lower 4bits
0 ~ 15
second Byte
0 ~ 127
third Byte
0 ~ 127
Note on 0x90 MIDI Ch Note Velocity
Note off 0x80 MIDI Ch Note Velocity

These are part of MIDI message type. Check on web for further information.

midi_sw_5_t_sw

Arduino Sketch

Send a MIDI message

This sketch shows most simple way to send MIDI message. MIDI channel is 2, musical scale is C-3,

This simple sketch execute play ‘C’ on MIDI sound module for each 1 second. You’d better watch out that actual MIDI channel is different from hex value.

CH 2→0x91

Below sketch is a simplified sending MIDI signal as a function.

Send message by a switch

Next sample is controlling message by a switch. This function depends on “gauge switch” I developed. You don’t have to make chattering avoidance on the circuit with this sketch.

Does it works? You probably got precise and easy MIDI button.

This method is very effective. But, it only does works to one switch. The problem is caused by ‘while’ loop. So I have to remove the problem.

The way is “maintain the value”.

Basic thinking that several digital ‘HIGH’ determines ‘switch on’ is same.

Reading high from switch pin increases ‘gauge’ count argument. Otherwise, count is reset. Then Arduino tells ‘switch’ has been on, if count equals a certain value. Although the order on sketch has been changed, basic is same and simple.

Also I simplify it as function.

‘BUTTON’ is function with a byte returning.

‘0’ is returned if the swtich is ‘OFF’.
‘1’ is returned if the switch has been changed into ‘ON’.
‘2’ is returned if the switch keep ‘ON’.
‘255’ is returned if the swtich has been changed into ‘OFF’.

Send MIDI message by the multiplex switches.

Well, it’s not so difficult to multiplex switch function. Just use array.

‘PIN_NUM’ is total number of connected digital pins.
‘pins’ is value for each connected digital pins.
‘notes’ is MIDI note number for each switch.

You can add more switches by set the array arguments.

Incidentally, LED deponds to any switches has been ‘ON’.

External Display & turing

If you attach OLED display, you can see change. And to learn ‘gauge’ function, let’s tuning it.

This sketch uses ‘u8glib’ library. If you want to know how to use, check here. Even if you don’t have OLED display, you can check ‘gauge’ counter value from ordinary Serial Monitor.

By the way, switch doesn’t work anymore, you think? No. This is caused by being changed ‘PUSH_SHORT’ value. Try keep push switch. There is so long delay.

So change ‘PUSH_SHORT’ value. I think the value will match over 10.

You can change MIDI message from here.

‘MIDI_CH’ is channel number for sending message.
‘VELOCITY’ is velocity. Because of ‘digitalRead’, the velocity is fixed.
‘notes’ is key note scale value for each switch.

In MIDI, ch 10 is for drum. So you can play drum if you change values to below.

Review

The most orthodox way to use MIDI is get Arduino Shield and use ‘MIDI library’. But in this article, I could use MIDI by only a cable and ‘Software Serial’. By furthermore learn, I maybe make physical MIDI controller, maybe..

Sketch:MIDI trigger switches

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.