jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

N-channel MOSFET on GPIO

Sun Jul 07, 2013 11:32 pm

Hey guys,

I've got an N-channel MOSFET to activate a 12V strike latch. I've attached the Gate of the MOSFET to GPIO4 on my Pi Cobbler breakout, the Drain of the MOSFET attached to the negative terminal of the strike latch, and the Source of the MOSFET attached to my circuit ground.

I'm unsure as to where the negative terminal from the 12V power supply goes? The positive terminal goes to the positive terminal of the strike latch, but I'm left with the negative wire just hanging around? My guess is that I connect it to the circuit ground to complete the circuit, but when I do that, the strike latch activates (a good sign), regardless of what I have the code doing in the Pi.

Here's the code I'm hoping will work:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT, initial=GPIO.LOW)

GPIO.output(4, GPIO.HIGH)

I'm obviously not doing something correctly, as the latch activates as soon as I ground the 12V power supply negative wire, which suggests that there is no logic involved with the power going through the MOSFET. GPIO4 is running on 3.3V. The N-channel MOSFET I'm using is this one:

http://www.freetronics.com/products/n-m ... dn6bY4cdfI

Any clarification here would be great!

Thanks

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 12:24 am

On further research, it appears that the maximum Gate voltage for the above listed N-channel MOSFET is 2.5V...Seeing as it's receiving a 3.3V Gate signal when GPIO4 is set to High, I'm guessing it won't work?

Strange, seeing as this MOSFET is designed for use with the Arduino, which operates at similiar voltages I thought.

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 12:33 am

The correct maximum voltage is Vgs(max), are you sure it's not Vgs(on) or Vt that you are confusing?
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 1:02 am

The documentation only mentions: Gate threshold voltage: Max 2.5V...

If that's the case, can I use voltage-reducing resistors to bring the 3.3V down to 2.5? I'm really under the pump time-wise, so finding a different MOSFET or a voltage leveler isn't really an option unfortunately.

Thanks for your input!

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 1:18 am

Ok, so the Gate ideal voltage is 1.8V. I've reached this voltage by putting an 8.2kOhm resistor in series with the 3.3V supply. I'm now getting ~1.8V going to the gate. However, when I connect the negative wire from the 12V power supply to the circuit ground, the latch still activates. Changing the script from output(4, False) to output(4, True) has no effect. Could it be that I've burnt out the MOSFET, and the current is going through it regardless of the logic being applied?

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 1:22 am

This is not the maximum voltage which can be applied, it is the maximum voltage that could be needed to start switching on the FET.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 1:31 am

That would definitely make more sense. What am I doing wrong then? I feel strange connecting the negative wire from the 12V power supply directly to the ground rail on the circuit breadboard...I have no idea where else to put it though? Thanks again!

User avatar
johnbeetem
Posts: 945
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Mountains
Contact: Website

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 1:34 am

jars121 wrote:On further research, it appears that the maximum Gate voltage for the above listed N-channel MOSFET is 2.5V...Seeing as it's receiving a 3.3V Gate signal when GPIO4 is set to High, I'm guessing it won't work?

Strange, seeing as this MOSFET is designed for use with the Arduino, which operates at similiar voltages I thought.
The maximum gate voltage of 2.5V just means that all NTD5867NL MOSFETs will turn on with Vgs >= 2.5V at 25C. Typically, they'll turn on at 1.8V. Some will turn on as low as 1.5V. The actual voltage Vgs you can apply can be much greater than 2.5V -- it can go as high as 20V continuous.

In fact, if you look at the NTD5867NL data sheet many of the curves are for Vgs = 10. In a MOSFET, the Vgs threshold is when you first get conductivity between drain and source, and near Vgs it can be very small, i.e., the on-resistance may be quite high. Take a look at Figure 3: "On−Resistance vs. Gate−to−Source Voltage". Below 4.0V, the on-resistance from drain to source rises exponentially and Vgs = 3.3V isn't even on the chart. This MOSFET will work OK when driven by a 5V GPIO like Arduino, but RasPi's 3.3V is not going to turn it on very far. You're probably better off with an NPN Darlington pair.

From the way you describe your hook-up, it sounds like your transistor may have shorted out. I'd try playing with the module separate from RasPi and drive the gate through a 10K resistor pulled up to your 12V supply. Since the module has a 10K pull-down, that will give you about 6V on the gate which should turn it on nicely.

If the transistor is burned out, one possibility that you need a flyback diode across the relay. The NTD5867NL appears to have a built-in zener so you may not need a flyback diode, but with most relay and motor circuits you do.

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 1:44 am

Thanks for the detailed reply. I'll have to read through it a couple of times to fully understand it. Can you clarify where the negative from the 12V supply goes? I've seen various diagrams of N-channel MOSFETs and I can't determine where it goes...

