Teensy 4.0 serial optocoupler 6n137s

Status
Not open for further replies.

ghostintranslation

Well-known member
Hi,

In order to protect the serial Rx pin 0 on Teensy 4.0, I've made a pcb with a 6n137s like that:
6n137s.jpg
But it's not working.

My goal is to be able to connect another teensy serial Tx but also any midi din cable that's why i need the optocoupler.
At the moment I'm trying with another Teensy to this Teensy.

I tried removing D1 which is there to protect from reverse voltages but even without it doesn't work.
I think this is coming from R7 that drops the current too much but Im not sure... since it's done on a pcb with super tiny format (I did it with JLCPCB) I can't easily remove and replace the resistor, but even if I shortcut that resistor it doesn't work actually...

And I can pinpoint the issue being on the optcoupler because if I take the incoming serial and wire it to the out of the optocoupler (pin 6) it makes it to Teensy and it's readable.

I tried also wiring a LED just to see if it lits to try to debug this. If I connect the led between the 2 pins of the incoming jack connector, it lits. If I connect between the jack connector and the left side of the resistor it lits. Now if I connect it between the jack connector and the right side of the resistor it doesn't. Which makes me think it's because the current is to low then?

Then I measured with a multimeter. I get 1.8V and 15ma between pin 2 and 3 of the optocoupler, and if I refer to the datasheet, https://media.digikey.com/pdf/Data Sheets/Lite-On PDFs/6N137 Series.pdf , I need 1.45V / 1.7V and 25ma, so that tends to confirm my issue?

So then why doesn't it work when I shortcut R7?

Is it possible to get the Serial of a Teensy through an optocoupler at all? Will I be able to make the otpocoupler work for both the Serial of a Teensy and a MIDI DIN connector?

I'm not an expert in electronics, I'm better at code, so I'd appreciate any help.

Thank you
 
The Teensy 4 chip isn't rated for more than a few mA per pin, I think you are just overloading it.

However I've had a look at the Vishay datasheet for the 6N137 - you should be feeding it between 5mA and
15mA absolute max, so I'd aim for 6mA or so which shouldn't be too large for the T4. Have you tried
tying the enable pin to the supply for the output side? Is there the 100nF decoupling on the device as
shown in the datasheet test circuits?
 
Last edited:
What colour LED? If its Vf is much greater than the Vf of the 6N137 then it won’t light when it’s connected to the right side of R7, because it doesn’t have a high enough voltage. A red LED is your best bet, but even so might be very dim...

+1 for tying EN high, just in case.

Have you tried a simple loop reading the pin and outputting its state on the Teensy’s built in LED?

This should definitely work for MIDI, I’ve done it with a 6N137 on Arduino and a different coupler on T4.1

Cheers

Jonathan
 
The Teensy 4 chip isn't rated for more than a few mA per pin, I think you are just overloading it.

However I've had a look at the Vishay datasheet for the 6N137 - you should be feeding it between 5mA and
15mA absolute max, so I'd aim for 6mA or so which shouldn't be too large for the T4. Have you tried
tying the enable pin to the supply for the output side? Is there the 100nF decoupling on the device as
shown in the datasheet test circuits?

Can you share the link of this datasheet? Doesn't sound like the same I'm looking at.
I don't have the enable pin on the supply nor the 100nf cap. I can try that.

What colour LED? If its Vf is much greater than the Vf of the 6N137 then it won’t light when it’s connected to the right side of R7, because it doesn’t have a high enough voltage. A red LED is your best bet, but even so might be very dim...

+1 for tying EN high, just in case.

Have you tried a simple loop reading the pin and outputting its state on the Teensy’s built in LED?

This should definitely work for MIDI, I’ve done it with a 6N137 on Arduino and a different coupler on T4.1

Cheers

Jonathan

Ok I see what you mean about the LED, my test was not a good one most likely, even though I used a red one.

I will test the EN pin.

The code I have is just reading MIDI with the MIDI library. if I take the wire coming from Tx of the other Teensy and connect it to the Rx pin of this one it works well, so the issue is with the optocoupler.


Thank you for the ideas I will test the EN and capacitor.
 
So I have tried to connect the EN pin to 5v and add a 100nf cap between gnd and vcc but it didnt change anything.
So then I did a simple program just to read the pin instead of trying to get MIDI as suggested:

Code:
void setup() {
  pinMode(0, INPUT);
}

void loop() {
  Serial.println(digitalRead(0));
}

So with this code, I can see that the optocoupler is working, I read 1 by default and when I power pin 2 and pin 3 of the optocoupler then I read 0.

But now with this program to read MIDI:

Code:
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE(); // MIDI library init

void setup() {
   MIDI.setHandleNoteOn(midiNoteOnCallback);
   MIDI.begin();
}

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

void midiNoteOnCallback(byte channel, byte note, byte velocity){
  Serial.print(channel);
  Serial.print(" ");
  Serial.print(note);
  Serial.print(" ");
  Serial.println(velocity);
}

