Introduction: Arduino Midi Class - Hardware, Codes and Shortcuts Explored

Over the internet there are a great range of makers who create awesome musical instruments using the MIDI interface.

You might be be thinking "I wish I had an electronic drum kit." Simple bro, build it yourself.

You might be thinking "I want to have a mixing desk to control garageband, but they are way too expensive."
Too easy! Such a simple project. Once you know how, you could do it in a weekend.

You might be thinking "If only I could use my tap dancing shoes to play other sounds!"
Just put piezo sensors on the bottom of your tap dancing shoes and send the rhythm in midi.

You might be thinking "I wish I could play rock band with a real guitar!"
Put capacitive touch sensing on your guitar strings and send midi to your rock band controller.

You might now be thinking, "Could I have my cat play drums by licking milk out of a capacitive touch sensing metal bowl?" Yes, you can!

You might be thinking, "If only I could play music using the forces of nature!!"
The answer to this is yes, you can! Put a moisture sensor in the ground, a rain sensor, a wind sensor, a light sensor, a temperature sensor, some bend sensors on thin tree branches, some vibration or knock sensors on branches that hit each other. Get the Arduino to turn it all into midi signals…Arduino's are great at that kind of thing.

If you type midi into the instructables search bar, you will find hundreds upon hundreds of different projects. Midi opens music up to a whole new range of options. And so your addiction to making weird and wonderful musical instruments begins. (Apologies to your partner, kids or flatmates for the space used storing all these new contraptions, but they don't have to be big.)


This instructable is designed to help you decide where to look and what to consider when starting a midi project.

USB plug and play devices are great for controlling all kinds of musical programs on computers enabling you to record a wide range of sounds from a single keyboard or midi device. Midi devices range from mixing desks to lighting rigs, to DJ scratch and button pads through to Lazer harps, iPads and the standard keyboard. Midi is the way that each of these controllers can talk to computers or to each other and understand the musical messages that are being sent.
Remember that Midi is not a sound, it is the instructions for the sound. Making a midi controller is a way to create music via a MIDI interface such as a keyboard, an electronic drumkit, or a computer with midi software (GarageBand, mainstage, logic, ableton live, fruityloops, etc).

For more information on MIDI I highly recommend this instructable
I also highly recommend visiting Amandaghassaei's page and browsing through her very comprehensive instructables. I also highly recommend sparkfun's midi tutorial.

Arduino midi is a DIY movement of digital instruments. It starts with basic keyboards literally made from bananas right through to complex algorithms tracking dancers in 3D space. Arduino adds the capability for a wide range of sensors to suddenly become musical. Bending, turning, distance, light, temperature, pressure... arduino has a huge range of sensors that it is compatible with and adding MIDI to the arduino creates endless musical possibilities. From using light sensors, temperature sensors rain and soil moisture sensors to map the weather into a song, right through to having a drum kit read your midi notes and play itself using motors. This instructable is here to give you tips and get your imagination ticking.

Tech specs on Midi

Also there is a lot of helpful information in this instructable which should not be overlooked.

In this instructable, each step is independent from the rest. You can mix and match steps for your preference and project. None of the steps are compulsory, there are many different ways to do this.

Also, I'm super open to collaboration on this instructable, so if you want to edit, add or correct anything in this instructable, please message me and I'll add you as a collaborator. The goal is to create a highly comprehensive view of arduino midi.

Step 1: Arduino Boards

Depending on what you want to achieve, there are a number of different arduino boards to consider. Digital pins read on or off signals and can also send on off messages for turning motors on, turning on lights, sending information to screens, etc.
Analog reads a voltage and this is helpful for things like touch sensitivity, piezo transducers for drum pads, pressure, light or flex sensors (among many other sensors). For a push button controller, these aren't an issue, but if you want to have lots of knobs and sliders, you will need a board with enough analog pins.

Voltage is handy to think about. Most guitar pedals run on a 9v power supply. If you want your midi pedal to fit nicely among other guitar pedals, something that would run at 9v might be handy. Also 5v is the voltage of the USB power connection, so when using smaller boards and fitting a USB connection, it's handy to have one that is 5v rather than 3.3v.

There is a complete list of Arduino boards here, but I've listed some of the most popular below.

UNO - Stock standard, fairly cheap to get, can be dual boot with dual Moco to create a fairly easy usb midi device. 6 analog pins. If you aren't a fan of soldiering and you want to be able to have standard MIDI jacks rather than MIDI via USB, there are heaps of shields that clip on.
https://www.sparkfun.com/products/12898 Midi shield.

101 - An uno with a built in Gyro and Bluetooth... midi via Bluetooth is possible on iPads and Macs. The code would be more difficult, but if you are up to the challenge, there are ways and means.

Leonardo - Compatible with arcore midi library for easy programming. 12 Analog inputs

Pro Micro - Cheap, small and versatile. Compatible with Arcore Midi library for easy programming. 6 Analog inputs

Lilypad - Want to create a wearable instrument like drumpad jeans or something? Lilypad is the wearable arduino.

Yun or MKR1000 - Want your midi device to connect to your ipad and computer via wifi? Maybe go for one of these.

Mega - stacks of inputs and outputs. 16 Analog inputs. Heaps of room to add heaps of code. Cool for things like... having an electronic drumkit that lights up in a whole heap of colours, doing a touch sensitive keyboard with 48 keys. Bigger projects like that.

Step 2: Custom MIDI Boards

Before you go arduino, there are a number of other boards that you might want to consider, this depends on the size of your project, what you want to do and the budget you have to do it in.

Makey Makey - super simple easy to muck around with. Very little learning curve. Not midi, but instead inputs keyboard presses which there are many online software converters for. Garageband and logic have a keyboard that makey makey can play. Just push caps lock and play away. Easy to play about with, but limited and quite expensive. Technically, Makey Makey is an Arduino Leonardo with huge pull down resistors, so it's also completely reprogrammable and could be simply made into a midi controller using the arcore library.

Teensy - Teensy is by far the easiest arduino compatible board to use for MIDI. Once set up, midi codes are fairly simple and tutorials on their website give a lot of information. Because Teensy is USB Midi compatible, it is easy to code then try out. There are ways to make a normal arduino do the same for a lot less money, but Teensy is ahead of the game for easy compatibility and ease of use. The Usb.midi library is very easy to use. Teensy comes with a stack of analog inputs which are very handy for building drum kits or mixing desks. Because it is arduino compatible, anything an arduino can do, a teensy can also do. The teensy 3.6 has 24 analog inputs which is huge. It is also faster than all of the arduino boards. For this reason, I've given Teensy it's own step.
Sparkfun do a great product review on Teensy boards

Doepfer have a range of different midi instruments for digitizing synthesisers. Their USB64 has a huge 64 analog inputs for creating touch sensitive keyboards or 64 piece electronic drumkits. It's acres above the capabilities of the arduino. Doepfer specialise in converting retro organs to midi so if that's what you are considering, they are seriously worth checking out. Any large scale velocity sensitive keyboard project should definitely include a glance at their products.

Livid Instruments have a few easy to use DIY Midi controller boards. They seem simple enough to use and although a little more expensive than arduino, would save a lot of time learning code if you had a fairly normal musical instrument, drumkit, keyboard, or mixing desk in mind.

Midibox design boards for midi projects that are capable of a wide range of tasks. Their boards are capable of recording and playing back midi for live midi looping and other ideas.

Midifighter have high quality kits (pretty expensive, but are pretty legit in functionality). These are specifically

Bhoreal slim is an open source midi controller based on buttons which has 16 - 64 pads of craziness.

Arduino on other chips

Arduino can be loaded onto other chips as standalone chips. This can be handy to reduce size, cost or increase functions of your board.

The Arduino Playground has information on what chips are compatible and how to do this. El Pajaro Manitas has written an instructable on how to shortcut the huge amounts of hardware by using just the chip from an arduino, or even experimenting with other smaller chips like the Attiny85.
Here is an example of a midi plug synth using attiny 2313 chip.

Other development boards

There are always other ways of doing things, have a look about because it can save money, time or create different quality or functionality.

http://djtechtools.com/2011/06/21/diy-midi-still-m…

https://www.instructables.com/id/DIY-USB-DJ-Contro…


Intel Edison - basically a small computer... could have all it's own sounds and programs preloaded for use as a standalone instrument. Here is an example of a midi project using Intel Edison.


Raspberry Pi - basically a small computer... could have all it's own sounds and programs preloaded for use as a standalone instrument

Step 3: Using a Teensy

Technically a teensy is arduino compatible, but has a few midi advantages over using an arduino.

1. It already knows how to talk MIDI. This is a big bonus. You don't have to have any clever work arounds or coding to get it to speak MIDI through your USB port. You don't have to unplug it to try your code, you don't have to use any external programmers, you don't have to dual boot it or do any other fancy ideas, it just speaks midi. You type your code, and try it out, edit the code, upload and go again until it is right. Once set up, you can just plug it into any computer and it will recognise it as a midi device.
https://www.pjrc.com/teensy/td_midi.html

2. It has an easy MIDI library. You don't have to learn how to write hexadecimal numbers in order to get it to the note you want, you can write in decimal format with normal numbers and it's easy to wrap your head around the concept.
Essentially it's codes are usbMIDI.sendNoteOn(note, velocity, channel);
This means that it's easy to tell what you are doing in the code, and you don't get very lost.

3. Because it is easy to use for Midi, heaps of people use it for midi and there are stacks of tutorials on how to use it.

Here is an example midi controller using a teensy.

Here is a more complex, but very helpful and comprehensive instructable on teensy midi. It's worth checking just for ideas and tips in general.

Here is a midi foot pedal that I made using a teensy. It was easy to learn and build with.

Step 4: Serial to Midi Converters

So, you've decided that you want to go with Arduino over the other ideas, but you want to do midi via USB. There are a wide range of ways to do this, which method you use will depend on how you want it to function.

When beginning out, a simple and easy way to play with midi is through serial to midi converters. It doesn't require extra tampering with your chip, just a simple download and away you go. I've tried a few of these ideas and they can be helpful ways to start out, but they do limit your project to use with computers that have the program installed.

This can be in the form of a midi usb cable, or a software converter like max, virtual piano, ardrumio or hairless midi.
Here is an example using virtual piano

Here is an example using Hairless Midi

Step 5: Reprogramming Your Chip

The arduino is a very versatile chip because it is open source and hackers play with it a lot. So an arduino doesn't always have to be an arduino. It can be reprogrammed as a midi controller. There are a few different ways to do this. I have listed them in order of easiness.

Arcore
This is a Midi library that is compatible with the Leonardo and Pro Micro boards for USB midi. Once this library is installed, the Arduino IDE can program the Leonardo or Pro Micro as a USB device in a similar manner to using the Teensy. This reprograms your chip instantly without the need for DFU programming (see below).

Musico Nerd has an awesome tutorial on how to use it.

USBMidi Library

TheUsbMidi libraryallows an Arduino board with USB capabilites to act as a MIDI instrument over USB. This works the same as Arcore, but does not need to be installed as a new hardware option. More info here.

Midi Library

This Midi Library now includes an add-on for USB Midi.

Dual MocoLufa

If you want to do MIDI on the cheap, this is a slightly harder process to understand, but it means that you can have USB MIDI on an Arduino UNO or Mega with no other software. It dual boots the arduino so you can code it, upload it, switch some connections and use it as a native USB MIDI device, test it out and if it's not quite right, just unplug it, switch the connections back, plug it in and recode it. This method doesn't require any other fancy programmer, you can just switch between them.

Lufa is the original, it was developed as a programming format for making a bunch of different types of usb devices using atmel chips. Moco Lufa was developed as a dual booting version of Lufa's midi controller and the standard arduino bootloader.

Dual Moco is a native midi programme that includes the arduino bootloader and boots as an arduino when two pins of the ISP are connected to each other. Once installed on your arduino, your arduino can be either a normal arduino or a class compliant midi device at the flip of a switch. This instructable explains how to load other programs on your arduino using DFU programming. A more comprehensive how to on Dual Moco is available on the arduino website and on the Dual Moco website.
This website shows how to build a theramin using that Moco technique.

This guy uses the moco lufa technique with his own board to create a reprogrammable Midi device.

Hiduino
Personally I'm not a fan of Hiduino. Unless you have a programmer, it seems like a lot of work when reprogramming, but check this out as an option anyways. The advantage of Hiduino is that your chip will only function as a Midi controller and does not have the arduino compatibility also built into the code. This means that you can have a larger program running on the chip and the chip is likely to run faster and have less delays as it is focused on being a MIDI device only.

Step 6: Libraries

There are a number of midi libraries that make midi coding a little easier. However, they aren't compulsory… they are just handy.

Arcore
This is a Midi library that is compatible with the Leonardo and Pro Micro boards for USB midi.

MidiUSB
This is a Midi library that is compatible with most Arduino models for USB midi.

Midi Library

This midi library now includes an add-on for USB Midi.

Step 7: Physical Hardware

There are a number of components you would do well to learn about when making a midi controller.

Digital inputs

The first are digital inputs. These are inputs that send messages to the digital input pins. The most obvious of these is a button or switch, but the options extend out with other components and libraries. These can include distance sensors, frequency sensors or PWM pulse sensors, IR receivers (for using any remote control as MIDI controllers).

The downside of digital inputs is that they only read on or off messages and nothing in between. Some sensors get around this by pulsing on and off and using a code to count the time between the pulses.

Most Midi keyboards use this idea to create velocity sensing touch. When the key is lightly pressed, it pushes the first button and starts counting, when the key is fully pressed it stops the count and sets the velocity. Although it is there to read how hard the key has been pushed, it actually reads the speed it is pushed. This is usually fairly flawless as the key is usually pushed faster when the player wants to play harder on the keyboard.

Analog inputs

The second input is an analog sensor. This reads a voltage being sent to a pin. This is used to measure a great deal of things by a wide range of sensors. Gas sensors, capacitive touch sensors, light sensors, rain sensors, vibration sensors, potentiometers, pressure sensors, bend sensors, etc. This analog input could be used to control the volume of a midi note, what note is being played, what channel it is being played on or a whole variety of interesting effects using midi control values.

Getting extra analog inputs…
What if you want to read a whole keyboard?
What if you need more than just the 6 analog inputs on an Arduino Uno?

Mux Shield: up to 48 analog inputs.

Blueberry 24 analog expander shield

Step 8: Coding With Arduino

The attached codes are written for an Arduino Uno running Moco Lufa.

They could also be used on an arduino with the midi cable wired to the tx pin as shown above. This image was taken from the arduino playground which has a helpful tutorial and first project.

Arduino codes start by introducing the arduino pins and giving them a name.

const int ButtonPin = 2;

This means that an input is at digital pin 2 of the arduino. We have named it button. "const" means ButtonPin will constantly be 2. It won't change to anything else.

The next step gives a variable to store information about ButtonPin.

int ButtonState = 0;

When we introduce it as being 0, the variable will mean 0 by default.

The next stage is setup. In setup, we tell the chip to send data at a certain speed so that it can be read at a certain speed and understood by the receiver. This is called the Baud rate.
The baud rate of MIDI is 31250.
We also let the chip know how it will be using pins. As the chips can be used as output that sends power for lighting Leds, turning on motors, etc or inputs where they read whether there is power coming in from a button or other input.

We let the chip know that ButtonPin is going to be an input.

void setup() {

Serial.begin(31250);
pinMode(buttonPin, INPUT);

}

In the code above, you may notice a couple of things. Firstly the code within the setup stage is capped by parenthesis { }. This tells the chip when that job is over. If a code has issues, check that all the parenthesis bookmark the right stages.
The second thing you will notice is that after each line of code there is a semicolon ; . This tells the chip that command is done. If you miss it, the code won't work.

The next part of the code contains a bunch of things that the chip will do over and over. This is called the loop. In the following loop, the button will be read, if it is pushed, it will send a midi note. If it is released it will send midi off.

void loop() {

ButtonState = digitalRead(ButtonPin);

if (ButtonState == HIGH) { Serial.write(0x80); Serial.write(36); Serial.write(127); //channel, note, velocity }

if (ButtonState == LOW) { Serial.write(0x90); Serial.write(0x25); Serial.write(0x00); //channel, note, velocity }

delay(1000);

}

In the code above, I have added a delay. Because the code is looping at an incredible speed, it will send a crap ton of Midi information to your computer. Once the button is pushed, the code will immediately turn the note on thousands of times a second. When the button is released it will turn the code off a thousand times a second. I have put a 1000 millisecond (1 second) delay into the code to slow this and stop the chip from freezing your computer.

There is however another way to do this. By adding a variable to store the last reading, you can get the chip to check how the button last was and ask it to do something only when it is reading something different. Remember to introduce the variable at the start of your code ( int lastButtonState = 0; ). Here is the loop stage with the last button check and a 10millisecond delay (for good measure).

void loop() {
ButtonState = digitalRead(ButtonPin);

if (ButtonState == HIGH && lastButtonState == LOW)

{ Serial.write(0x80); Serial.write(36); Serial.write(127); //channel, note, velocity }

if (ButtonState == LOW && lastButtonState == HIGH)

{ Serial.write(0x90); Serial.write(0x25); Serial.write(0x00); //channel, note, velocity }

lastButtonState=ButtonState;

delay(10); }

Other codes to try

Each of the attached codes have different components that were tested with them. Some are written for buttons, others are written for piezo sensors, distance sensors or IR remote controls. Each different code will require the components to be set out as per any other tutorials on using those components. The difference in these codes is the midi codes built in and a few basic functions that are helpful in midi controllers.

Midi_Basic is written as a test for 4 buttons.
The buttons are wired with one end to the 5v and one end to the arduino pins 2, 3, 4 & 5. Each pin is connected to the ground using a 10k resistor. This is called pulldown as the pin will usually read off.

AnalogMidi reads a voltage on analog pin 0 and sends a midi message. It can read potentiometers, flex or pressure sensors, temperature sensors or piezo disks.

Distance_sense reads a 4 pin distance sensor and outputs a midi message.

IR Midi reads an inferred sensor. It is mapped to a remote from an arduino starter pack. The hex codes may differ from your own remote and you will need to download and install the IR library to make it work.

CC switch reads the state of a switch which changes the inputs from being notes to being control changes.

Drum Presets reads piezo disks and a button. When the button is pushed, hitting the piezo disks will select a new group of notes for each disk.

Step 9: Sending Notes and Other Messages

This Arduino example is a great example of a test code


Midi specifications for all the different types of messages can be found here. I copied much of that information onto this page.


SerialWrite(0x80+ channel); //note on
SerialWrite(note); //note between 0-127
SerialWrite(velocity);//velocity between 1-127

SerialWrite(0x90+ channel); //note off
SerialWrite(note); //must be same as note on note
SerialWrite(0x00);//velocity of 0 turns note off

SerialWrite(0xC0+ channel); //program change
SerialWrite(program); //program number between 0-127

SerialWrite(0xD0+ channel); //Channel Pressure aftertouch
SerialWrite(velocity);//velocity between 1-127

SerialWrite(0xE0+ channel); //pitchbend
SerialWrite(bend); // this is a 14bit number with a max of 16384 and minimum of 0 where 8192 represents the centred position or no pitchbend.

Control Change Messages have a few different types of messages that turn them into Channel Mode Messages.

SerialWrite(0xB0+ channel); //Control Change message
SerialWrite(note); //note between 0-119
SerialWrite(velocity);//velocity between 1-127

Channel Mode Messages.
This the same code as control change, values for notes select specific messages.
120 = All Sound Off. When All Sound Off is received all oscillators will turn off, and their volume envelopes are set to zero as soon as possible. c = 120, v = 0:

121 = Reset All Controllers. When Reset All Controllers is received, all controller values are reset to their default values. (See specific Recommended Practices for defaults). c = 121, v = x: Value must only be zero unless otherwise allowed in a specific Recommended Practice.
.
c = 122, v = 0: Local Control Off: When Local Control is Off, all devices on a given channel will respond only to data received over MIDI. Played data, etc. will be ignored.
c = 122, v = 127: Local Control On: Local Control On restores the functions of the normal controllers

All Notes Off. When an All Notes Off is received, all oscillators will turn off.
c = 123, v = 0: All Notes Off (See text for description of actual mode commands.)
c = 124, v = 0: Omni Mode Off
c = 125, v = 0: Omni Mode On
c = 126, v = M: Mono Mode On (Poly Off) where M is the number of channels (Omni Off) or 0 (Omni On)
c = 127, v = 0: Poly Mode On (Mono Off) (Note: These four messages also cause All Notes Off)

Step 10: MIDI IN and Sound Engines.

If you want to read midi from a computer, keyboard or other midi controller and do some fun things with it, here are some fun things to do with that.


WAV player: This shield helps you to play audio files triggered by… well whatever you want them to be triggered by. Midi is one thing you could use. This can be used to make an electronic drum kit, add sounds to a midi keyboard for portable jamming or send midi from your phone to make a fart noise under the couch.

Synth or effects: This shield has built in sounds and synth. So you can use midi to shape waveforms and output to an amp. This could be via midi or straight from your controller.

Lighting rigs: Midi is an awesome way to control a great many things. You could write a midi code to go along with your stage performance and set up an arduino read the midi input and control relays and turn lights off and on throughout different parts of your dance or musical performance.

Midi controlled band: Here is an instructable on how to build a robotic drum kit. The arduino reads midi and writes to the motors that play the drums. You could duplicate this idea with a whole range of instruments. Here is a paper on the history of robotic instruments for some fun inspiration.

Step 11: MIDI Thru

Midi thru is designed for daisy chaining midi instruments. Midi thru is the same as the output line, except it also includes a direct copy of the input line. Anything coming in the input goes straight through.

This means that you can plug an instrument into the input, use the instrument to control effects on a midi device, but then also send the midi information out of the midi thru straight into the next instrument in a chain.

You can also use midi thru as multiple effects. If you have a single input routed to multiple through ports, you can play a single keyboard that outputs to control multiple sounds. A switch box on the box could turn off different through ports to control this.

Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017