Arduino -> Optocoupler -> Relay problem

Hey,

I'm using an Arduino UNO to drive a relay via an optocoupler, and the relay on the other side of the optocoupler refuses to activate.

I'm connecting the digital pin 4 through a 180 ohm resistor to a TLP521 http://www.toshiba.com/taec/components2/Datasheet_Sync//206/4215.pdf optocoupler. At 5v the 180 ohm resistor should provide 27ma to the TLP521. The TLP521 has a Current Transfer Ratio of 50%, which should provide 13+ma to the relay coil which requires 12ma to operate.

When I activate pin 4, I see 5v at the input to the optocoupler, but the relay coil won't operate. If I bypass the optocoupler, pin 4 operates the relay coil just fine.

Does anyone have any experience with optocouplers and might be able to provide some insight? Perhaps some suggestions for test I can run to get to the bottom of this?

Thanks in advance.
-Mitch

Are you providing any voltage to the relay side of the optocoupler?

At 5v the 180 ohm resistor should provide 27ma to the TLP521. The TLP521 has a Current Transfer Ratio of 50%, which should provide 13+ma to the relay coil which requires 12ma to operate.

I suspect you don't have a correct understanding of what 'current transfer' means. There is no real current transferred from the internal LED to the internal light sensor. You must provide a proper DC voltage source for the relay coil power that the opto's output transistor can then turn on or off.

Lefty

An optocoupler will not pass current from the input to the output. That's how they provide isolation between two circuits.

