Go Down

Topic: MIDI Controller (Read 18819 times) previous topic - next topic


Dec 11, 2006, 11:39 am Last Edit: Dec 11, 2006, 01:16 pm by Tubedogg Reason: 1

I just registered to this Forum to share my experiences with the great Arduino-Platform so far.
I programmed a simple MIDI-Controller that reads 6 analog sensors and outputs MIDI CC messages.
As far as I know this has not been posted before and i think its a good adddition to the Midi Drum trigger and similar projects. It can be used to directly control any gear that accepts MIDI data.
The Hardware Midi-Out is shown in the diagram:

The code:

// by Tubedogg 12/2006; fabian at tubedogg.de

//This program reads up to 6 analog sensors / potentiometers ans converts the data to Midi-Controller messages
// define Switchpin:
#define switchPin 10
//  define LED:
#define LEDpin 13

// Variables

// define variables for the controller data
int AnalogValue[6] = {0,0,0,0,0,0};    

// define the "lastValue" variables
int lastAnalogValue[6] = {0,0,0,0,0,0};
// select the midi Controller Number for each input
int midiCCselect[6] = {1,2,3,4,5,6};

// select threshold for each analog input  
int thresh[6] = {1,1,1,1,1,1};  

// select number of desired analog inputs (max 6)
int input_no = 6;

void setup() {
 //  set the states of the I/O pins:
 pinMode(switchPin, INPUT);
 pinMode(LEDpin, OUTPUT);
 //  Set MIDI baud rate:


// main program loop
void loop() {

for(int i=0;i<input_no;i++){

         AnalogValue = (analogRead(i))/8;    // read the value from the analog input and divide by 8 for range 0-127
      // check if value is greater than defined threshold (good for resistive touchpads etc)
     if ( AnalogValue >thresh ) {
           // check if analog input has changed
           if ( AnalogValue != lastAnalogValue ) {

           //send control change on cc#i
           midiCC(0xB0, midiCCselect, AnalogValue);
           // update lastAnalogValue variable
           lastAnalogValue = AnalogValue;

           //End if
       //End if
//End for

//End Loop

// This function sends a Midi CC.
void midiCC(char CC_data, char c_num, char c_val){
 Serial.print(CC_data, BYTE);
 Serial.print(c_num, BYTE);
 Serial.print(c_val, BYTE);


I hope there's no errors anymore; it worked fine for me so far.

Best regards,


this thread was read 5856  times but no reply? Strange.
I just tried your code right now with Arduino0014 and it works out of the box, just as I expected. Great work!

I use it to send Midi CC messages to my guitar amp. I connected a modified wah-wah pedal to the analog input and now I have a digital expression pedal. Great! And easy, thankx a lot.


Sep 25, 2009, 03:39 pm Last Edit: Sep 25, 2009, 04:04 pm by rliivt Reason: 1
Hey!  I tried this now, and am using Pure Data to receive MIDI on a computer, and it works, somewhat.. I am using a flex sensor as controller, and have hooked the midi into my edirol Ua-25 soundcard. I got two rather annoying problems though, but they only appear after the patch in Pure Data have been running for  a while:

1.  The midi output is delayed after i change position of the flex sensor.  It varies how much it is delayed, seemingly somewhat based on how long it is since I turned on the computer, and how long it is since the flex sensor have been bent for some reason.

2.  In addition to this, I get an extra signal, delayed after the first one.  So i basically got two signals, the "original" first, and then a "copy", so if i bend it down the midi values first go down, then skip up again to the initial values, then go back down again.  Needless to say this is not ideal.

Do anyone have any ideas as to why this happens, and perhaps can offer any help on a solution?  I am still such a noob at arduino that I do not know where to start troubleshooting the issue.  :-?'

UPDATE:  I restarted Pure Data, wich i am using and it then works fine for a while... But after a time again it starts skipping and delaying and stuff.  But perhaps the problem here lies with Pure Data and not arduino? hmm....



I will risk asking the obvious: Why is there a maximum of 6 CC messages? Is that arbitrary for this patch or is there a hardware limit? If there is a hardware limit is that different for different Arduino boards? Can one send Note ON/OFF messages in addition to these CC messages?

thank you so much for your help,


Cheers for posting this mate, its been very helpful in understanding how to convert arduino code into MIDI messages. I just had a query I thought I would run past you though...

I'm building a project that involves using a Sharp distance measuring infra-red sensor as the input. I plan on using the sensor to trigger MIDI messages via the Arduino to be sent to Reason or Ableton to be used as a MIDI controller (though I think I will have to use MAX/MSP some where along the line to help send the MIDI from the arduino to Reason? Though I'm not quite sure how to go about this part yet...)

Anyway my query was: your controller has 6 analog inputs into the Arduino where as my IR sensor only needs 1 input - which I have connected to analog 0. I was just wondering if you could help me modify the code you have posted above and change it to suit my means. Is it just a case of changing the input value to analog 0 for me? If so - which lines need to be changed?

So far I have been able to successfully connect the sensor with my arduino by using this following code:

int var1 = 0;

void setup() {

void loop(){
var1 = analogRead(0);

I used this code to see if the sensor was working and used the serial monitor which showed that the sensor was indeed communicating the Arduino because a new line of code was being written as I moved my hand in front of the IR sensor - with the number changing depending how close or far away from the sensor my hand was.

What I'm wondering is - do I still use the above little code I used to send information from my sensor to the arduino; and then just add a modified version of your code for MIDI instructions and upload that after uploading my code and add that afterwards?

Cheers for your help man - much appreciated!!!  :)


Hi guys,

Just by chance I came back upon this thread; I almost can't believe that after it's been online for 3 three years, people have been finally replying to this...
A lot has happened since and with the now available MIDI library (http://www.arduino.cc/playground/Main/MIDILibrary) this is probably obsolete anyway.... Still I do appreciate the feedback!



i am new to arduino, more familiar with building/modifying hardware devices, working with pd and constructing stringinstruments out of industriall waste from the scrapyard.
i appreciate very much your code posting as it is a working thing i can use to get things going.
my project is to integrate pitchbend controller and reading the tempo of some delay ,sampler machine through connecting the loopstart led of those devices  to the arduino and generate midiclock.
still many things to learn...


Hi Yann,

sounds like an interesting project you have.
I did some experiments with midi clock interfacing my DR55 and Gameboy LSDJ using the following code for a start. (I got it from somewhere online; didn't really write it myself though)
Code: [Select]
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
int syncoutPin = 3; //clock out pin
int pulsewidth = 25; //clock pulse length in ms
byte data;

void setup() {
pinMode(syncoutPin, OUTPUT);
digitalWrite(syncoutPin, HIGH);


void loop() {
if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
else if(data == midi_continue) {
play_flag = 1;
else if(data == midi_stop) {
play_flag = 0;
else if((data == midi_clock) && (play_flag == 1)) {

void Sync() {
   digitalWrite(syncoutPin, HIGH);
   digitalWrite(syncoutPin, LOW);



I bought the material for do ityoutube.com/watch?v=1p2PwtdcCNg . I have  problems
with the programation. You can help me?

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131