Sending MIDI note with Arduino Uno

Hello everybody!

I’m trying to send MIDI note messages to my pc via Arduino Uno. Till now I’ve got just one button (so it should be easy), a MIDI output circuit and MIDI to USB converter cable.

Anyway the pc doesn’t receive the messages and (testing all the other parts of the circuit) I think the problem is in the code, but I can’t figure out what it is.

I’ve also have a MIDI monitor that doesn’t display the MIDI messages.

Thanks!

Here the code:

Button_State_Modified.ino (870 Bytes)

So exactly how is your MIDI output connected to the PC? 5-pin DIN cables? What does your MIDI Out circuit look like and what is it connected to on the Arduino?

Steve

MIDI.sendNoteOn(36, 127, 1);

Is not how you send a MIDI note on message.

You also can't serial write to the monitor and send MIDI messages at the same time with this library.

You seem to be mixing up the code that uses the MIDI library and that which doesn't

Here is how to send random note on / off messages

/ Arduino pin assignments
#define midiChannel (byte)0

// Start of code
void setup() {
 //  Setup serial
   Serial.begin(31250);    // MIDI speed

}

//********************* MAIN LOOP ***********************************

void loop() {
  int val;
  val = random(20,100);
    noteSend(0x90, val, 127);
    delay(200);
    noteSend(0x80, val, 127);
   delay(800);
    } // end loop function
    
//********************* Functions *********************************** 


//  plays a MIDI note
 void noteSend(char cmd, char data1, char data2) {
  cmd = cmd | char(midiChannel);  // merge channel number
  Serial.write(cmd);
  Serial.write(data1);
  Serial.write(data2);
}

If you want to use the MIDI library then your code should look like this:-

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

const int pinButton = 4;

int buttonstate = 0;
int lastbuttonstate = 0;

void setup() {
  
  //MIDI.begin(MIDI_CHANNEL_OMNI);
  Serial.begin(31250);
  delay(100);
  

  pinMode(pinButton, INPUT_PULLUP); // if you are using a pull down resistor then remove it.
}

void loop() {
  buttonstate = digitalRead(pinButton);
  
  if(buttonstate != lastbuttonstate) {
    if (buttonstate == LOW){
      //Serial.println("PRESSED"); 
      MIDI.sendNoteOn(36, 127, 1);
    }
     
    else {
     //Serial.println("RELEASED"); 
     MIDI.sendNoteOff(36, 0, 1);
     
    }
  }

  lastbuttonstate = buttonstate; 
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
//void NoteOn(int cmd, int pitch, int velocity) {
//  Serial.write(cmd);
//  Serial.write(pitch);
//  Serial.write(velocity);
//}

Pay attention to this line:-

pinMode(pinButton, INPUT_PULLUP); // if you are using a pull down resistor then remove it.

@Grumpy_Mike thanks I used the MIDI library. But now when I press the button the MIDI monitor display an invalid message like this instead of a NoteOn

slipstick I've got a 5-pin DIN Cable

pin 2 - GND pin 4 - to +5V through a 220 ohm resistor pin 5 - TX on Arduino

MIDI to USB converter plugged in the pc and with Ableton open I'm trying to use a piano plug-in

But now when I press the button the MIDI monitor display an invalid message like this instead of a NoteOn

So you must have something wrong with the hardware because I have tested those two codes on my own machine before I posted them.

A common thing to get wrong is the wiring of the 5-pin socket, Some diagrams don't show what side of the connector you are looking at, hence you get pins 4 and 5 swapped over. There is no mention of the second resistor, see https://www.arduino.cc/en/tutorial/midi

Thanks a lot Grumpy_Mike now it all works!

Just a last question. Now that I know for sure that both the code and the hardware work I have to expand the project to an actual keyboard.

I designed a pcb of a matrix for 8 keys (for now) wired to a shift register. Do you think it's better to use the code which include the midi library or the one that not? Thanks!

I have to expand the project to an actual keyboard. ...... I designed a pcb of a matrix for 8 keys (for now) wired to a shift register.

When you say keyboard do you mean piano type keyboard? A simple matrix will give you note on information but no velocity sensitive information for the velocity MIDI parameter. For this you need a key with two contacts a normally closed and normally open. You need to design it so that when the normally closed starts timing when it is released and when the normally open becomes closed you stop the timer. The difference between finish and start time is then mapped to a range of velocities 0 to 127.

Do you think it's better to use the code which include the midi library or the one that not?

I would start off by writing some stand alone code so you get the matrix bit right, and then incorporate it into your existing code if you need to.

That way if you need help you can simply post the bit you are having trouble with and not the whole Bluetooth stack of things.

Grumpy_Mike:
When you say keyboard do you mean piano type keyboard? A simple matrix will give you note on information but no velocity sensitive information for the velocity MIDI parameter. For this you need a key with two contacts a normally closed and normally open. You need to design it so that when the normally closed starts timing when it is released and when the normally open becomes closed you stop the timer. The difference between finish and start time is then mapped to a range of velocities 0 to 127.

Yes piano keyboard. I’m trying to restore an old synth I have.
Thanks, I’ll post here if something doesn’t work!