The simplest way is the circuit below:
It shows a basic circuit with an N-MOSFET that drives a relay. As a relay is an inductive load, the fly-back Diode D1 is necessary to protect the FET.
If we presume the input signal is on a TTL level, the FET needs to be a ‘logic FET’ that switches on these levels.
To avoid ‘floating’ of the input, we make sure that without any input signal the Gate of the FET has a defined low level by adding resistor R2. The value of this resistor is not very critical.
With regard to the gate resistor R1, there is a lot of discussion whether this is necesary or not, but as it limits the current surge when a load is applied I have chosen to add one.
There is also discussion on whether the gate resistor should be placed to the left of R2 instead of the right, but that is not necessary, and not critical. Should it be on the left, R1 and R2 would form a minimal voltage divider and as R2 is solely there to define a logic state and R1 only to protect the gate, they can stay as is.
The value of R1 is also not very critical: at 150 Ohm the current is limited to 5/150= 33mA which is still within the 40mA spec of the Arduino (mind you, this is an extreme case as the Gate resistance is very high). Upon increase of the value of R1, it takes longer to send the FET in saturation, which increases the RDS value, causing the MOSFET to get hotter.
Let’s take the IRL520NSP as example for our calculation: As the input capacity of MOSFET is 440pF (datasheet page 2/11), the time it would take for the FET to reach saturation would be:
t=2.2RC t=2.2 * 440*10-12 * 150 t=145.200 *10-12 t=145 *10-9 t=145nS
Though the MOSFET, once in saturation is a pretty good switch, there still is a minimal resistance called RdsOn. To make sure the FET can handle the load, we have to calculate the power that will be dissipated in the FET. As a relay as in the example usually doesn’t use that much current, lets presume we want to switch a device that draws 4 Amps. For the logic FET we have chosen- the IRL520NSP- the specs are:
RdsOn=0.22 Ohm @VGS=5V @ Id=6Amp.
The power dissipated in the FET is then P=I²*R=4x4x0.22 =3.52 Watt.
That is well within the specs of the FET.
In order to calculate how hot the FET will be (and to see if we need a heatsink), we need the “Thermal Resistance Junction to Ambient” (RØJA). For our FET that is 40°/Watt, so that means a temperature rise of 3.52*40=140.8 degrees. Thus at an ambient temperature of 20 degrees that will be 161°C. The TjMax (maximum Junction temperature) for this FET is 175°C so that would be just within spec. A decent heatsink would not be a luxury in this case. Mind you though that many modern MOSFETS’s have a gate capacitance that is much lower, in the range of 10-15pF
In the above example the FET was sinking the current. This means that the Vcc was ‘sourcing’ the current to the load, whereas the FET was providing the connection to ground, i.e. ‘sinking’ the current.
Sometimes it may be necessary to ‘source’ the current, for instance if we need the grounds of two digital circuits to be galvanically connected, but we want to be able to switch off one of the circuits. A battery operated Attiny with an Rf transmitter attached might be a practical example: in order to save the battery, the transmitter needs to be switched off when it doesn’t need to be active.
In that case we ‘source’ the current to the ‘+’ of that circuit, that is galvanically connected to earth where it ‘sinks’ the current. A common cathode RGB is another example that would need this.
Sourcing current can be done with a p-FET. A Logic Level P channel MOSFET is e.g. the TPS1110 with an RdsOn of 65mOhm at -4.5Volt at 6 Amp. Another Logic Level MOSFET is e.g. The IRL6402 or the IRLR9343, but the latter is more suitable for audio amplifiers. The IRL6402 also does have a very low RdsOn of 65mOhm at 4.5Volt (and 3.7 Amp)
The circuit doesnt differ that much from the ‘sinking’ circuit with the N-FET. It is in fact everything flipped over its horizontal axis (but the direction of polarity for the diode remains). The circuit is OFF when the Gate is the same voltage as the Source (VGS = 0), which means that we have to output 5V (HIGH) from the Arduino to turn the MOSFET off. To turn it on we need -5V at the Gate (relative to the Source) so we need to output 0V (LOW) from the Arduino to turn the MOSFET on.
If we briefly go back to the ‘sinking’ circuit with the N-FET, it is clear that it doesnt really matter what the required Voltage of the Load is: 5V, 12 Volt, 30 Volt: as long as the Mosfet is able to handle it it doesnt make any difference for the arduino (provided that has its own 5 Volt supply).
The sourcing/P-FET circuit however does face a problem if the supply voltage goes up.
Suppose the supply Voltage is 12 Volt. Then our 5Volt TTL level will never be high enough to make VGS equal to 0 (relative to the source, so 12 Volt). It will 0Volt (Arduino pin LOW) or 5 Volt (Arduino pin HIGH), ergo the MOSFET will never be OFF.
The next circuit will solve that:
In this circuit we use a simple signal transistor to Switch the VGS between 5 and 0 Volt (relative to the source so 7 Volt or 12 Volt relative to ground). If the Output from the Arduino is HIGH, the transistor will conduct and the voltage on the collector will become LOW, causing the VGS to increase (remember, it increases in relation to the Vcc). If the Output from the Arduino is LOW, the transistor will not conduct, pushing the Collector voltage towards Vcc, or in other words: lower the VGS, thus closing the FET and switching the Load OFF. This also has the advantage that in comparison with the prior circuit, the signal is no longer inverted: Arduino pin HIGH => load ON. Arduino pin LOW => Load OFF. Another ‘advantage’ is that at a higher voltage also non-TTL level MOSFETs can be used.
The calculations on this circuit are as follows:
The base transistor limits the current to
5V-Vbe/1000 =5-0.7/1000 =4.03/1000 =4 mA
As the Arduino pin will have no trouble sourcing that, the BC547 will be in full saturation.The Collector Emittor saturation voltage is 0.2 Volts. Therefore the Vcollector and thus the Vgate will be 0.2 Volts, in other words the VGS will be 11.8 Volt, which is more than enough to send the MOSFET in saturation.
As a double check: The current through the resistor will be 11.8/1000=11.8mA
lets presume the gain of the transistor is around 100 (it depends a bit on the type of BC547 and the current range in which we are working), the Ib will then need to be minimally 118uA, which is sufficiently met by the current of 4mA.
If we want to calculate the power and heat development in the transistor we do that as follows:
P = (VBE * IBE) + (VCE * ICE) =0.7*4+0.2*40 =2.8+8=10.8mW RØJA = 200 °C/W (from datasheet) Temperature increase= (10.8*200)/1000=2.16°C
This works well, untill the Supply Voltage goes up to above 12 Volt. That is not some magical number, but it is in general the max VGS the FET’s can take. The TPS1110 for instance has already maximum rating of 7 Volts.
We calculate the circuit as follows: If the Arduino goed HIGH. there will be 5 Volt on the base of T2. Therefore there will be 5-0.7=4.3 Volt on the emitter. Therefore the current through R2 is:
4.3 / 330 = 13 mA
With an hFE of 100, about 99% of that current will flow through the collector as well and thus through R1. This would normally mean a voltage drop of 13 Volt over R1. Because we have a zenerdiode, much of that current will flow through the zener, when it tries to maintain the voltage drop to 10 Volt over VGS.
The function of the 1 k resistor (R1) is then mainly to pull the gate quickly back up to the supply voltage and thus VGS = 0 if the transistor is off. R2 is there mainly to create a current that can activate the Zener without the zener having to dissipate the full Vcc
This won’t be an issue for low-switching speeds (eg. turning garden lights on and off, or a fish feeder) but becomes an issue when doing PWM (pulse-width modulation) where you may be switching on and off many times a second.
The analogWrite function on the Atmega328-based Arduinos runs by default at 490 Hz. Tests indicate that the circuit above will take about 1 µS to switch on and 8 µS to switch off (if using the high-side driver) and 1 µS to switch both on and off if using the low-side driver. (High side means your switch/FET is between the Vcc and the load; low side means the switch/FET is between the load and ground.)
This is one of the reasons for choosing a reasonably low value for R1 in the high-side driver. We rely on R1 to pull the gate of the MOSFET high when the transistor is turned off, and we need that done as quickly as reasonably possible. We could potentially use a lower value resistor for R1 (eg. 470 ohms) to increase the switching speed, but then Q1 (the driver transistor) has to switch higher currents, and we need to watch that this transistor (Q1) does not get too hot.
It is not intended to drive a 3 amp electric motor.
In this case you have a separate power supply (maybe a battery, maybe a plug-pack) which is the “+5V” or “+12V” on the circuits above.
However it is important to connect the grounds. That is, connect the ground wire from the battery or plug-pack to the ground wire on the Arduino. That way they have a common “reference voltage”.