help connecting Arduino UNO to n-ch MOSFET via Optocoupler

If it has emitter and collector, it is not a MOSFET, it's a BJT (a k a "70's transistor.")

BJTs let current through between collector and emitter when there is also a current (the right way) through the base. This means that there will always be current flowing through the base in addition to the switched current when it is on. This make it easy to switch them on and off with a single switch -- current flows, transistor on; current interrupted, transistor off.

MOSFETS let the current through one way ("body diode" direction) always, with some body diode resistance. However, when you charge the gate capacitor (meaning apply a voltage and a short spike of current) they will conduct equally well in both directions. When wired with the controlled voltage against the body diode (normal,) this means they are "off" when the gate voltage is low, and "on" when the gate voltage is high. This actually saves power, because there is no "wasted" current through the gate, other than when switching state.

However, this also makes a MOSFET annoying to switch, because you need to actively pull it UP (charge) when turning on, and DOWN (discharge) when turning off. (This is for N-channel; P-channel is approximately inverse.) Thus, a simple make-or-break switch won't work, unless you replace one of the "pull" directions with a resistor. The output of the Arduino is already switched both ways -- it either ties to VCC, or to ground, so it can drive a gate directly. The output of an optocoupler is not, though, hence this confusion.

For a MOSFET, the allowable voltage from source to gate (what controls the built-in gate capacitor) is different from the allowable voltage from source to drain (the controlled voltage.) A typical low-voltage device might allow 20V from source (0-reference) to gate, and 30 volts from source (0-reference) to drain. (IRLB8721 for example -- popular 30V D/S device with 4.5V recommended gate voltage and 20V max G?S voltage) Here's an example data sheet showing those voltages:
http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CDEQFjAA&url=http%3A%2F%2Fwww.adafruit.com%2Fdatasheets%2Firlb8721pbf.pdf&ei=IZiNUKaZM8LtiwKghYHADA&usg=AFQjCNGUrxQ2jGVS0ZaY7Mic2FOCkETjEQ

jwatte:
If it has emitter and collector, it is not a MOSFET, it's a BJT (a k a "70's transistor.")

BJTs let current through between collector and emitter when there is also a current (the right way) through the base. This means that there will always be current flowing through the base in addition to the switched current when it is on.

MOSFETS let the current through one way ("body diode" direction) always, with some body diode resistance. However, when you charge the gate capacitor (meaning apply a voltage and a short spike of current) they will conduct equally well in both directions. When wired with the controlled voltage against the body diode (normal,) this means they are "off" when the gate voltage is low, and "on" when the gate voltage is high.

For a MOSFET, the allowable voltage from source to gate (what controls the built-in gate capacitor) is different from the allowable voltage from source to drain (the controlled voltage.) A typical low-voltage device might allow 20V from source (0-reference) to gate, and 30 volts from source (0-reference) to drain. (IRLB8721 for example -- popular 30V D/S device with 4.5V recommended gate voltage and 20V max G?S voltage) Here's an example data sheet showing those voltages:
Redirect Notice

Thank you for explanation. The optocoupler has collector and emitter, so it must be the BJT type.

It says: Vemittercollector = 6, Vcollectoremmiter = 80 (if again im looking at the right thing). Its says thats absolute maximum.

The first bit is the negative voltage limit so the emitter should not be more than 6V above the collector, you only get this if you wire it up the wrong way.
The collector emitter voltage is the maximum you can have so 24V is well within the limit in the circuit you want to use it in.

Terraviper-5:
Thank you for explanation. The optocoupler has collector and emitter, so it must be the BJT type.

Yeah, I don't know of any MOSFET optocouplers (a MOSFET light transistor?) I thought you meant the data sheet for the transistor you're trying to switch. You have to make sure you don't break that device, too :slight_smile:
If the opto-coupler can't take the voltage, a Zener and a resistor can fix that in a pinch.

I don't know of any MOSFET optocouplers

Yes you can get opto couplers with FET outputs as well as couplers with SCR and Triac outputs. Do a search in any major distribuitors.

so it must be the BJT type.

