MIDI Note Player using the MIDI firmware for the 8U2 (Uno and Mega2560 only)

This tutorial shows how to play MIDI notes from an Arduino programmed to appear as a general MIDI device.

This tutorial shows how to play MIDI notes from an Arduino programmed to appear as a general MIDI device. This tutorial is only applicable to Arduinos with the 8U2 chip, the UNO and Mega2560 boards. This will not work with earlier models that use the FTDI chip (Duemilanove and previous).

Following the instructions on the Programming 8U2 instructions using the MocoLUFA firmware, your Arduino will appear to the computer as a native MIDI device without any additional hardware.

You can read the Wikipedia MIDI entry for complete information on the specification, but in a nutshell, MIDI is a useful protocol for controlling synthesizers, sequencers, and other musical devices. MIDI devices are generally grouped in two broad classes: controllers (i.e. devices that generate MIDI signals based on human actions) and synthesizers (including samplers, sequencers, and so forth).

For more information, see this introduction to MIDI or this example.

The Serial.print() function will send a MIDI command to the software synthesizer of your choice (in this example we will use Cycling74's Max/MSP, but any application that accepts MIDI will do).

Before programming the 8U2 firmware, you will need to program the Atmega 328 on your Arduino, the same way you usually do with the IDE. We will be using the MIDI communication example that comes with the Arduino software. It can be found in File>Examples>Communication>MIDI.

Code

1/*
2 MIDI note player
3
4 This sketch shows how to use the serial transmit pin (pin 1) to send MIDI note data.
5 If this circuit is connected to a MIDI synth, it will play
6 the notes F#-0 (0x1E) to F#-5 (0x5A) in sequence.
7
8
9 The circuit:
10 * digital in 1 connected to MIDI jack pin 5
11 * MIDI jack pin 2 connected to ground
12 * MIDI jack pin 4 connected to +5V through 220-ohm resistor
13 Attach a MIDI cable to the jack, then to a MIDI synth, and play music.
14
15 created 13 Jun 2006
16 modified 13 Aug 2012
17 by Tom Igoe
18
19 This example code is in the public domain.
20
21 http://www.arduino.cc/en/Tutorial/Midi
22
23 */
24
25void setup() {
26 // Set MIDI baud rate:
27 Serial.begin(31250);
28}
29
30void loop() {
31 // play notes from F#-0 (0x1E) to F#-5 (0x5A):
32 for (int note = 0x1E; note < 0x5A; note ++) {
33 //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
34 noteOn(0x90, note, 0x45);
35 delay(100);
36 //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
37 noteOn(0x90, note, 0x00);
38 delay(100);
39 }
40}
41
42// plays a MIDI note. Doesn't check to see that
43// cmd is greater than 127, or that data values are less than 127:
44void noteOn(int cmd, int pitch, int velocity) {
45 Serial.write(cmd);
46 Serial.write(pitch);
47 Serial.write(velocity);
48}

Program your Arduino, and prepare to program the 8U2 chip. Follow the instructions on the Programming 8U2 page, replacing the Arduino firmware with the MocoLUFA firmware.

After the 8U2 has been updated with the MIDI firmware, disconnect and reconnect the USB cable.

Open your MIDI application of choice and look for connected devices. You should see the Arduino appear as an input and output device named "MIDI/MOCO for LUFA".

MIDI device list
MIDI device list

A simple Max patch that will allow you to hear the notes generated by the MIDI commands looks like this :

Max patch
Max patch

To change the program on the Arduino, you must first reset the firmware on the 8U2 to the Arduino's default.

See Also:

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.