Teensy 4.0 and MIDI hardware serial port issue

Hello, I used this schematic from the Notes and Volts video to build a MIDI hardware serial port for my Yamaha P95, but after I uploaded this code:

#include <MIDI.h>




MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);




// -----------------------------------------------------------------------------




// This example shows the old way of checking for input messages.

// It's simpler to use the callbacks now, check out the dedicated example.




const int Led14 = 14;




// -----------------------------------------------------------------------------




void BlinkLed(byte num)         // Basic blink function

{

    for (byte i=0;i<num;i++)

    {

        analogWrite(Led14, 5);

        delay(50);

        analogWrite(Led14, 0);

        delay(50);

    }

}




// -----------------------------------------------------------------------------




void setup()

{

    pinMode(Led14, OUTPUT);

    MIDI.begin();           // Launch MIDI, by default listening to channel 1.

}




void loop()

{

    if (MIDI.read())                // Is there a MIDI message incoming ?

    {

        switch(MIDI.getType())      // Get the type of the message we caught

        {

            case midi::ProgramChange:       // If it is a Program Change,

                BlinkLed(MIDI.getData1());  // blink the LED a number of times

                                            // correponding to the program number

                                            // (0 to 127, it can last a while..)

                break;

            // See the online reference for other message types

            default:

                break;

        }

    }

}

the Teensy behave like this:

I have zero knowledge about Arduino, programming language and the Teensy.
The only thing I know now, is how to add a led to the circuit and use it in place of the one embedded in the Teensy.

The components I used in the circuit:

  • 5 Pin DIN female plug
  • 220 Ohm resistor
  • 470 Ohm resistor
  • 6N138 optocoupler
  • 1N4148 diode
  • Teensy Audio Shield Rev D (I had to solder the pins for the breadboard and sandwich connection with the Teensy 4.0)

Pin 2 and 3 are connected with the 1N4148 diode, there is a 220 Ohm resistor bridge with the jumper that connects to the Pin 5 of the MIDI connector.

Pin 5 of the MIDI connector is connected to Pin 3 of the optocoupler (there is a bridge with the 1N4148 diode).

Pin 8 is connected to the 5V of the Teensy.

Pin 6 of the optocupler is connected to the RX1 (Pin 0) of the Teensy. After the jumper wire, you can see the 470 Ohm resistor connected to the 3.3V of the Teensy.

Pin 5 of the optocoupler is connected to GND.

I upload the sketch, plug the MIDI cable, turning on the keyboard and the led goes berserk.

Does the code indicates that there is communication between the keyboard and the Teensy and that is working properly?

I don't understand what this supposes to mean and what I am doing wrong.

Are you using EXACTLY the same model of diode that he used in the video?

The Teensy 4.0 is a 3.3V-only microcontroller and applying 5V will damage it.

You'll either need some level shifters, or use an appropriate 3.3V MIDI circuit (with the caveat that the low resistor values used in those circuits do not protect the TX pin from short circuits). See e.g.
https://midi.org/5-pin-din-electrical-specs.

Hello, thank you for your reply.

The 1N4148 diode I am using is this one:

https://www.ebay.co.uk/itm/152056020912?var=451255897046

and as you pointed out, it looks like they are different, the one I bought has a Max Reverse Voltage of 100V and a Max Rectified Current of 200mA.

