pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Opto-isolator directly driven by GPIO port?

Fri Sep 14, 2012 8:54 pm

Does anyone know of an opto-isolator that works directly from the GPIO pins without further circuitry to drive the internal led? The reason I ask is that I had to use a transistor connected to the 5V line to drive the optos on an 8 channel relay board that I picked up because the 3.3v CMOS output of the GPIO pins didn't have enough power.

That was a circuit to control DC voltages and now I need to make a different circuit to control AC voltages and I would like to put the following in series: raspberry pi GPIO pin > opto-isolator > ULN2803 > mechanical relay. And yes, I understand that I don't really need the opto, but I prefer it for safety's sake and complete circuit isolation.

The manufacturer and part number of an opto-isolator that someone can verify is working would be great. A working circuit with all four parts would be even better, but that I could figure out.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

Blars
Posts: 88
Joined: Sun Aug 28, 2011 3:22 am
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Fri Sep 14, 2012 9:11 pm

You should use a current limiting resistor. If your opto needs 1ma and drops 0.6 volts,
(3.3-0.6)/0.001 = 2.7k ohms

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Fri Sep 14, 2012 9:32 pm

Blars wrote:You should use a current limiting resistor. If your opto needs 1ma and drops 0.6 volts,
(3.3-0.6)/0.001 = 2.7k ohms
Yes, I understand that. What I meant by no extra circuitry was not having to use external voltage sources and transistors in order to get the opto's led to light up sufficiently or at all; I would like for the 3.3v CMOS output of the GPIO pin to drive the input of the opto directly.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12354
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Opto-isolator directly driven by GPIO port?

Fri Sep 14, 2012 10:30 pm

should not be a problem, as long as you limit the current with a resistor. Most opto-couplers use a red LED internally that requires about 5mA, and a red LED has a diode drop of about 2,0 Volt, that means that when there is 3,3 Volt available to light the LED, you need a resistor that has 3,3 - 2,0 = 1,3 volt over it, and it should limit the current to 5 mA. With the formula R=V/I (or R=U/I for people not living in an Anglo-Saxon controlled school environment) you can calculate the resistance, as 1.3/0.005 (1.3 Volt, and 5mA) = 270 Ohm (which is a nicely available resistor value).

So you just tie a 270 Ohm resistor in series with the LED, and you can turn the LED on safely with a 3,3Volt source.

But how exactly can you control the resistor+opt-controller (LED+R) with a GPIO pin, there are two possibilities, depending on whether you want the LED to light when you send a "High", or when you send a "low" (a "1" or a "0"). Most often it would be that a "1" would turn on the LED, so in that case you connect LED+R between a GPIO and GND, (with the kathode of the LED connected to GND). A high will send a current (limited by the resistor) through the LED to GND.

The alternative would be to connect the Anode of the led to 3V3 and the resistor to the GPIO pin, driving the GPIO pin low will send a current through the LED.

The first method is known as "sourcing the current", the second as "sinking the current".
Both work equally well on a PI.

One of the cheapest suitable Photo-couplers (at about 50 eurocent typical) would be the SFH618A-2X.

User avatar
Bencom
Posts: 56
Joined: Wed Mar 07, 2012 10:40 pm
Location: New Zealand
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Fri Sep 14, 2012 11:10 pm

I have posted this link several times.
http://arduino-direct.com/sunshop/index ... tail&p=181
This is the relay I use on my RPi.
Switching the port to output turns the relay on, switch back to input and it is off.
Could not be simpler than that. The user space program gpio, part of the wiringPi package, allows user access. Up to you whether you write a script or a program to use the ports. I wrote a C daemon.
Search my other posts for more info.

User avatar
Grumpy Mike
Posts: 914
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Sat Sep 15, 2012 8:55 am

Bencom wrote:I have posted this link several times.
http://arduino-direct.com/sunshop/index ... tail&p=181
This is the relay I use on my RPi.
Switching the port to output turns the relay on, switch back to input and it is off.
Could not be simpler than that.
Not the best idea in the world. There are two things wrong with that:-
1) When the relay is off the base is effectively floating. This means it is much more susceptible to interference causing the relay to false trigger.
2) When the relay is off you are subjecting the Pi's input pin to +5V through the reverse bias leakage of the base diode in the transistor. This will damage the Pi's GPIO pins and after time might make them not work at all.

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Sat Sep 15, 2012 2:24 pm

