(edit sidebar)
Intro to Physical Computing Syllabus

Research & Learning

Other Class pages

Shop Admin

ITP Help Pages
Tom's pcomp site
DanO's pcomp site


Controlling Garage Band with Arduino

Overview

This tutorial shows you how to connect an Arduino making MIDI sounds to MIDI applications via a USB-to-MIDI interface.
For a basic setup of MIDI Output on the Arduino module, see MIDI Output.

For this lab you'll need:

Solderless breadboard
Solderless breadboard
hookup wire
22-AWG hookup wire
Arduino module
Arduino Microcontroller
module
MIDI connector
MIDI connector


resistors
10Kohm resistors
resistors
220 ohm resistors
push button switch
Push button switch
MIDISport
USB-to-MIDI interface


Click on any image for a larger view.


Arduino + GarageBand

Here we will control GarageBand from switches connected to the Arduino board. Connect switches to digital input on the Arduino like you did in the digital lab.
The switch shown below is a Mini Push Button Switch, but you can use any switch.


prepare
Prepare the breadboard

Connect power and ground on the breadboard to power and ground from the microcontroller.
On the Arduino module, use the 5V and any of the ground connections.

setup
Connect the Sensors

Connect Switches to digital input on the Arduino.


* Pin 8
* Pin 9
* Pin 10
* Pin 11
* Pin 12
* Pin 13


midisetup
Connect the MIDI connector

Add the MIDI connector.


Looking at the 5 pin din MIDI female connection from the back, point the five soldering lugs downwards.
For a more detailed schematic, see the MIDI notes on Tom's physical computing site.


Now that the circuit set-up is done we are ready to play with the sound.


midisetup
Connect the MIDI connector
midisetup
Connect the MIDI connector
tx
Connect the MIDI connector



Program the Module

Look over the code and read a description before you use it.
Program your Arduino with the following code:

/*
 * Convert Arduino to a MIDI controller using 6 digital inputs. 
 *
 * This sketch is set up to send 6 MIDI notes on MIDI channel 1,
 * but it can be easily reconfigured for other notes and channels
 *
 * Created 3 Nov 2008
 * By Hyeki Min
 */

// define the pins we use
int switchPin1 = 8;      
int switchPin2 = 9;
int switchPin3 = 10;
int switchPin4 = 11;
int switchPin5 = 12;
int switchPin6 = 13;

// general midi notes
char note1 = 60; //Middle C
char note2 = 62; //D
char note3 = 64; //E
char note4 = 65; //F
char note5 = 67; //G
char note6 = 69; //A

// Variables
int switchState1 = LOW;
int switchState2 = LOW;
int switchState3 = LOW;
int switchState4 = LOW;
int switchState5 = LOW;
int switchState6 = LOW;
int currentSwitchState1 = LOW;
int currentSwitchState2 = LOW;
int currentSwitchState3 = LOW;
int currentSwitchState4 = LOW;
int currentSwitchState5 = LOW;
int currentSwitchState6 = LOW;


void setup() {

  // set the states of the I/O pins:
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(switchPin3, INPUT);
  pinMode(switchPin4, INPUT);
  pinMode(switchPin5, INPUT);
  pinMode(switchPin6, INPUT);
  // set MIDI baud rate :
  Serial.begin(31250); 

}

void loop() {

  //switchPin1
  currentSwitchState1 = digitalRead(switchPin1);
  if( currentSwitchState1 == LOW && switchState1 == HIGH ) // push
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note1, 0x45); 
  if( currentSwitchState1 == HIGH && switchState1 == LOW ) // release
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note1, 0x00);
  switchState1 = currentSwitchState1;

  //switchPin2
  currentSwitchState2 = digitalRead(switchPin2);
  if( currentSwitchState2 == LOW && switchState2 == HIGH ) // push
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note2, 0x45); 
  if( currentSwitchState2 == HIGH && switchState2 == LOW ) // release
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note2, 0x00);
  switchState2 = currentSwitchState2;

  //switchPin3
  currentSwitchState3 = digitalRead(switchPin3);
  if( currentSwitchState3 == LOW && switchState3 == HIGH ) // push
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note3, 0x45); 
  if( currentSwitchState3 == HIGH && switchState3 == LOW ) // release
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note3, 0x00);
  switchState3 = currentSwitchState3;

  //switchPin4
  currentSwitchState4 = digitalRead(switchPin4);
  if( currentSwitchState4 == LOW && switchState4 == HIGH ) // push
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note4, 0x45); 
  if( currentSwitchState4 == HIGH && switchState4 == LOW ) // release
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note4, 0x00);
  switchState4 = currentSwitchState4;

  //switchPin5
  currentSwitchState5 = digitalRead(switchPin5);
  if( currentSwitchState5 == LOW && switchState5 == HIGH ) // push
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note5, 0x45); 
  if( currentSwitchState5 == HIGH && switchState5 == LOW ) // release
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note5, 0x00);
  switchState5 = currentSwitchState5;

  //switchPin6
  currentSwitchState6 = digitalRead(switchPin6);
  if( currentSwitchState6 == LOW && switchState6 == HIGH ) // push
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note6, 0x45); 
  if( currentSwitchState6 == HIGH && switchState6 == LOW ) // release
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note6, 0x00);
  switchState6 = currentSwitchState6;

}