I think the transistor may be dead, so I'm going to quickly go and grab a few more (in case I fry another one :D )

Any clarification on the negative supply wiring would be amazing, thanks!

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 1:48 am

The ground rail or 0v point in the complete circuit.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 2:46 am

Awesome, thanks for that.

I've picked up a few more MOSFETs.

If the latch automatically activates when I connect the 12V supply negative wire to the circuit ground, regardless of the signal being supplied to the gate, does this indicate that I've connected the circuit incorrectly? It seems like such a simple circuit to make, but it's just not working!

I might try a pull-up resistor to bring the voltage up towards the 5V mark.

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 4:04 am

I'm starting to pull my hair out, I just can't get this. I've tried three MOSFETs, all of which seem to have absolutely no effect on the circuit. As soon as that negative 12V supply wire touches the circuit ground, the latch activates. I've tried putting in pull-up resistorsm which haven't had any impact at all.

I don't understand why it's not working? I've followed various tutorials/videos down to the letter, but I'm getting nowhere?

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 5:51 am

I've tried running the MOSFET on a separate board, using a 5V input signal. The MOSFETs (I've tried 4 now) are still having no impact on the circuit at all. If I remove the Gate signal entirely ie. nothing connected to the Gate, the switch still activates. I would have thought this was impossible, as the MOSFET needs a Gate voltage to allow current to the load?

I have no idea what to do now. Any input? I'd upload a diagram/picture of my setup, but it really is as simple as this:
  • Positive 12V supply goes to positive terminal on strike latch
    Negative terminal on strike latch goes to Drain
    3.3V RPi signal goes to Gate
    Source goes to circuit ground
    Negative 12V supply goes to ground
No matter what I do, whether it be adding resistors, changing power supplies, changing MOSFETs etc etc, as soon as that negative 12V supply wire touches the circuit ground, the switch activates.

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 6:46 am

Never mind, I finally got it working :D

I swapped the negative terminal of the strike latch with the negative 12V supply wire, and it works fine now.

Not entirely sure why, but I don't have time to question it haha.

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 12:43 pm

Just bumping this up. I'm really hoping someone is able to explain to me in layman's terms how to get this working properly.

The MOSFET is working intermittently, but I don't have any resistors involved. What do I need to do in terms of adding resistors to increase the Gate voltage? Running at 3.3V is definitely too low, the MOSFET is getting quite warm.

It's nearly 11pm and I'm presenting in 12 hours...it's going to be a late night.

Anyone?

pksato
Posts: 295
Joined: Fri Aug 03, 2012 5:25 pm
Location: Brazil

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 12:44 pm

Hi,
How to power lamp, relay, motor, solenoid, and other power device are very common question. I drawed four way to do it.
Image
http://img259.imageshack.us/img259/8032/cu2.png
For reactive devices, like a relay, motor a diode is necessary to absorb flyback current.

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 1:04 pm

Thanks for that, I really appreciate it. I'm afraid the diagrams themselves don't help all that much, I can't really visualise how to replicate that on the breadboard itself. I don't have access to a diode either, which is really annoying, but there's nothing I can do about that now.

It was working before, I just need to increase the Gate voltage to make it more stable. Can anyone explain how I would do this on a breadboard?

Thanks!

User avatar
johnbeetem
Posts: 945
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Mountains
Contact: Website

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 7:08 pm

jars121 wrote:Just bumping this up. I'm really hoping someone is able to explain to me in layman's terms how to get this working properly.

The MOSFET is working intermittently, but I don't have any resistors involved. What do I need to do in terms of adding resistors to increase the Gate voltage? Running at 3.3V is definitely too low, the MOSFET is getting quite warm.
I think the best way to increase the gate voltage is to use a "High-Side Switch Controller" such as Linear Tech LTC1981. I haven't used this part myself, but it appears to do the right thing. When enabled, its internal charge pump boosts VCC 2.5 times up to 7.25V.

Another way is to replace the MOSFET with an NPN Darlington Pair. It only needs 1.3V to operate, with a suitable series resistor.

masterdrain
Posts: 36
Joined: Fri Jun 07, 2013 7:44 pm
Location: Isle of Wight
Contact: Website

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 9:25 pm

You must tie the gate to the source with (say) 2M2 resistor. Have a look at http://www.electronics-tutorials.ws/tra ... ran_7.html - explains it all.

Iain Johnstone

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

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 10:53 pm

its a positive gate-to-drain voltage that will turn a n-fet on. normally anything above a certain threshold, of several volts will do it. Connecting a 10K resistor between the gate (or the base of a transistor) and the GPIO pin will be an insurance against blowing up the GPIO when accidentally a high voltage manages to reach the gate (by shorting wires together, etc) and won't influence the FETs gate voltage.

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: N-channel MOSFET on GPIO

Mon Jul 08, 2013 11:55 pm