Now with this program to read MIDI it doesn't read anything. But if I take the Tx wire of the other Teensy and plug it directly to Rx on this Teensy then it works. So the transmission seems correct, but it seems not to pass in the optocoupler. All I can think of is that it doesn't support the baud rate? Or else, is the pull up correct with this optocoupler or is it that I actually need a pull down?

Otherwise I don't see where is the issue...
 
Nearly there! (1) get the Tx Teensy to toggle its output at 1Hz and run your first program to show what’s seen. If nothing then (2) check your wiring, especially the polarity... Assuming you get that working, (3) run MIDI on your Tx Teensy, and check the Rx one still sees some changes. You might want to print only if you see the pin at 0 (active). Finally (4) use your MIDI code, but specify Serial1 rather than hoping the library picks the right port.

2k is fine, and should be a pull-up.

Cheers

Jonathan
 
...actually, 2k could be a bit high, 1k or 470R might be better. And (3b) would be to open Serial1 at 31250 baud and print out what you receive, before diving into the MIDI library

Cheers

Jonathan
 
I'm using the 6N137 on a Teensy's output. The opto inverts the signal - when the internal LED lights, pin 6 goes to 0. You can fix this by connecting +V to pin 2 and the signal on pin 3 with an appropriate dropping resistor. I use 330R for about 6mA.

If you are feeding pin 6 to a teensy, it will be close to +5V when high. Not a good thing.

The datasheet gives it's Vcc min as 4.5V. In order to safely interface the 6N137's output to a teensy, you will need some sort of translation. You could use a divider. Perhaps a better way is to use an NPN transistor with the collector pulled up to 3.3V. This will also give you a second inversion. The diagram below shows my circuit. The difference is you would have R41 pull up to 3.3V (not isolated 5V as in mine). I would probably make it 10K. C2 is a bypass cap, the datasheet recommends it. Put it as close to pins 8 and 5 as you can. This is from a board I am about to start shipping so I am using/testing this circuit regularly.
6n137.png
 
I'm using the 6N137 on a Teensy's output. The opto inverts the signal - when the internal LED lights, pin 6 goes to 0. You can fix this by connecting +V to pin 2 and the signal on pin 3 with an appropriate dropping resistor. I use 330R for about 6mA.

If you are feeding pin 6 to a teensy, it will be close to +5V when high. Not a good thing.

The datasheet gives it's Vcc min as 4.5V. In order to safely interface the 6N137's output to a teensy, you will need some sort of translation. You could use a divider. Perhaps a better way is to use an NPN transistor with the collector pulled up to 3.3V. This will also give you a second inversion. The diagram below shows my circuit. The difference is you would have R41 pull up to 3.3V (not isolated 5V as in mine). I would probably make it 10K. C2 is a bypass cap, the datasheet recommends it. Put it as close to pins 8 and 5 as you can. This is from a board I am about to start shipping so I am using/testing this circuit regularly.
View attachment 24409

So you said the signal was inverted and I suspected that because of the readings I was getting and I thought this is probably different than with the 6N138 example I saw on the website... And so instead of trying to fix that with hardware I looked up if we can invert the serial logic, and we can...

So it works now!

here is the solution:
Code:
#include <MIDI.h>
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); // MIDI library init

void setup() {
  MIDI.setHandleNoteOn(midiNoteOnCallback);
  MIDI.begin();
  Serial1.begin(31250,SERIAL_8N1_RXINV);
}

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

void midiNoteOnCallback(byte channel, byte note, byte velocity){
  Serial.print(channel);
  Serial.print(" ");
  Serial.print(note);
  Serial.print(" ");
  Serial.println(velocity);
}

So I didn't change anything to my schematics from my 1st post, I only had to invert the serial's logic to be able to use the 6N137S without extra components.

Thank you all for the help, and thank you PhilB for the hint!
 
Excellent!

For the benefit of future readers, the 6N137’s pin 6 is open drain, so if you do as ghostintranslation has done and pull it up to the Teensy’s 3.3V rail there’s no problem with excessive voltage. And EN is active high.

Neat trick with re-opening Serial1 in inverting mode, must remember that. You could also re-wire the Tx Teensy to drive pin 3, with pin 2 connected to power (not ground).
 
Hi, I am trying to design a teensy to dmx driver board with ideally 8 DMX outputs.
Perhaps some of you have experience with this process and know how much current a small (8 LED pars/universe) DMX universe draws, so how much current draw to expect max per MAX485(5V device) RS485-transceiver?
Thanks.
 
Hi, I am trying to design a teensy to dmx driver board with ideally 8 DMX outputs.
Perhaps some of you have experience with this process and know how much current a small (8 LED pars/universe) DMX universe draws, so how much current draw to expect max per MAX485(5V device) RS485-transceiver?
Thanks.

I can't help you with that. You should open a thread of your own to get proper help.
 
Status
Not open for further replies.
Back
Top