Note to utilize the isolation you need a separate power supply for the load. See the diagram below. Swap the Resistor on the right for your relay coil (don't forget the flyback diode) and it should work.

Sorry...I neglected to state that I also feed +5v and GND to the coil side of the optocoupler. The +5v is coming straight from the power supply. The power supply is rated at 500ma and my entire circuit is currently consuming ~30ma, so the additional 40ma shouldn't be a problem at all for the power supply.

-Mitch

Can you show us a complete drawing of your setup?

Lefty

Sorry...you're right. A picture is worth a thousand words.

Here is my schematic,

The +5v feeding pin 7 of the TLP521 (through the 180 ohm resistor) comes from digital pin 4 of an Atmega328, and as such should be rated at 40ma. The +5v feeding pin 10 of the TLP521 comes directly from my power supply. The coil on the relay http://www.pickercomponents.com/Data_Sheets/PC458_Data_Sheet.pdf is rated at 208 ohms (which works out to 24ma), but the manufacturer told me it generally operates fine at 12ma (I may add a resistor at some point).

If I remove the TLP521 from the socket, I see +5v at pin 10 and +5v at pin7 (once I activate pin 4 on the Atmega). However, I plug the TLP521 in and the relay just sits there. I have replace the TLP521 with a new one...same thing.

Thanks again for your help.

-Mitch

  1. In calculating the current through the 180 ohm resistor, you haven't allowed for the voltage drop of the IR emitter in the optocoupler. The datasheet gives it as 1.15v typical at 10mA. It will be a little more at 27mA.

  2. You haven't allowed for the saturation voltage of the optocoupler. Below Vce around 1v, the current transfer ration will be lower. So you'll need to provide around 6v or a little more to the relay + optocoupler combo for it to work.

Why are you using an optocoupler, when the relay provides isolation already?

You have wired the output transistor wrong, it needs to be in a 'low side' npn switching configuration. Ground the emitter pin 9, wire pin 10 to C1 on the relay, wire C2 on relay to +5vdc. That should work.

Lefty

retrolefty:
You have wired the output transistor wrong, it needs to be in a 'low side' npn switching configuration. Ground the emitter pin 9, wire pin 10 to C1 on the relay, wire C2 on relay to +5vdc. That should work.

I'm sure it won't make any difference whether it is wired as a low- or high-side switch. It's not the same as a regular transistor whose base is driven from an Arduino pin, it's a phototransistor and the base is not connected.

I would suggest you try my method anyway, just to humor me if nothing else. Then if it works we can talk all day on why. If it doesn't then I guess optos just don't work in your part of the world, a nearby black hole influence perhaps?
Wait you live near me and my optos work OK!

Thank you guys so much for responding.

Retrolefty, your suggestion will require a board modification, which is...painful since I'm building these by hand...on weekends. If you are confident that this is the problem, then I will spin a board, no problem. I'm going to see if I can cut some traces and add some wires to make your recommended changes to my current board tonight to confirm that solves the problem.

Dc42, your suggestions certainly do sound well informed, but more difficult to take action on. Are you suggesting that dropping the 180 ohm resistor value to accomodate the voltage drop across the IR emitter in the optocoupler is all that is necessary on that side of the optocoupler? As for increasing the voltage to the coil, that would be a big problem. I'd have to redesign my power supply significantly to get the 6v you recommend. Also, how does this account for the fact that the 5v out of my power supply operates the relay just fine? Are you asserting that the addition of any switching electronics (NPN, etc.) would introduce a voltage drop that would render my +5v insufficient?

Being a software engineer by profession, this EE stuff is quite new (and confusing) to me, so I really appreciate your help and patience.

Thanks guys.
-Mitch

I also am curious as to what the opto is buying you here, if I read correctly and GND and +5V for the relay are coming from the system. Might as well use a good old NPN configured as an open collector driver for the relay.

Optoisolators are fine, but they have some significant limitations, usually speed and Ic/Vce ratings. I wouldn't expect one to drive a relay other than maybe a reed relay. CTR can be less than unity.

A book that would probably help: Horowitz and Hill "The Art of Electronics". It covers all these topics in detail.
An EXCELLENT reference!

I'm afraid there is something else going on here. I pulled the TLP521 out of its socket and decided to measure the current being provided by digital pin 4 on the Atmega328. Given +5v through a 180 ohm resistor I expected to see 27+ma, but I only see 13ma. Forget the optocoupler and the relay, setting pin 4 high (+5v) and feeding across a 180 ohm resistor to GND only draws 13ma? That seems to violate Ohm's Law, and I believe thats why my relay doesn't fire.

I have double checked that the total resistance of the circuit (pin 4, through the resistor, all the way to GND) is 178.8 ohms. I have confirmed that I have set pin 4 to OUTPUT in setup(). I have tried two separate Atmega328 chips running the same program (one brand new, and the other is older), both stop at ~13ma (the second is slightly higher...14ma). I am running the Atmega328 in my own board (i.e. not an Arduino). Could I have an assembly/solder problem? I eliminated my power supply completely by powering my board entirely from an off-board Arduino I wired in. Still 13ma on pin 4.

Any ideas why pin 4 is only giving me 13ma?

Sorry for the earlier misdirection.

-Mitch

Did you subtract the forward voltage of the optos LED when calculating current? Also check the data sheet, the io pin may 'droop' some at that current level.

There is definitely something wrong there. The data sheet guarantees Voh to be at least 4.2v with a 5v supply and 20mA load, meaning that a 210 ohm resistor as load will draw at least 20mA. I think either your supply is lower than 5v, or you have a bad connection, or the meter you are using to measure current has significant resistance, or your meter is faulty.

That should be the case, however it sounds as though something else is wrong if you are only measuring 13mA with the optoisolator replaced with a meter.

You can get a low voltage drop across an NPN transistor if you choose one that has a low saturation voltage at the current concerned and you overdrive it, i.e. give it much more base current than its hfe suggests is needed for the collector current it has to pass. The transistor in an optoisolator isn't designed or low Vcs(sat) at moderate current, neither is it practical to overdrive it sufficiently with light from the emitter. However, you could use an NPN transistor to amplify the current from the optoisolator. This would also allow a much smaller drive current to be used for the IR emitter.
[/quote]

My power supply looks good to me. I'm seeing 4.98v input to the Atmega. FWIW, the Atmega otherwise works great. I'm driving a DS1307 Real Time Clock, an RN42 Bluetooth chip and a 4x20 LCD (which draws 120mA all by itself) and with all that going on the power supply runs steady at 4.98v. My LCD shows the clock ticking away and the system runs for days without any issues. I can connect via bluetooth from my cell phone and run commands to set/read the clock, turn on/off pins, etc. with no problems.

...I just can't activate my relays. :frowning:

I've seen mixed responses claiming that the 13mA is normal (see http://arduino.cc/forum/index.php/topic,90747.0.html), but you agree with me that it looks like there must be a problem. I'd rather not redesign my circuit until I know for sure that the 13mA is normal and I don't have an assembly problem. Tonight I'll try my test on a stock UNO and see how much current I read there.

Thanks for your response.

-Mitch

With a 180 ohm load on pin 4 I get 23.4mA. This is in line with the datasheet, which in effect guarantees 20mA with a 210 ohm load and 5v supply.

I was afraid of that (and sort of what I expected).

I must have a problem with my board. I just have no idea what to look for. I've already re-soldered all my connections to eliminate cold solder joints as a root cause.

I did observe that without power, and the power supply completely isolated, 250k ohms VCC to GND. Could that be a problem?

-Mitch

That's normal.

If you connect your 180 ohm resistor and meter in series between +5v and ground, what does the meter read then?