All opticouplers are of bjt type: as a minority carrier device, bjts are a lot more sensitive to light than a majority carrier device (mosfet for example).

You should look at the datasheet for your device.

Generally, the If specification is between 5ma - 20ma - I typically do 10ma to maximize ctr. Your runs at ~20ma, so it is properly done.

The phototransistor side is not. The ctr is generally 50 - 150%, and I typically design for 50%. That means your phototransistor side's Ic is about 10ma - likely lower with earlier opto-couplers. At 10ma, your Vgs, fully on, is about 7v - not bad but not great either.

Outputting on the emitter is not common: it is often done for speed or to reduce ctr. Most of the times, the output is done on the collector - it produces sharper on/off behaviors. If you look at your datasheet, it shows that as well.

I would put the opto-coupler on the bottom and use a zener to protect the gate.

I put this simulation together to get you a comparison between two approaches to switching.

The plot is about power dissipation on the mosfet, switching a 12amp / 24v load.

The emitter output approach dissipates about 14w over the mosfet 50% of the time. The collector approach dissipates 14w for about 2.5% of the time. That means that you can run the mosfet (to220) without heat sink in the 2nd approach but not the 1st approach.

So try not to output on the emitter.

Thank you for explanation.
Sorry it took me so long to answer, I have been very busy and did not have much time for this project. Now I have assembled this:

But I still got problems: To test the circuit before I connect Arduino to it, I connected a 5V power supply where Arduino pins should be. It activated the motors, but when I disconnected, the motors were still on. They were also still on if I disconnected the main power and then reconnected it. Only after disconnecting the MOSFETs from Optocouplers did the solenoids return to off-state. Im probably missing something important that I do not see because Im a newb. I again ask for your help!

PS: I know that I should put separate resistors for each OC, but they will not be all online at the same time.

Thank you!

What a very confusing way to draw a schematic.

The emitters of the optos should be connected to ground, the -ve of your 24V source and the FET gates should be connected to the collector of the FETs.

Grumpy_Mike:
What a very confusing way to draw a schematic.

Sorry, I have made very few schematics and am still learning how to make them better.

The emitters of the optos should be connected to ground

Do you mean to the - of the 24V? But that makes all three optos connected, if one opens all three FETs open.

the -ve of your 24V source and the FET gates should be connected to the collector of the FETs.

Isnt the FET gate same as opto's emitter, since the two are connected?
Do you mean drain by collector?

But I still got problems:

I think you may save a lot of time / money by buying a ready-made board.

dhenry:

But I still got problems:

I think you may save a lot of time / money by buying a ready-made board.

But then I wouldn't learn anything :\ And I already bought all the components :frowning: True though, this thing is taking far too long.
I successfully made it without the optos, im having trouble now when Im trying to add them with a separate power supply since 24 V is too much for the FET's (which have max 20V).
A pre-made boards like Arduino Motor Shield R3 usually have only two outputs, and I need three. If I bought two of these, it would cost me 45 € + shipping, while these components cost me 10 € max. Sure I could probably find one with more outputs, but shipping would still be the problem (as its expensive and I cant get those things in my country).

I cant believe that I have so much trouble assembling such a simple circuit...

"I cant believe that I have so much trouble assembling such a simple circuit..."

It's always a deceptively easy looking prospect. (:

But then I wouldn't learn anything

Sure.

Try this:

from the mcu pin: use a small value resistor (330 or 390 ohm for example) to the opto's anode. its cathode to ground.
on the opto's phototransistor side: a 1k resistor from the 7v source to the phototransistor's collector. Its emitter to ground.
the mosfet: its gate to the phototransistor's collector. Its source to ground. Its drain to the load, parallel'd by a diode (pay attention to the polarity). The other end of the load to the 24v source.

If you cannot make that out or make it work, you should just buy a ready-made board.

Note: depending on the load and diode used, such an arrangement will have a lot of rf noises. I would put a bead on the mosfet's drain.

Isnt the FET gate same as opto's emitter, since the two are connected?
Do you mean drain by collector?

I mean:-
The emitters of the optos should be connected to ground, the -ve of your 24V source and the FET gates should be connected to the collector of the FETs.