mahjongg wrote:Most opto-couplers use a red LED internally that requires about 5mA. One of the cheapest suitable Photo-couplers (at about 50 eurocent typical) would be the SFH618A-2X.
Thanks. That is what I was looking for, and more importantly it explains why my Sainsmart relay board does not work from the GPIO ports without an external driver circuit consisting of a transistor with a 5V supply voltage, even though the specs say it can be driven with a supply voltage of 3.3 Volts. The transistor is needed for both voltages for a different reason; the input to the opto is active low. Actually it is on the hairy edge of working at 3.3V with some relays working and others not, and when it does work, the relay click is very faint (At 5V I get a loud definitive click every time). I see a couple of others are having the exact same problem. The relay card circuit board has a 1K resistor in series with not only the opto-coupler LED, but an external indicator LED as well.

Maybe I am reading the specs wrong that it would run at 3.3V, but sounds like a design flaw to me. It is a moot point anyway because at least I have it working. There was no documentation with the board, or available from the manufacturer, but I was able to dig up the schematic and specs on the internet through various sources and have attached the pdf files. This forum does not allow uploads, so I have stored them online (There is a time limit of 30 days, so when they are gone, they are gone).

Relay Card Board
http://www.amazon.com/SainSmart-8-Chann ... =sainsmart

Relay Card Schematic
http://drop.st/yQbM8J

Relay Card Specifications
http://drop.st/M978gk
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

User avatar
Bencom
Posts: 56
Joined: Wed Mar 07, 2012 10:40 pm
Location: New Zealand
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Sat Sep 15, 2012 10:18 pm

Back to the drawing board I guess. Had no idea leakage voltage reaches the level it does (4.2v off 3.8v on). Will see what I can do with the 6N137 opto's and a drive transistor I have coupled with the relay modules.

Thanks for the tips.

pygmy_giant
Posts: 1562
Joined: Sun Mar 04, 2012 12:49 am

Re: Opto-isolator directly driven by GPIO port?

Sat Sep 15, 2012 10:24 pm

why not try this: http://www.ebay.co.uk/itm/5-pcs-PS2501- ... 4cfef23cc2 data sheet: http://html.alldatasheet.com/html-pdf/1 ... 501-1.html via 220 ohmish resistor ?
Ostendo ignarus addo scientia.

kaos
Posts: 108
Joined: Mon Mar 26, 2012 8:14 pm

Re: Opto-isolator directly driven by GPIO port?

Sun Sep 16, 2012 8:52 pm

Grumpy Mike wrote:
Bencom wrote:I have posted this link several times.
http://arduino-direct.com/sunshop/index ... tail&p=181
This is the relay I use on my RPi.
Switching the port to output turns the relay on, switch back to input and it is off.
Could not be simpler than that.
Not the best idea in the world. There are two things wrong with that:-
1) When the relay is off the base is effectively floating. This means it is much more susceptible to interference causing the relay to false trigger.
2) When the relay is off you are subjecting the Pi's input pin to +5V through the reverse bias leakage of the base diode in the transistor. This will damage the Pi's GPIO pins and after time might make them not work at all.
Hmm. Interference capable of making bipolar transistor with a floating base go into conduction? Enough conduction to make a relay close? If it was a question of several tens of meters of wiring in electrically "noisy" environment, maybe, but I can't see it happening under most circumstances. Of course, we don't know whether the transistor bipolar or FET. In the latter case it might be more susceptible to interference.
As for the reverse leakage, that is very small and I would be very surprised to learn that the SOC's built in protection could not handle it.
Of course both potential problems can be circumvented by using a slightly different method of operating the relay: Instead of switching the GPIO between input and output, keep it in output mode but drive it high to switch on the relay, or low to switch it off.

--
Best regards,
Kári.

User avatar
Bencom
Posts: 56
Joined: Wed Mar 07, 2012 10:40 pm
Location: New Zealand
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Sun Sep 16, 2012 11:03 pm

The RPI has been exposed to this leakage current for a few weeks now (24/7).

Test measurements. I did a bit of testing before connecting but not real extensive.

5.8mA idle current. (led)
76.2 - idle = 70.4mA relay current
All relays on ~280mA

Leakage voltage and current measured for both 5v and 3.3v
Voltage - Leakage Voltage
5v (4.79) - 4.28v
3.3v - 2.71v
Voltage - Leakage Current. Board ground connected.
5v - 3.9mA
3.3 - 2.42mA
Voltage - Leakage Current. Board ground not connected.
5v - 4.01mA
3.3v - 2.48mA