// Send a MIDI note-on/off message.  
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}



MIDI Interface

Since we are using our circuit as a hardware MIDI device, a MIDI interface is needed. The more gear you have the more ports you need. You can find 1x1, 2x2, 4x4 and 8x8 MIDI interfaces commonly available. A simple m-audio MIDIsport 2x2 will do just fine for most people. This would give you 32 channels on 2 ports. That means only 2 devices can go into the computer, but as many as 32 could go out, given that each device only has 1 channel. The MIDISport 2X2 can be found in the ER.

Midisport
MIDISPORT 2X2
Midisport"
MIDISPORT 2X2

You would need to install the drivers. These can be found in the drivers directory of the M-Audio Site.
Once you've installed the driver, you can identify your MIDI hardware in the Utilities folder inside the Applications folder.
Open it and Click on the MIDI Devices tab.
You should see a window like below:



Midisport"
MIDI Devices

If your MIDI interface does not appear, make sure it is powered on and connected to your computer. If your MIDI interface is grayed out, that means this interface was previously present when you ran a MIDI application, but the driver could not locate it, or the driver is no longer installed. If your MIDI interface appears and is not grayed out, like shown above, this means that your device’s driver has successfully located the hardware and all is well. You can begin using your MIDI applications without any further setup.

GarageBand Setup

Now, open up GarageBand - note that if it's already open, you'll have to relaunch it for the new MIDI interfaces to be recognized. This setting is found in preferences menu for GarageBand. In the preferences panel, click on "Audio/MIDI" and you'll see GarageBands Audio and MIDI input settings. An example of the Audio/MIDI setup panel is shown below:

Midisport"
GarageBand Preferences
Midisport"
Check MIDI Status



Now that you've got your MIDI Interface working properly you can explore the vast collection of soft synthesizer sounds with GarageBand.

MIDI Note Numbers for Different Octaves

The MIDI specification only defines note number 60 as "Middle C", and all other notes are relative. The absolute octave number designations shown here are based on Middle C = C4, which is an arbitrary assignment. There is a discrepancy that occurs between various models of MIDI devices and software programs, and that concerns the octave numbers for note names. If your MIDI software/device considers octave 0 as being the lowest octave of the MIDI note range, then middle C's note name is C5. The numbers used are 0 to 127. The lowest note upon a MIDI controller is a C and this is assigned note number 0. The C# below it would have a note number of 1. The D note below that would have a note number of 2. So "Middle C" is note number 60.

MIDI notes
MIDI Note Numbers


Organization MIDI commands and data in a byte of information

MIDI bytes range between 0 and 255. Note that a byte is a binary number that contains 8 digits. Furthermore, command bytes are split into half. The most significant half contains the actual MIDI command, and the second half contains the MIDI channel for which the command is for. For example, 0x91 is the note-on command for the second MIDI channel.


Here is a table of the MIDI commands:

MIDI commands
The MIDI Commands


* Note On - This message indicates the beginning of a MIDI note and consists of 3 bytes. The 1st byte (Status byte) specifies a note-on event and channel. The 2nd byte specifies the number of the note played. The 3rd byte specifies the velocity with which the note was played.
* Note Off - This message indicates the end of a MIDI note. The 1st byte (Status byte) specifies a note-off event and channel. The 2nd byte specifies the number of the note played. The 3rd byte specifies the release velocity.
* Aftertouch - This message is sent when additional pressure is applied to a key that is already being held down.

The messages from 0x80 to 0xEF are called Channel Messages because the second four bits of the command specify which channel the message affects. The messages from 0xF0 to 0xFF are called System Messages. they do not affect any particular channel.

Status Byte
Status Byte
  Edit | View | History | Print | Recent Changes | Search Page last modified on March 11, 2009, at 02:13 PM