If you need to switch a dc load, a MOSFET is a very useful component. Typically carrying much more current than a standard transistor, and better performance characteristics, like a high impedance gate that draws very little current. BJT’s are current driven devices, MOSFETs are voltage driven devices.
Not all MOSFETs are the same, and too many Arduino sites show the IRF series MOSFET. The IRF series require 10v (VGS = 10.0 V) at the gate to fully open at anywhere near rated loads, so we use the IRL series. Any logic level N-Channel MOSFET (VGS = 5.0 V) will work, and look for the lowest RDS(on) (Ω) resistance you can practically find, to limit heat buildup. Connect your DC load between + and the Drain (D) of the MOSFET. If you are using an inductive load, like a motor, relay, or solenoid, do not forget the flyback diode across the load terminals.
Become the Maker you were born to be. Try Arduino Academy for FREE!
Connect the MOSFET Source (S) to ground, or negative terminal of your voltage source. We add two resistors, a 10k Ohm from the MOSFET Gate (G) to ground to ensure turnoff when Gate signal is removed, and a 125 Ohm resistor between the Arduino output and the MOSFET Gate (G). This protects the Arduino pin from too much current draw if the MOSFET fails. The value is determined by the voltage of the Arduino pin (5v) divided by the max current we want to allow (40ma). The Arduino sends a HIGH signal to turn on the MOSFET, a LOW to turn it off, and can also use PWM (analogWrite on an appropriate pin) to control motor speed, lamp brightness, etc.
However this won’t work with a 12V common ground system like an automobile for controlling trailer lights. I’ve been trying (and smoking a few components) to work out a good solution and have yet to come up with an answer.
Corrected email addy.
40mA? That is pretty high for an ATMEGA pin! Double the resistor to 250 or 300 Ohms to be sure you never over tax the upc pin. You could also raise the resistor to ground to 20K if needed, and still have pretty good nose rejection.
I do not understand the 125 Ohm resistor requirement. The IRL540 is high impedance and low input capacitance.
True, it will work without it, but the main idea is to limit the peak switching current and suppress possible oscillations by limiting the bandwidth of the FET circuit a bit. With a really fast switching edge, the peak current into the gate could be many amperes, assuming the output can source this level of current, that’s what gate drivers are for.
With an I/O pin, the current limiting structures in the chip have to deal with the spike, better to have the current externally limited.
It's to protect the Arduino pin in case of a mosfet malfunction.
The body diode will not protect the mosfet. The inductive kick puts a HIGH voltage on the drain – you need a diode across the motor.
Not so …
The reverse diode is an avalanche diode and will protect the transistor under a lot of inductive switching conditions. The difficulty is in establishing exactly what the actual condition are.
The IRL540 data sheet shows a max avalanche current rating of 28A, single pulse avalanche energy of 440mJ, and a repetitive avalanche energy of 15mJ. So given most hobby projects will have motor currents of much less than 28A, unless you are switching very rapidly (say PWM-ing) the motor, you will have nothing to worry about. If you are PWM-ing you should use a diode on the motor.
You are correct. Same thing with relays.