MIDI In/Out Board Software


The MIDI IN/OUT board can be used with any microcontroller which contains a UART, including the ATmega, PIC, etc. microntrollers, to send and receive MIDI  messages.

To set Arduino Pins D2 and D3 as Outputs, to provide +5 Volt and 0 Volt(GND), include these lines in the source code: 

void setup() {

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(3, LOW); // GND 0 Volt supply to opto-coupler
digitalWrite(2, HIGH); // +5 Volt supply to opto-coupler

 

To set Arduino Pins D4, D5, D6 and D7, connected to the 4-way DIL switch,  to select the MIDI Channel (1 to 16), incclude these lines in:

 void setup() {

pinMode(4, INPUT); // Set Inputs for 4 way DIP Switch
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
digitalWrite(4, HIGH); // Set inputs Pull-up resistors High
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);

 

And in the main programme: 

void loop () {

 

// Read 4-way DIP switch
MIDIchannel=digitalRead(4) + (digitalRead(5)<<1) + (digitalRead(6)<<2) + (digitalRead(7)<<3); 


Download:

Open the  source code file and copy and paste it into the Arduino IDE Software.


/*
 NAME:                 MIDI Input to MIDI Output 
 WRITTEN BY:           TOM SCARFF
 DATE:                 4/10/2018
 FILE SAVED AS:        midi_in_out.ino
 FOR:                  Arduino
 CLOCK:                16.00 MHz CRYSTAL                                        
 PROGRAMME FUNCTION:  Detect midi input and send to MIDI output.
                        digitalWrite(3, LOW); // GND 0 Volt supply to opto-coupler
                        digitalWrite(2, HIGH); // +5 Volt supply to opto-coupler
 

 HARDWARE NOTE:
 The Midi IN Socket is connected to the Miduino RX through an 6N139 opto-isolator
 *
 * To send MIDI, attach a MIDI out Female 180 Degree 5-Pin DIN socket to Arduino.
 * Socket is seen from solder tags at rear.
 * DIN-5 pinout is:                                         _______ 
 *    pin 2 - Gnd                                          /       \
 *    pin 4 - 220 ohm resistor to +5V                     | 1     3 |  MIDI jack
 *    pin 5 - Arduino Pin 1 (TX) via a 220 ohm resistor   |  4   5  |
 *    all other pins - unconnected                         \___2___/
 *
 *
 ***************************************************************************** 
 *
 */
 

//variables setup

byte midiByte;
byte MIDIchannel;
byte x;

int LedPin = 13;   // select the pin for the LED

//setup: declaring iputs and outputs and begin serial 
void setup() {
 
  pinMode(2, OUTPUT);     
  pinMode(3, OUTPUT);  
  digitalWrite(3, LOW); // GND 0 Volt supply to opto-coupler
  digitalWrite(2, HIGH); // +5 Volt supply to opto-coupler
    
  pinMode(4, INPUT); // Set Inputs for 4 way DIP Switch
  pinMode(5, INPUT); 
  pinMode(6, INPUT); 
  pinMode(7, INPUT); 
  digitalWrite(4, HIGH); // Set inputs Pull-up resistors High
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
 

  pinMode(LedPin,OUTPUT);   // declare the LED's pin as output

  for (x=1; x<=4; x++){
    digitalWrite( LedPin, HIGH );
    delay(300);
    digitalWrite( LedPin, LOW );
    delay(300);
  }
  
  
  //start serial with midi baudrate 31250 or 38400 for debugging
  Serial.begin(31250);        

  Serial.flush();
}

//loop: wait for serial data, and output the message 
void loop () {
  
   // Read 4-way DIP switch
  MIDIchannel=digitalRead(4) + (digitalRead(5)<<1) + (digitalRead(6)<<2) + (digitalRead(7)<<3);

  
    
       if (Serial.available() > 0) { 
          digitalWrite(LedPin,HIGH);   
          midiByte = Serial.read();
          Serial.write(midiByte); 
          digitalWrite(LedPin,LOW);          
       }

}