The ones he uses in the video have the VRRM (which I don't know what the letters stand for) of 75V and a Rectified Current of 150mA.

I didn't know the 1N4148 comes in different voltage and rectified current. I am going to buy the correct product this time.

I did not know that connecting the Pin 8 of the optocoupler to the 5V Pin of the Teensy could damage it, I think I missed the part where the author says that is dangerous.

For the 5 Pin DIN, I am using this one I bought from The Pi Hut:

It should be fine to just connect the pullup for the opto-coupler to 3.3v


This worked for me.

@blacksad Keep a close eye on 5-pin DIN pinout. The numbering for the pins is not information that is consistent when searching online. If the MIDI circuit is correct then it won't harm to try them each way around. (That was one of the design considerations)

Hello @Deva_Rishi, thank you for your reply and for sharing the schematic.

I apologise in advance, but I am not very familiar with schematics *(I forgot to mention in this post that my knowledge to electronics has started last Wednesday and the only thing I know how to do is to assign a variable to turn on a led...)
*
from what I understood, the Pin 8 of the opto-coupler goes to the 3.3v (1), Pin 5 goes to GND (2), but does it go to the 3.3v too?
Also I don't understand what happen with Pin 5 and Pin 7 (5) it looks like they share a 10K resistor.
Pin 6 shares a 470 resistor with Pin 8? (4)
(5) is this the name for an arduino board?

Sorry for all the questions, but I am having a hard time to understand the connections!

No ! It goes to GND of the teensy and through a 10K resistor it connects to pin 7
Pin 8 connects though a 407R Resistor to pin 6 and then to the Rx line of the Teensy

No i was wondering about that too, i am actually not really sure what it is and what it is doing there, but it is an old schematic i made and did not remove and found as a quick way to post it.

Usually i use 1N136 as an opto-coupler but for that the schematic is a little different.

Those are different things, but i would suggest you start reading some books on the matter.

The most important thing is not to get things working but to figure out how things actually work,

Thank you, I will try with the guides for dummies first :sweat_smile:

Could you please let me know if is this is the configuration that you meant?

If you have not already done so, I would recommend posting on the [PJRC forum]( Teensy Forum)
There are a lot of Teensy users who do a lot of stuff up there using Midi and other Audio type projects.

No, pin 8 to 3.3v

Pin 5 of the DIN Through the 220R resistor to pin 2 (this is important, the current limiting resistor needs to be in series !!)

But again, i do recommend you start reading some books, like electronics for dummies.

Hello KurtE, thank you for your recommendation. I am just waiting for the admin to approve my registration to the forum, but it has not happened yet.

@Deva_Rishi

Is this schematic correct now?

Almost... ah somehow i can see why you've messed up the pin numbers now (you had them correctly before) and one of the resistor values is non longer correct (it should be 10K not 10R)

pin 8 (Vcc) connects to Pin 6 through a 470R
Pin 5 (GND) connects to Pin 7 through a 10K

Pin numbering is counter clockwise starting at the mark.

@Deva_Rishi Like this?

Yeah that looks right.

It looks like something is wrong, I uploaded this sketch but my Yamaha keyboard and the Teensy aren't communicating. The led is not switching on when I press a note on the keyboard:

#include <MIDI.h>

#define LED 13

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

void setup () {
  MIDI.begin(MIDI_CHANNEL_OMNI);
  pinMode (LED, OUTPUT);
  MIDI.setHandleNoteOn(MyHandleNoteOn);
  MIDI.setHandleNoteOff(MyHandleNoteOff);


}

void loop () {
  MIDI.read();
}

void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
  digitalWrite (LED, HIGH);
}

void MyHandleNoteOff (byte channel, byte pitch, byte velocity) {
  digitalWrite (LED, LOW);
}


and if I use this Basic_IO.ino example from the MIDI library, the led on the Teensy it stays on and it doesn't react to the notes I press on the keyboard:

#include <MIDI.h>




// Simple tutorial on how to receive and send MIDI messages.

// Here, when receiving any message on channel 4, the Arduino

// will blink a led and play back a note for 1 second.




MIDI_CREATE_DEFAULT_INSTANCE();




void setup()

{

    pinMode(LED_BUILTIN, OUTPUT);

    MIDI.begin(4);                      // Launch MIDI and listen to channel 4

}




void loop()

{

    if (MIDI.read())                    // If we have received a message

    {

        digitalWrite(LED_BUILTIN, HIGH);

        MIDI.sendNoteOn(42, 127, 1);    // Send a Note (pitch 42, velo 127 on channel 1)

        delay(1000);                    // Wait for a second

        MIDI.sendNoteOff(42, 0, 1);     // Stop the note

        digitalWrite(LED_BUILTIN, LOW);

    }

}

I added MIDI.begin(MIDI_CHANNEL_OMNI); to the code like this:

#include <MIDI.h>




// Simple tutorial on how to receive and send MIDI messages.

// Here, when receiving any message on channel 4, the Arduino

// will blink a led and play back a note for 1 second.




MIDI_CREATE_DEFAULT_INSTANCE();




void setup()

{

    pinMode(LED_BUILTIN, OUTPUT);

    MIDI.begin(MIDI_CHANNEL_OMNI);                      // Launch MIDI and listen to channel 4

}




void loop()

{

    if (MIDI.read())                    // If we have received a message

    {

        digitalWrite(LED_BUILTIN, HIGH);

        MIDI.sendNoteOn(42, 127, 1);    // Send a Note (pitch 42, velo 127 on channel 1)

        delay(1000);                    // Wait for a second

        MIDI.sendNoteOff(42, 0, 1);     // Stop the note

        digitalWrite(LED_BUILTIN, LOW);

    }

}

but nothing has changed

Please check all connections using a DMM. please post a good picture of the breadboard.

UPDATE:

Considering that you guys told me that the standard MIDI was designed for 5V systems and a Teensy 4.0 works on 3.3V, I understood that there is a sort of less "voltage pressure".

It looks like the 6N138 I have works even worst at 3.3V. So I swapped the 10KR of Pin 7 and 5 with a 220R, and now it switches off fast enough to keep up with the speed of MIDI signal from my Yamaha.

Finally my instrument communicates with my pc. Thank you everyone for the help and @Deva_Rishi for the help with the schematic.

This is the final schematic for my Teensy and setup on the breadboard (I am using an external led to check the MIDI behaviour):