Do you mean to the - of the 24V?

Yes.

But that makes all three optos connected, if one opens all three FETs open.

No. Each FET gate should be connected to the collector. Each emitter should be connected to the same ground.

The circuit as you drew it is just not going to work. Transistors work with current flow, there is no where for the current to flow if you connect the emitter directly to the FET's gate because a FET has such a high input impedance.

Please ignore dhenry he is our resident idiot.

Transistors work with current flow

This is kind-of ambiguous, because every component "works with" current flow of some sort, or it wouldn't be a very useful component. I think I know what you meant, but someone new to electronics might not.

This is basic stuff. I feel it will help anyone who's not yet clear on it and reads this thread:

BJTs (NPN and PNP transistors) work such that the flow through Collector-Emitter is proportional to flow through Base times the amplification factor, with polarity depending on the type. Thus, a BJT transistor needs current flowing through the base to let current flow through the controlled path.

FETs (N-channel and P-channel transistors) work such that flow through Source-Drain is proportional to the charge built up between the gate and the source. The gate is like a capacitor, so while a small amount of current will flow into it to build up that charge, no current will flow through the gate once it's built up, and the MOSFET will still conduct. The only reason to keep the gate voltage on is to replenish leakage current from the gate. This is also why disconnected (floating) MOSFET gates may stay turned on after disconnection, or even spuriously turn on, or off, if there is not enough impedance to either gate voltage or ground.

This is also why it's OK-ish to leave a BJT base floating when you're not driving it -- no current flows, so it will not conduct. But for a FET, that's not true. You must make it so that the gate is ALWAYS connected to either a voltage, or ground. Typically, you do this through a pull-up or pull-down, depending on what you want the default condition to be.

For an opto-coupler that pulls the gate up to turn it on (N-channel FET) you need to also keep a pull-down to ground to the gate, so that the gate turns off when the opto-coupler is not transmitting current. Perhaps a 2 kOhm pull-down will be sufficient, if you're not trying to switch the FETs at too high an on/off frequency.

This is kind-of ambiguous, because every component "works with" current flow

No a FET as you point out works on voltage, there is no current involved in making it work. Leakage current is just that: current that leaks, it is not current that makes it work.

Grumpy_Mike:
I mean:-
The emitters of the optos should be connected to ground, the -ve of your 24V source and the FET gates should be connected to the collector of the FETs.

Oh, now I get it (I read the sentence wrong). Do you mean like this?

The circuit as you drew it is just not going to work. Transistors work with current flow, there is no where for the current to flow if you connect the emitter directly to the FET's gate because a FET has such a high input impedance.

I though that if I connect a 7.5 V power supply through the optos and to the gates of FETs, the optos will block the path when they are off, and when they are on, they will let the current through and to the gates of the FETs, which will then turn on. At first I though about using the 24V supply for this too, but then I saw the limit on the FETs : +-20V absolute max.
Probably stupid thinking, seemed logical to me though since I know very little about electronics (for now).

jwatte:
The gate is like a capacitor, so while a small amount of current will flow into it to build up that charge, no current will flow through the gate once it's built up, and the MOSFET will still conduct.

Ohh, so thats why the solenoids stayed on (when I reconnected the pwr, ofc) even if I turned off the power and then turned it on again.

For an opto-coupler that pulls the gate up to turn it on (N-channel FET) you need to also keep a pull-down to ground to the gate, so that the gate turns off when the opto-coupler is not transmitting current. Perhaps a 2 kOhm pull-down will be sufficient, if you're not trying to switch the FETs at too high an on/off frequency.

Im actually not sure if my optos are pullup or pulldown. I didnt know where I can get this information. grumpy_mike mentioned they are pulldown on previous page

No prob about the frequency, they will work slowly :slight_smile:

So basically, I add a connection between the gate of FET and -ve of 24V source (trough 2kOhm resistor)? Wont that transmit power to all other FETs as well, so when one is open, all are open?

Grumpy_Mike:
The transistor in an opto coupler can only pull down, so there is nothing to supply the voltage to the FETs gate.

That means they are actually inverse of what I need?