you need to check the data sheet for the MOSFET that you are trying to use. it is most likely so that the transistor has too high threshold voltage, so that it cannot drive much current when you apply 3.3V to the gate. you need to get a MOSFET that has Rdson specified at 2.5V gate voltage.
(edit: I checked the datasheet mentioned, and the transistor typically should be able to drive several amperes even with 3.3V, but this device only has Rdson specified at 4.5V, so there is absolutely no guarantee that you can use it with 3.3V).

regarding your other questions, you do not need any resistors, but it is a good idea to have a resistor between GPIO and gate. the resistor is already on the external board. it is 1k. Ground the external power supply should always be connected to GND of the Pi. and don't forget to connect Source of MOSFET to GND also.

you need to measure the voltage between Gate and Source of the MOSFET (directly on the pins, or across R2), so you know if the part is working and that the GPIO is delivering enough voltage. note that if you leave the gate unconnected it is very possible that the Drain will conduct current, as the gate voltage is floating and could be anything. if R2 is present it should pull gate-tource to zero volts, so there should be no current flow in Drain-source.
try to connect gate to source, and make sure the MOSFET is not conducting. if it is, it is broken. the most likely cause is gate overvoltage.

have a look at the data sheet for "BSC046N02KS G" from Infineon. this is a power MOSFET that can easily handle 15A current with 3.3V gate voltage, even when just using the circuit board as heatsink. It only is rated for 20V maximum Drain-source voltage, but that is fine for 12V circuits. Add a 100ohm resistor from gate to GPIO pin. you can use higher resistor value, but that slows down the switching of the MOSFET, causing it to heat up when it is turned on or off. do not try to switch faster than a few times per second. if you need to switch with higher frequency you need a gate driver between the GPIO and the MOSFET gate, as the GPIO is so very weak as a gate driver.

PiGraham
Posts: 3553
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: N-channel MOSFET on GPIO

Tue Jul 09, 2013 6:39 am

Tage wrote:you need to check the data sheet for the MOSFET that you are trying to use. it is most likely so that the transistor has too high threshold voltage, so that it cannot drive much current when you apply 3.3V to the gate. you need to get a MOSFET that has Rdson specified at 2.5V gate voltage.
(edit: I checked the datasheet mentioned, and the transistor typically should be able to drive several amperes even with 3.3V, but this device only has Rdson specified at 4.5V, so there is absolutely no guarantee that you can use it with 3.3V).
According to the data in the link in the OP gate threshold is 2.5 max so 3.3 should have it full on.
Tage wrote:regarding your other questions, you do not need any resistors, but it is a good idea to have a resistor between GPIO and gate. the resistor is already on the external board. it is 1k. Ground the external power supply should always be connected to GND of the Pi. and don't forget to connect Source of MOSFET to GND also.

you need to measure the voltage between Gate and Source of the MOSFET (directly on the pins, or across R2), so you know if the part is working and that the GPIO is delivering enough voltage. note that if you leave the gate unconnected it is very possible that the Drain will conduct current, as the gate voltage is floating and could be anything. if R2 is present it should pull gate-tource to zero volts, so there should be no current flow in Drain-source.
try to connect gate to source, and make sure the MOSFET is not conducting. if it is, it is broken. the most likely cause is gate overvoltage.
The board has 10k gate pulldown.

It should work.

PiGraham
Posts: 3553
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: N-channel MOSFET on GPIO

Tue Jul 09, 2013 6:44 am

@jars121,

Have you got the drain and source the right way round?
You should have supply positive to your latch, other side of the latch to Drain, Source to Pi 0V, supply negative to Pi 0V.
if you connect drain and source the other way, with positive on source it will conduct through the diode whatever you do with the gate.

jars121
Posts: 123
Joined: Tue Jun 25, 2013 8:35 pm

Re: N-channel MOSFET on GPIO

Wed Jul 10, 2013 8:41 am

Thanks for the continued support guys, it's really appreciated. I don't have access to the MOSFET/12V latch at the moment (it's at work), but I will check the above things as soon as I have access again.

GregWoods
Posts: 4
Joined: Mon Aug 06, 2012 9:39 am

Re: N-channel MOSFET on GPIO

Wed Nov 20, 2013 9:53 am

pksato wrote:Hi,
How to power lamp, relay, motor, solenoid, and other power device are very common question. I drawed four way to do it.
Image
http://img259.imageshack.us/img259/8032/cu2.png
For reactive devices, like a relay, motor a diode is necessary to absorb flyback current.
@pksato
I like the diagrams. Extremely useful.
Could you provide a little info on the purpose and relationship of the two resistors to the MOSFETs?
I had assumed that for the Raspberry Pi, I would be able to connect a GPIO pin directly to the MOSFET Gate-Source. Is that a bad thing to do?

Return to “General discussion”