I also tested the relay at 3.3v and within limits (50mA from the 3.3v gpio connecter) just activating one relay. One relay draws ~50mA, whether more than two or more relays will switch is unknown. I need to organize an addition power source ($3 2A dc-dc converter) to test this. This will allow the relay board voltage and consequently the leakage voltage to be adjusted. My project requires 24v 6A where the RPi is remotely situated (gateway) therefore I require more than one dc-dc converter anyway (5v RPi, 12v LED lighting).

Nonetheless I am also going to grab an opto coupled relay board ($17NZD) and some new 2n2222's.

As for noise. Not really an issue in the environment the RPi is housed in. The relays are to be in the same box as the RPi. Although low levels of RF are there, bypassing will be used on inputs and unless problems are detected, outputs need little protection. The main concern is to treat the RPi nice.

kaos
Posts: 108
Joined: Mon Mar 26, 2012 8:14 pm

Re: Opto-isolator directly driven by GPIO port?

Sun Sep 16, 2012 11:34 pm

Actually, I find those leakage current figures surprisingly high. I would have expected something in the micro- or possibly even nano ampere range. How are you doing the test? Positive voltage to Vcc, negative voltage to Gnd, ammeter + to input and ammeter - to gnd?

--
Best regards,
Kári.

User avatar
Bencom
Posts: 56
Joined: Wed Mar 07, 2012 10:40 pm
Location: New Zealand
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Sun Sep 16, 2012 11:53 pm

Yes. Plus I tested with the board gnd disconnected. I probably need to make a break out board to test much more.

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: Opto-isolator directly driven by GPIO port?

Mon Sep 17, 2012 12:04 am

kaos wrote:Of course both potential problems can be circumvented by using a slightly different method of operating the relay: Instead of switching the GPIO between input and output, keep it in output mode but drive it high to switch on the relay, or low to switch it off.
It is a PNP BJT connected as a high-side switch. So the base must be driven low to switch on, or high to switch off. But the implication is that the transistor's emitter is connected to 5V, which means it stays on even when the Pi drives it up to 3.3V. Hence the need to abuse the GPIO's high-impedance input state, and allow the base to float to an out-of-spec voltage.

We do not have enough information to say statistically what the long-term effects are of exposing the GPIOs to ~4.4V. But it is certainly not doing any good.

User avatar
Bencom
Posts: 56
Joined: Wed Mar 07, 2012 10:40 pm
Location: New Zealand
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Mon Sep 17, 2012 12:28 am

jojopi wrote:
kaos wrote:Of course both potential problems can be circumvented by using a slightly different method of operating the relay: Instead of switching the GPIO between input and output, keep it in output mode but drive it high to switch on the relay, or low to switch it off.
It is a PNP BJT connected as a high-side switch. So the base must be driven low to switch on, or high to switch off. But the implication is that the transistor's emitter is connected to 5V, which means it stays on even when the Pi drives it up to 3.3V. Hence the need to abuse the GPIO's high-impedance input state, and allow the base to float to an out-of-spec voltage.

We do not have enough information to say statistically what the long-term effects are of exposing the GPIOs to ~4.4V. But it is certainly not doing any good.
Until recently I did not have the circuit diagram. The problem being a high side switch. I want to keep component count down and saw the relay modules as ideal. I built two relays before I got my RPi's but did not try them after trying the YwRobot relay. I can not build relay modules for the price I can get them from China.

Anyway thanks for the explanation.

User avatar
Bencom
Posts: 56
Joined: Wed Mar 07, 2012 10:40 pm
Location: New Zealand
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Mon Sep 17, 2012 3:33 am

Two units I built. The over kill one is probably more suited to other high current uses rather than just driving a relay. MC34152 Input threshold voltage max 2.6v Typ 1.58v - 1.75v Iih 200uA Iil 20uA.
http://www.zlham.geek.nz/images/news/relays-cct.jpgImage
http://www.zlham.geek.nz/images/news/relays-brd.jpgImage

kaos
Posts: 108
Joined: Mon Mar 26, 2012 8:14 pm

Re: Opto-isolator directly driven by GPIO port?

Mon Sep 17, 2012 9:22 am

