Monday 15 March 2021

Arduino Midi note player - MIDI out DIN connector

This project is good for testing some hardware connections. 
I mainly wanted to test a MIDI out connector.
 
 
It's also a good first Arduino midi project.
Super simple.
MIDI, the Musical Instrument Digital Interface, is a useful protocol for controlling synthesizers, sequencers, and other musical devices
 
If this circuit is connected to a MIDI synth, it will play the notes
  F#-0 (0x1E) to F#-5 (0x5A) in sequence.
You don't need a breadboard. 
The code is in the public domain.
 It was created on the  13 Jun 2006 & modified 13 Aug 2012
  by Tom Igoe. Thanks Tom.

I'm using old fashioned DIN connectors (rather than USB).
ie a  hardware serial connection.
The Uno can’t communicate using MIDIUSB, btw.,
but the Leonardo can.
These have 5 pins. MIDI DIN connectors use just the middle 3.
The centre pin is always ground.




The other two pins have 220 ohm resistors soldered. They connected to pins 5V & Tx
on the Arduino.










Note:
Some of the MIDI OUT connectors I've seen on the web, use just one 220 ohm resistor (on the 5V connector).
I'm not sure if these are any better.

I'll make a MIDI IN connector in a later post. I understand many use optocouplers to protect the microcontroller and prevent ground loops.

 
I'm using a Arduino Uno in this project.


MIDI is a serial protocol that operates at 31,250 bits per second. 
It's digital. Data is usually notated in hexadecimal.
MIDI banks are usually grouped in banks of 16.

Unlike analog clocks & voltages that I'm used to using, 
MIDI signals are sent in the form of bytes.

These can be divided into two types: command bytes and data bytes
 
Command bytes are always 128 or greater, or 0x80 to 0xFF in hexadecimal. 
If you convert these numbers to binary, you will see they range between
 10000000 to 11111111..... that is, the first number (MSB or most significant bit) is always a one.
This is how it knows its a Command byte.
These include things like note on, note off, pitch bend, aftertouch, continuous controller, channel pressure, .

Data bytes are always less than 127, or 0x00 to 0x7F in hex. 
If you convert these numbers to binary, you will see they range between
 00000000 to 01111111..... that is, the first number (MSB or most significant bit) is always a zero.
This is how it knows its a Data byte.
These include things like Pitch, Velocity , pitch bend amount & loudness.
 
The order of the binary numbers tell the synthesizer lots 
The first 3 bits after the MSB set the type of command.
The last 3 bits set the midi channel.

For more details, see the MIDI specification or one of the many MIDI Protocol Guides on the Web.


The serial ports are TX & RX - digital pins 1 & 0.

Here is the schemo

The 5-pin DIN Jack in this pic is viewed from the front.

Red = Tx
Black = Gnd
White = 5V

--------------------------------------------------------------------

This is the code:

/*
  MIDI note player

  This sketch shows how to use the serial transmit pin (pin 1) to send MIDI note data.
  If this circuit is connected to a MIDI synth, it will play the notes
  F#-0 (0x1E) to F#-5 (0x5A) in sequence.

  The circuit:
  - digital in 1 connected to MIDI jack pin 5
  - MIDI jack pin 2 connected to ground
  - MIDI jack pin 4 connected to +5V through 220 ohm resistor
  - Attach a MIDI cable to the jack, then to a MIDI synth, and play music.

  created 13 Jun 2006
  modified 13 Aug 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Midi
*/

void setup() {
  // Set MIDI baud rate:
  Serial.begin(31250);
}

void loop() {
  // play notes from F#-0 (0x1E) to F#-5 (0x5A):
  for (int note = 0x1E; note < 0x5A; note ++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    delay(100);
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00);
    delay(100);
  }
}

// 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);
}

--------------------------------------------------------------------------------------





Links
 
 ---------------------------------
-------------------------------------

1 comment:

  1. Great Post! Nice information you shared. The demand of din connectors has increased.

    ReplyDelete