Opto Isolator Help

I need some help picking resistor values with this 4N25 opto isolator chip.

I have a Photon using Software Serial on D2 as the Softserial UART RX pin and I have a 5v device spitting out UART data and I need to isolator between the two devices.

What would be good resistor values to use?

I’m assuming pulling the output to the Photons D2 pin up to 3.3v is the way to go vs pulling up to 5v as in the schematic above.

Hey Ryan! @RWB

I’m not good at resistors but I had a question:
pin 6 would be connected to 3v3 too?

If you using this for serial data, the schematic you shown would invert the serial data on the RX end, and you would get garbled data. I.E. a high on the input would give you a low on the output.

@seulater Same thing with this schematic?

Found that info here: https://github.com/pepaslabs/Electronics/wiki/Designing-a-TTL-Serial-Opto-isolator

What exactly do I need to make this work? It’s all new to me.

No idea :blush: This is new territory for me.

OMG, LTspice! I remember using spice in university. Good times…

Now, in this last circuit seems like there is no inversion of the signal.
Some random thoughts:

  • I would connect V2 to the photon’s 3v3
  • R2=330 would give you ~10milliAmp of current from 3v3, seems reasonably low, according to ohm’s law

Thanks for the feedback.

So if V1 is 5v then R1 = 330 ohms is OK?

Would you recommend a higher or lower value for R2 or is 330 OK?

I have 100, 220, and 470 OHM resistors here so I’m curious if I hade to use a single resistor which way would you lean as far as resistor values go?

let me refresh my memory

I=V/R -> I=5v/330 ohms -> I=15 milliAmps

R1=330 ohms seems ok

I would use 470 ohms all over the place

UPDATE: R1=330ohms seems ok in the case what you are connecting it to can provide 15mA (or 10mA if you use a 470 ohms resistor)

If I remember correctly the source can drive 20ma so either of those resistor values should work.

I’ll let ya know how it goes.

1 Like

@RWB The opto is spec’ed at 10mA and the diode drops about 1.4V (middle of specs), so you could use 190 Ohms on the diode. A more practical value for a digital application is around 5mA, so 330 Ohms will work fine. I would lean towards a lower resistor value to account for aging and CTR variation effects.
The output resistor depends on the speed you are trying to achieve. The BJT can drive 50mA, so at 3.3V the output pull-up can be as low as 60 Ohms. These are the max values, but more appropriate would be 500 Ohms. 470 is fine.

You should check how fast the output needs to toggle. This is not a particularly fast opto with prop delay of 100usec.
There are so many better options and in 4 pin packages and with higher CTR at lower diode currents. All depends on cost, size and availability criteria.

BTW, if you can drive the diode from the cathode instead of the anode, the output will not be inverted.

Thanks for the reply.

I’m trying to run UART data at 19,200 baud through this which I believe is possible based on somebody else testing the capability of the 4N25 chip as shown below.

I’m this web page: https://github.com/pepaslabs/Electronics/wiki/Designing-a-TTL-Serial-Opto-isolator

I’ll try your lower value resistors also.

The 330 ohm as used in the example? Based on the scope pictures it looks like they were working with 5v input and 4v on the output. I’m 5v in and the output is running to the Photon is 5v tolorent on the D2 pin I’m using for Softserial at 19,200baud.

why not just use a purpose built Ic for it :wink:
No need to mess with resistors and what not.
http://www.ti.com/lit/ds/symlink/iso721.pdf

I shouldn’t even need it but I had some issues pop up that I thought this isolation may be able to fix it but after testing level shifters and opti-isolators they are making no difference.

I just picked these up at a local Fry’s because they were quick and easy to get for testing.

I have an oscilloscope out now trying to figure out what exactly is causing problems.

I basically have 2 pieces of equipment one with a 3.3v UART port being read by RX pin on the Photon and another 4.5v UART port that is being read on pin D2 on the Photon using Software Serial.

When the 3.3v device is connected the Photon receives the data always without issues. When the Software Serial device is connected by it’s self it works just fine at 19200 baud.

When I plug in both devices at the same time the device on Software Serial stops receiving data while the 3.3v UART device on the RX port keeps on working.

There is a lot going on with this setup with 24v to 5v converters and other equipment so just trying to figure it all out one by one.

1 Like

In the end the solution was not an optoisolator or level shifters or switching from software serial to Serial2 hardware port.

After a day of trouble shooting what ended up working was turning ON and OFF the Serial1 port every 2.5 seconds. When Seial1 was OFF then Software Serial with RX on Photon D2 worked fine.

Toggling Serial1 ON/OFF allowed software serial to work while Serial1 was OFF.

Has something to do with the 2 serial devices when conncted together because 2 other systems with the exact same everything but 1 different device work just fine without turning Serial1 OFF.

It’s working now and that’s all that matters for now :slight_smile:

At least I got a crash corse in isolators and level shifters :grinning:

@RWB, are you saying that the one specific serial device (with 4.5v output) seems to be causing the problem? What device is it (link if possible)?

The device is a solar charge controller and its UART data on RX was 4.5v on the scope. This was on Softserial but the exact same happened when connected to Serial2.

When I attached a battery monitor to the Serial1 the Softserial stopped receiving data unless I temporarily disabled Serial1. The Softserial worked fine if I discnnected the battery monitor which is what lead me to try toggling Serial1 Off for a short period of time.

https://www.victronenergy.com/upload/products/239_309_20180705165838.png

I use a different charge controller and same battery monitor on some other systems with the same connections and code and it works fine without toggling Serial1.

Hi @RWB

I think you could have a common ground problem when connecting to two systems. The symptoms are like you describe–each serial connection works on its own but together there are problems.

1 Like

Opti isolators should have fixed that I thought.

Turning Serial OFF does not fix a ground loop does it?

I looked at the signals with an oscilloscope from both sources at the same time while connected and they look nice and clean and I didn’t see any noticable changes when I connected or disconnected the devices.

Hi @RWB

If you are careful with the power connections for the optoisolators, then yes they will get you out of ground problems. So in your schematics above, exactly which +5 and which GND you are using is key. If the GND pins on the two sides of the optoisolator are not connected together, that will work OK but your schematic does not indicate that.

Turning off serial makes all the pins be high impedance inputs (with protection diodes) so that can possibly open a ground problem. A no-ground situation can cause inputs to float and work at some times and not others as well.

Your scope also has a hard ground which is typically connected from the probe to earth safety ground. This is normally a good thing for your safety but there are times when you must run a scope without a safety ground on a isolation transformer. I have seen cases where the scope probe ground wire acted like a fuse and melted due to high current.

I am not saying that your problem was absolutely a ground problem; I’m just saying that your strange symptoms do fit that hypothesis. If you don’t know for sure how the systems are grounded, you could poke around a bit with your scope and likely find out. Try connecting the scope probe tip and only the tip to one system’s GND pin and see if there is DC there. Look at the other pins relative to earth ground too. Then try the other system.

3 Likes