jojopi wrote:It is a PNP BJT connected as a high-side switch.
Ah, mea culpa, I should have thought of that possibility. :oops: That, of course, explains the current figures; it is not reverse leakage current over the C-E junction, but the base current of the transistor in operation. I agree that this kind of setup might be less than healthy for the Raspi in the long run.
Bencom wrote:I can not build relay modules for the price I can get them from China.
Maybe so, but they still need not be expensive. I may be telling you something that you know already, but for a similar module, minus LED indication, but with a Raspi safe low side switch, all you need in the way of components is the relay itself, a diode, a small signal NPN transistor, and a resistor. The relay can be any voltage (within reason; don't try mains voltage) that you have handy, and is connected with one side of the coil to + supply of that voltage. The diode can be any normal silicon rectifier, 1N4004 for example, and is connected in parallel with the relay coil, with cathode towards +. For the transistor, BC547 or 2N2222 come to mind, but there are many types that will work. It has it's collector connected to the other side of the relay coil / diode anode, and emitter to - supply voltage, which is also connected to Raspi ground. Base is connected to one end of the resistor, with the other end connected to a Raspi GPIO pin. A 1 kOhm resistor should work for most small relays and small signal transistors with reasonable hFE.

--
Best regards,
Kári.

kaos
Posts: 108
Joined: Mon Mar 26, 2012 8:14 pm

Re: Opto-isolator directly driven by GPIO port?

Mon Sep 17, 2012 9:57 am

kaos wrote:it is not reverse leakage current over the C-E junction, but the base current of the transistor in operation
Edit: That, of course, should have been the C-B junction.

User avatar
Bencom
Posts: 56
Joined: Wed Mar 07, 2012 10:40 pm
Location: New Zealand
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Tue Sep 18, 2012 12:02 am

1 x 4 Relay module $8.50USD shipping built in. Equals $10.28 NZD cheaper if ordering more.
Available in 5, 12 and 24v plus 30cm cable. The link below has the circuit and is low side switched.

http://www.aliexpress.com/item/5V-12V-o ... 69677.html

I have used this broker site before. Many recognize sample quantities are required to sell.
The module I have (YwRobot) and another on the way (Opto) sell close to twice the price taking into account internal shipping costs, on the local auction site trademe. NZ GST law allows $300 to be imported free of tax and is the main barrier for small production runs. But I view the RPi as being the first Linux board truly suited to modular construction and fits so well with the other boards I have it is not funny. I have several STM32V ARM Cortex LCD touch screen modules, a MC9S08 and old HC11 development board (Actually a small collection of old boards from all sorts). So two SPI CE equals lots of facility meaning most other pins can be used for simplistic things.

But for the time being, making a gate open with vid/aud from gate and aud to the gate is the proof of concept project.

I like playing with technology and high power hf rf. :D
http://www.zlham.geek.nz.

User avatar
Grumpy Mike
Posts: 914
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Tue Sep 18, 2012 5:08 pm

Bencom wrote:As for noise. Not really an issue in the environment the RPi is housed in. The relays are to be in the same box as the RPi. Although low levels of RF are there, bypassing will be used on inputs and unless problems are detected, outputs need little protection. The main concern is to treat the RPi nice.
Noise is always an issue whether it is in a domestic environment or not. There is plenty of hostile equipment from the thermostat on a domestic iron, to the taxi parked outside your door. I have see cases where even properly biased transistors are interfered with.

rjcollingridge
Posts: 10
Joined: Wed May 23, 2012 8:15 am

Re: Opto-isolator directly driven by GPIO port?

Mon Jan 14, 2013 10:26 am

I'm using ILQ74 opto-isolators. These have a forward voltage of 1.3V typically, so can be connected to GPIO pins using a resistor to limit the current. Also use these to protect the GPIO pins when used as inputs as I'm using 12V signalling (elimiates interference and false switching) in my smart home automation stuff.

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Opto-isolator directly driven by GPIO port?

Mon Jan 14, 2013 1:08 pm

So I see my original thread is still going. I found a Toshiba TLP624-4(F) many months ago. It only requires 1.6 mA recommended typical forward current, and has a 1.15 volt forward voltage @ If = 10mA. I have a 1k ohm resistor between the GPIO output and the opto LED, so I am running the LED at a measly 2 ma (measured). The opto transistor output feeds the input of a ULN2803A Darlington Pair (The chips internal 2.7K ohm resistor limits current) whose output operates a relay with a 5Vdc 185 mA coil which has 30 Amp 250Vac rated contacts. I use the circuit to turn on/off higher current AC devices with my pi.

You can change the status of this thread to SOLVED.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

Return to “Interfacing (DSI, CSI, I2C, etc.)”