Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 14 of 14

Thread: MIDI serial (DIN) to usbMIDI issues: got weird MIDI messages

  1. #1

    MIDI serial (DIN) to usbMIDI issues: got weird MIDI messages

    The context:
    In my MIDI Drawbar controller based on Teensy LC I use both usbMIDI and serial MIDI (i.e MIDI IN and OUT with DIN plugs).
    The whole thing is configured so it can accept MIDI messages from both the IN (usb and serial), merges them with those generated by itself as a controller, then output them to both OUTs (usb and serial).




    The issue:
    While serial MIDI IN to serial MIDI out doesn't have any problem, when I try serial IN to usbOUT i get any sort of random MIDI messages and values.

    I really can't understand what's wrong with the thru/merge implementation: I more or less derived it from the "Interface_3x3" example (obviously reduced to 1x1).

    Here is a stripped down script that only merges and forward messages from serial IN to usb and that can be used to reproduce the problem, just open a midi monitor that reads the usbMIDI and try some note on note off or CC...

    Code:
    #include <MIDI.h>
    
    // Create the Serial MIDI ports
    MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI1);
    
    // A variable to know how long the LED has been turned on
    elapsedMillis ledOnMillis;
    
    void setup() {
      Serial.begin(115200);
      pinMode(13, OUTPUT); // LED pin
      digitalWrite(13, LOW);
      MIDI1.begin(MIDI_CHANNEL_OMNI);
    }
    
    void loop() {
      bool activity = false;
    
      if (MIDI1.read()) {
        // get a MIDI IN1 (Serial) message
        byte type = MIDI1.getType();
        byte channel = MIDI1.getChannel();
        byte data1 = MIDI1.getData1();
        byte data2 = MIDI1.getData2();
    
        Serial.println(type, HEX);
        Serial.println(channel, HEX);
        Serial.println(data1, HEX);
        Serial.println(data2, HEX);
        Serial.println("---------------");
    
        // forward the message to USB MIDI virtual cable #0
        if (type != midi::SystemExclusive) {
          // Normal messages, simply give the data to the usbMIDI.send()
          usbMIDI.send(type, data1, data2, channel, 0);
        } else {
          // SysEx messages are special.  The message length is given in data1 & data2
          unsigned int SysExLength = data1 + data2 * 256;
          usbMIDI.sendSysEx(SysExLength, MIDI1.getSysExArray(), true, 0);
        }
        activity = true;
      }
    
      // blink the LED when any activity has happened
      if (activity) {
        digitalWriteFast(13, HIGH); // LED on
        ledOnMillis = 0;
      }
      if (ledOnMillis > 15) {
        digitalWriteFast(13, LOW);  // LED off
      }
    
    }

    I'm really confused because, looking at the Arduino's Serial monitor output, looks like the serial MIDI IN Type is just wrong before being used by usbMIDI, but on the other way it just can't be wrong since it is sent right to the serial MIDI OUT (this thru part is handled directly by the MIDI library)...
    Note that if I just output data generated by the controller to usbMIDI I've got no errors at all.


    Technical info:
    • Teensy LC
    • Arduino ver 1.8.7 on Windows 10
    • Teensyduino ver 1.44


    Libraries used in my sketch: (don't think they matter since even the super-stripped-down sketch abve shows the problem...)
    • #include <Wire.h>
    • #include <Adafruit_MCP23017.h>
    • #include <Bounce2.h>
    • #include <MIDI.h>
    • #include <ResponsiveAnalogRead.h>



    Any suggestion is appreciated!
    Thank you

    Stefano

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,136
    I can only test this on a Teensy 3.6 but your code works with no problems. The text below shows some Note on/off messages but it also works when I send Control Change messages.

    Code:
     ===> MIDI-OX Version: 7.0.2.372
     ===> Log Opened: Tue 15-Jan-2019 10:09:16 ===>
     TIMESTAMP IN PORT STATUS DATA1 DATA2 CHAN NOTE EVENT               
     000272F1   2  --     90    3C    32    1  C  4 Note On               
     0002738E   2  --     80    3C    00    1  C  4 Note Off              
     00027468   2  --     90    3E    35    1  D  4 Note On               
     000274F5   2  --     80    3E    00    1  D  4 Note Off              
     000275EF   2  --     90    40    37    1  E  4 Note On               
     0002766C   2  --     80    40    00    1  E  4 Note Off              
     00027766   2  --     90    41    2A    1  F  4 Note On               
     000277F2   2  --     80    41    00    1  F  4 Note Off              
     0002791B   2  --     90    43    43    1  G  4 Note On               
     00027989   2  --     80    43    00    1  G  4 Note Off              
     <=== MIDI-OX Log Closed: Tue 15-Jan-2019 10:09:43 <===
    I notice that your code treats any non-sysex MIDI message as if it had three bytes. Note on/off and Control Change are 3-byte messages but Program Change, for example, is two bytes and Active Sense is one byte.

    Pete

  3. #3
    Quote Originally Posted by el_supremo View Post
    I can only test this on a Teensy 3.6 but your code works with no problems. The text below shows some Note on/off messages but it also works when I send Control Change messages.
    Hi Pete, thanks for your feedback!

    Quote Originally Posted by el_supremo View Post
    I notice that your code treats any non-sysex MIDI message as if it had three bytes. Note on/off and Control Change are 3-byte messages but Program Change, for example, is two bytes and Active Sense is one byte.

    Pete
    Hmmm, you are right, but that code is barely a copy-paste from the MIDI_3x3 example and I presumed it works.

    I'm now upgrading to the latest Arduino and Teensyduino, for both I'm on the second-last version.
    I don't think this will change anything, but worth a try.

    Stefano

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,136
    The usbMIDI send function does not differentiate between 2 and 3 byte MIDI messages so that isn't causing a problem.

    Pete

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,309
    looking at the Arduino's Serial monitor output, looks like the serial MIDI IN Type is just wrong before being used by usbMIDI
    Maybe there's a hardware issue?

    Is this the schematic?

    Click image for larger version. 

Name:	drawbar-commander-schematics.jpg 
Views:	24 
Size:	112.9 KB 
ID:	15614

    This input circuit doesn't look right.

    4N35 optocouplers are marginal for MIDI. Normally much faster parts recommended, like 6N138. Even if you do everything perfectly, you can expect at best poor signal quality with this sort of part.

    But this schematic looks far from the right way. In fact, pretty much everything about it on the output side looks completely wrong!

    Normally the emitter connects to ground and the collector is the output. Normally a pullup resistor in the 220 to 1K range is used on the collector. Here's one circuit I found online which looks right (as good as 4N35 can be), but of course you would use 3.3V with Teensy LC. DO NOT CONNECT 5V, since Teensy LC is not 5V tolerant.

    Click image for larger version. 

Name:	MIDI_connections.png 
Views:	46 
Size:	10.3 KB 
ID:	15615

    However, I would not recommend this circuit at all.

    This page has the recommended MIDI circuit to use with Teensy.

    https://www.pjrc.com/teensy/td_libs_MIDI.html



    A few important points...

    6N138 is a much better part to use for MIDI. It's faster, so you get good signal quality at 31250 baud.

    The 6N138 needs 5V power, and a 0.1 uF capacitor.

    But you get a 3.3V output because the 470 ohm resistor connects from pin 6 to 3.3V. Make sure you do not connect that to 5V. The resistor is what determines the signal output voltage, even though the optocoupler has 5V power.

    Using a low value resistor like 470 ohms gives a good fast rise time. You need this for a high quality MIDI signal. The internal pullup resistors inside Teensy are much too weak. Use a real resistor and make sure it's in the 220 to 1K range, so you have a fast rise time on the signal.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,309
    If you do want to try using that 4N35 (since you already have it), connect it similar to that known-good circuit (pin 4 to GND, pin 5 as the output). But replace the 560 ohm resistor with 220 or 330 ohms, and connect it to 3.3V instead of 5V. That will give you a 3.3V output compatible with Teensy LC, and using a lower value resistor will keep the 4N35 near its best current transfer ratio when using the lower voltage. Odds are good it will work, but the signal quality probably won't be as good as 6N138.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,309
    One more quick comment... that schematic from the Hackaday.io site would be correct for a PC900 optocoupler, which used to be the most commonly recommended part for MIDI (until Sharp discontinued it several years ago).

    Maybe this schematic originally meant PC900, but then was changed to 4N35 without testing or considering the 4N35 isn't anything like the old PC900.
    Last edited by PaulStoffregen; 01-15-2019 at 11:44 PM.

  8. #8

    Unhappy

    Hi Paul,
    thank you very much for your deep analysis and suggestions!

    Quote Originally Posted by PaulStoffregen View Post
    Maybe there's a hardware issue?
    Yes, I start thinking that is an hardware issue... especially now that I ha read your posts

    Quote Originally Posted by PaulStoffregen View Post
    Maybe there's a hardware issue?

    Is this the schematic?

    Click image for larger version. 

Name:	drawbar-commander-schematics.jpg 
Views:	24 
Size:	112.9 KB 
ID:	15614

    This input circuit doesn't look right.
    Mmmm, I built the MIDI interface months ago and I can't remember... It could be that I changed something that proved to be wrong during the breadboarding phase but then I haven't updated the schematics.
    I'll open the controller and check the wiring and the components...

    Quote Originally Posted by PaulStoffregen View Post
    One more quick comment... that schematic from the Hackaday.io site would be correct for a PC900 optocoupler, which used to be the most commonly recommended part for MIDI (until Sharp discontinued it several years ago).

    Maybe this schematic originally meant PC900, but then was changed to 4N35 without testing or considering the 4N35 isn't anything like the old PC900.
    Yes, this could be the case. I remember that at the time I had troubles finding the right optocoupler and at last resorted to a "passable" one. It could be that I swapped one for another without much attention.

    I'll open the controller in the next few days and let all you know my findings

    Thank you again for the help

    Stefano

  9. #9
    In the meantime a re-tested the serial MIDI IN to serial MIDI OUT, connecting the OUT to a real MIDI monitor and not only to my musical expander and it proved to send out any sort of garbage too.
    So I can assert that is not a problem with only serial MIDI IN to usbMIDI OUT (as I wrongly said in my first post).

    Looks like it's really an hardware problem at serial MIDI IN

    Thank you again, I'll keep you updated

    Stefano

  10. #10
    Junior Member
    Join Date
    Jan 2019
    Posts
    9
    Jumping in this thread because I was having a similar issue and figured I shouldn't create a separate post. Just a couple of days ago I was trying to get MIDI-in working with the 6n138 and schematic posted below (Teensy 3.2)
    Click image for larger version. 

Name:	td_libs_MIDI_sch_t3.png 
Views:	18 
Size:	4.9 KB 
ID:	15622

    It was outputting very strange/unusable data. Eventually I tried connecting a 1k resistor between pin 7 and GND and like magic it worked great. Does anybody know why this might be?

  11. #11
    Banned
    Join Date
    Jan 2019
    Location
    Australia
    Posts
    16
    The 1k resistor from pin 7 to ground would be what I'd call a Pulldown resistor. When the photo transistor turns off, effectively the base of the second transistor is left floating hence it's actual switch-off behaviour may be determined by it's own peculiarities rather than what we're sending it. eg. The transistor may be a little leaky, noisy or whatever. So the pulldown yanks the base cleanly to ground in the same fashion as a pullup. I always thought it good practice to use a pulldown in this situation and yeah I'd be lookin at 1k here too.

    Have built a number of Midi devices using the 6N138 (without the pin 7 pulldown) and have had no problem with 'em, however an off-the shelf Midi Shield (6N138) occasionally inputs garbage so I guess this prompts me to eat my own words and tack a pulldown on pin 7 and see what happens.

    Hope this is useful

  12. #12
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,309
    I tested 6N138 chips from 2 different manufacturers. Both worked without anything connected to pin 7.

  13. #13
    Banned
    Join Date
    Jan 2019
    Location
    Australia
    Posts
    16
    Hey I know I'm quoting myself here

    however an off-the shelf Midi Shield (6N138) occasionally inputs garbage
    Turns out the culprit was a dodgy 6N138 so duly replaced with a 6N139 and a quick test showed that the device is working correctly again.

    WRT the 1k pulldown on pin 7:-

    Hooked a square wave generator @20 Khz to Midi in and looked at Output pin 6 with my CRO and observed that the rising edge looked more like the first quadrant of a sine wave, the falling edge - as square as one would reasonably expect given that it is pulled low by the output transistor.

    Starting with 4k7 and working downwards found that 1k pin 7 pulldown yielded falling edge and rising edge with a similar degree of squareness.

    Unfortunately my Jurassic CRO does not have a screenshot button so I can't post the image in any case for me the jury is out. From now on all 6N139's I press into service for Midi in are gonna get that 1k.

  14. #14

    [resolved]

    The last weekend I opened the controller and checked the circuit... and as Paul suggested it was wrong...
    I used the 6N139 but connected as if it was a P900 or maybe something else...

    So I removed the wrong paths and components, replaced with the ones suggested in the schematic at https://www.pjrc.com/teensy/td_libs_MIDI.html and now all is working!

    Thank you for the support!

    p.s.
    I updated the schematic both at Hackaday that on Github:
    https://hackaday.io/project/162255-m...bars-commander
    https://github.com/garubi/MIDI-Drawb...ster/schematic

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •