The Classroom

Making with MOSFETs

Using MOSFETs with Microcontrollers

Daniel Koch

Issue 32, March 2020

This article includes additional downloadable resources.
Please log in to access.

Log in

Using MOSFETs to control loads from an Arduino or Raspberry Pi is not as simple as the Internet would have you believe.

Although microcontrollers can be used for an almost limitless number of purposes and situations, for the maker, projects tend to fall into certain categories. Some projects read sensors and give data to a computer for use. Others use a computer to control outputs, and others use inputs to control outputs. In many cases where an output is needed, the voltage and current available from an Arduino or Raspberry Pi (or any other micro) are simply too small to use directly.

Options for controlling loads are usually relays or transistors. The transistor options commonly used are some form of bipolar transistor, or a MOSFET. Browsing online designs and maker projects, there is a strong preference for MOSFETs. Sometimes this is just because of the lead set by other projects. At other times, it is because of the switching characteristics of MOSFETs. However, many project examples are actually bordering on not working, or will destroy the MOSFET after a while. Just because a circuit works when first turned on, does not mean it will keep functioning after a hundred hours, or even ten hours.


MOSFET is an acronym for Metal Oxide Silicon Field Effect Transistor. It is a sub-type of FET, so we’ll explain those first. Field Effect Transistors are a different physical construction of N- and P-type materials compared to bipolar transistors. If you want to read up on those, or refresh your memory on N- and P-type materials, those have been covered in previous issues of DIYODE.

Discussing the N-channel devices, rather than a sandwich of NPN materials like a transistor, FETs consist of a piece of N-doped semiconductor material with terminals at each end. These are the ‘drain’ and ‘source’ terminals, which is where the load connects. This piece of material is called the ‘channel’, and it carries the current like water flows through an estuary channel. The ‘gate’ terminal, which does the controlling, is connected to a piece of P-doped material which surrounds the channel. This behaves as a PN junction.

Operating a FET involves biasing the gate with a voltage, not a current like in a bipolar transistor. The ‘source’ terminal is grounded, while the ‘drain’ terminal is connected to the load. As the voltage between the gate and source (VGS) increases, a voltage field is generated around the gate, which restricts the current flowing from drain to source. Note that the voltage at the drain cannot be negative compared to the gate, and both should be positive when compared to the source. If VGS falls, the field effect reduces, and current flows. Thus, voltage controls current flow in a FET. A P-channel FET is the same, except that the polarities are all reversed.

MOSFETS are a development of the FET. A thin layer of silicon oxide is placed between the channel and gate. This changes the properties in several ways, depending on materials and construction. MOSFETs can be made as either ‘enhancement’ devices, or ‘depletion’ devices. Enhancement devices have a channel that is electronically incomplete.Forward-biasing the gate causes the field effect which completes the channel, causing current to flow proportional to the gate voltage. In depletion mode devices, the opposite is true, and they behave similarly to a plain FET. Reverse-biasing the gate causes current flow, and a forward-biased gate will switch the device off. Both enhancement and depletion types are available in N- and P-channel devices.


While the FET can use voltage changes to vary current flow, MOSFETs are more often used as switches. They can still amplify, but are usually not used to do so in the maker world. In a MOSFET, there is a threshold voltage at which the device begins to conduct measurable current. Called the Gate-Source Threshold voltage and abbreviated as VGS(th), the datasheets will give this value as a voltage, measured at a specific current. For example, the commonly used IRF540N has a VGS(th) of 2 to 4 volts for 250μA (microamperes). This means that, depending on manufacturing tolerance, between 2 and 4 volts is needed at the gate to even get a quarter of a milliamp from the drain to the source.

If switching with 5V logic, it is unlikely that the full 5V would gain any usable current capacity from the device. The response after this is dictated by a graph included with the datasheet and varying from device to device. The graph is labelled ‘Typical Transfer Characteristics. Remember, the different VGS(th) numbers are a minimum and maximum for manufacturing tolerance, and whether a particular example is at the 2V end of the scale, the 4V end, or in between, cannot be chosen. The graph is quite steeply curved at first, and if a device happens to have a 2V VGS(th), it may be able to drive a load with a couple of amps. However, the device is rated at 33A, and to gain this current capacity, the VGS(th) needs to be much higher than the minimum threshold. The graphs in the datasheets are a good guide, but at the lower ends, are not perfect. Noted in the datasheets are the test conditions. For the IRF540N, the test was conducted at a drain-source voltage of 50V in twenty microsecond pulses. For continuous drive, the current will be lower.

Below threshold voltage, the device is in what is called the ‘cut-off’ region. At this point, for all practical purposes, the device is fully ‘off’. After the threshold, there is a ‘linear region’, at which point Drain-Source current increases linearly with gate voltage up to a point where the maximum current capability is reached. This is shown in a graph in the datasheets as well. At the end of the linear region, which is below the maximum gate voltage, is the ‘saturated’ region. Regardless of the gate voltage, the device passes its maximum current in this area.

All of this means that a 100V 33A MOSFET with a 20V Gate-Source voltage maximum rating and a gate threshold of 2 to 4 volts will not deliver 33A on a 12V load if the gate is driven from 5V. While many maker circuits feature the IRF540N, it is clear that the full current capacity cannot be realised, and in fact, may be only an amp or two. That is one of the limitations of MOSFETs, however, it is not a disadvantage. MOSFETs exist with logic level drive in mind, and these have VGS(th) figures of between 0.5 and 1V minimum, and 2 to 2.5V maximum. This means a 5V logic signal is almost double the worst-case threshold voltage.

One of the advantages to the maker in using MOSFETs when compared to bipolar transistors, aside from the larger current capability, is the switching speed. MOSFETs are significantly faster than bipolar transistors, thanks to the way they are constructed. This makes them ideal for any Pulse Width Modulated (PWM) switching, or any other application where frequency is a factor. Like any device, they are not instantaneous. Datasheets will have listings for Turn-On Delay and Turn-On Rise Times, and the same for turn-off. These are not the only factors involved, however, and some MOSFETs in specific circuits can achieve switch speeds of 5ns. It is unlikely that anyone driving a circuit with an Arduino or Raspberry Pi will ever be able to drive their outputs faster than the MOSFET can switch. However, there is a little more to it.


The input of a MOSFET is actually capacitive. There is a capacitance between the gate and the drain that has been factored into a datasheet figure called the Total Gate Charge, or Qg. There is also a Gate-Source charge, and Gate-Drain charge, which are Qgs and Qgd respectively. Qgd is listed separately as well as factored into the total Qg. All are measured in nanoCoulombs in the datasheets. Don’t confuse this with input and output capacitance, which are a separate consideration. The Coulomb is a measure of charge, a count of the number of electrons, and is a fixed unit. In fact, it is 6.24 x 1018 electrons. An ampere is one Coulomb of electrons per second past a given point. There is a graph in the datasheets that shows the QG, usually in nanoCoulombs, required for a given Gate-Source voltage, in order to pass a specified current.

While the input impedance of the gate is very high, capacitance of the gate means that, when voltage is first applied, there is a very low-impedance load presented as the gate charges. This inrush current necessitates a resistor on the gate. Significant debate can be found online, with all sorts of experts, many even having significant qualifications, disagreeing on whether there is such an inrush current. The debate seems to centre on end-use. While driving from DC, a gate resistor is often considered to be unneeded for an Arduino or Raspberry Pi, and some very convincing maths is provided in the forums to say so. Others point to the fact that the Arduino, even at maximum PWM rate, still does not switch fast enough for the gate capacitance to draw high currents and be a problem. However, these arguments seem to make use of average current calculations. The real danger is in the time when the output is swinging from one state to another, when the peak current can be much higher than the average suggests.

Others say the danger lies in the harmonics of the driving square wave, which is never as pure as the theoretical graphs show, and includes a lot of harmonic frequencies. This can result in rapid unwanted switching, similar to bounce in mechanical switches. This rapid switching places a much greater current load as the gate’s capacitance rapidly charges and discharges. It also highlights that this argument and others rely on average current figures during the whole cycle at a low switching speed, and may not reflect the highest peaks. This makes the gate resistor argument even harder to interpret reliably. Additionally, high, short peaks over time still have a detrimental effect, which may take a long time to show up.


If you’ve ever read anything online about using MOSFETs with maker microcontrollers, you’ve no doubt come across the debate that is summarised above, and its many facets backed up by all sorts of experts. The DIYODE position is this: it’s better to be safe than sorry. A resistor is small and cheap, and including it is unlikely to ruin anyone’s day. An additional argument for using one is that there is an amount of inductance that exists between the inductance of the gate, and both the internal and external wiring of the device. This can lead to a degree of oscillation, or ‘ringing’, and a resistor helps damped this.

The value of this resistor has to be thoughtfully considered. Too low, and it will not limit current enough to provide the protection required for the driving circuit. This current can be quite large, albeit for a very brief pulse, and can still damage an Arduino pin. This is often overlooked in maker circuits. Too high, and the resistance will slow the switching of the MOSFET by slowing the charging of the gate to threshold and above to fully charged. The resistor selected should be as low as possible while still protecting the driver circuit with a safety margin.

The capacitance is still important to know, however, as the RC (resistance/capacitance) time constant is one of the determiners of switching speed. The capacitance value in Farads is the number of Coulombs divided by the voltage. Using the commonly-used IRF540N again, we have a Qg of 71 nC. So, at a gate drive voltage of 10V, 71 nanoCoulombs are divided by 10V. The calculation should be performed in base units:

7.1 x 10-8 / 10 = 7.1 x 10-9, which equals 7.1nF.

For a 5V gate voltage (bear in mind that this is too low to be guaranteed practical on the common IRF540N, but we’re not going to change devices mid-way through the example), the equation for the same device looks like this:

7.1 x 10 / 5 = 1.42 x 10-8, which equals 14.2nF.

MOSFET Circuit Symbols

These are the four MOSFET circuit symbols in common use. While there are others, they are either specialised, or non-standard.

The P-channel symbols feature an outwards-pointing arrow, and the N-channel devices have the arrow pointing in. Note that the depletion mode symbols have a solid between source and gate, while enhancement mode devices feature a broken line. Note also that the centre connection where the arrow lies, is the body connection. This is tied to the source in most devices and is drawn as such. You may encounter the body connection in some literature.

So, at a lower gate voltage, the apparent capacitance for calculating an RC time constant is higher, and thus must be factored in when calculating switching times with the gate resistor.

Switching time is also limited by other factors, including the recovery time of some internal diodes. However, this is highly unlikely to be relevant to the maker. No microcontroller in non-industrial or non-engineering use is fast enough to exceed the response times of the diodes. While it would be tempting to ignore switching time so long as it is within the PWM limits. However, while the MOSFET is switching, it is in the linear region. When the device is saturated (fully on), the resistance given in the datasheets between drain and source applies. For the IRF540N, this is 0.044Ω.

However, in the linear region, the resistance between drain and source increases. The closer to the cut-off, the higher the resistance and the more heat that is generated. As such, designing as fast a switching time as practical helps with heat management.

The Arduino’s heart, the ATmega328, has an absolute maximum GPIO pin rating of 40mA, but the typical operating current is listed as 20mA. This 20mA typical value already has the safety margin built in, so from 5V logic, in order to restrict current flow to 20mA, we get:

5V/0.02A = 250Ω

The good news is we don’t actually need to take the effort to make this calculation. For the most part you can comfortably use common values such as a 1K ohm resistor. Since no DC current flows into the gate of the MOSFET there will be no DC voltage drop across the gate resistor.

Note: Orientation matters here, do not create a voltage divider with the pull-down and gate resistor as this will reduce the voltage potential at the gate.

Many online circuits feature a gate resistor of between 100Ω and 1kΩ. Some sources also say they factor in gate resistance, but the source of this data remains unclear and we were unable to find any listing for it in the datasheets. The fact that gate impedance is a capacitive element and not a fixed DC resistance likely explains this.

While many designs connect the GPIO directly to the gate or use a very small resistor, the reality is that damage to the IO pin will likely take time to occur enough to notice or cause failure.

We conducted early experiments successfully with a few different devices. The challenge as noted, is that we ran one for a maximum of twenty hours. It may be that modern, highly-protected devices are immune from the risk.

There is one more resistor that sometimes is used and sometimes is skipped in maker designs. This is a resistor between the gate and ground, and exists to stop the gate triggering in situations where the IO pin is floating. Such situations may be during power cycling or during code uploads during development. While it could be argued that this is unnecessary given the threshold voltage required for activating the MOSFET, the reality is that even static spikes can trigger the gate. It is more critical in situations where the threshold voltage of a particular device is low. Again, adding the resistor is unlikely to make life overly difficult, so best practice says to include it anyway.


Device Type

By far the most common devices on retail markets are enhancement types. It suits the maker more to be able to connect the source to the ground rail, the drain to the power supply via the load, then drive the gate high with a microcontroller to turn the MOSFET on. Having the output low will switch off the MOSFET. Using a depletion mode MOSFET is easily possible, however, they are less common in the hobby-level electronics world, partly because of the need to have the output pin in use driven high to have the load switched off. Similarly, PWM practice around maker microcontrollers has a convention of high for load on, low for load off. This would need to be reversed with a depletion-mode device, something that is possible but will require care and attention to detail.

Additionally, N-channel varieties dominate maker circuits. P-channel devices are still available in both enhancement and depletion modes, but there is less choice on the retail market. As always, the retail maker market is a whole different game compared to engineering-level, business-to-business commercial suppliers. When using P-channel devices, the considerations around gate voltage polarities and load polarities are similar to NPN versus PNP bipolar transistors - in the maker space, N-channel devices as a low-side switch are often more practical.

Threshold Voltage

When working with 5V logic, Threshold voltage as described earlier needs to be within the 0.5 to 1V minimum, 2.5V maximum range. To make life a little easier, most part numbers for these devices include an ‘L’ for ‘logic’. This is a trend and not a rule, and where the ‘L’ appears in the part number sequence is also up to the manufacturer. However, if in doubt, most suppliers of parts include a link to the datasheet for each device they list, so download it and check the Gate Threshold Voltage VGS(th) line in the ‘Electrical Characteristics’ table.

Current Capacity

The current capacity should be about double whatever you plan to need. This ensures that even if the device is not fully turned on and is still in the linear region, heat due to resistance will be minimised. The same factor applies to the time the device is in the linear region while switching, which is even more important during PWM use. In addition, having a current capacity much higher than needed means that there is little risk of device damage from turn-on currents in inductive or capacitive loads.

Other Factors

While these are the main factors involved in choosing a MOSFET for maker applications, consideration should be given to everything discussed up to this point. Also, consider the package type. TO220 packages are the most common among the devices the maker is likely to encounter, but there are others too. Heatsinking, if required, will contribute to case type decisions. While MOSFETs have been considered static sensitive in the past, and many older devices indeed still are, more recent designs include protection diodes and other features built in. Only sometimes does a MOSFET arrive at the DIYODE office from suppliers in a static-protective package or bag, but that does not mean any others are guaranteed protected. Incidentally, the ones purchased for this article build were marked as static-sensitive. Regardless of built-in protections or lack thereof, it is best to treat any MOSFET as though it is static-sensitive.


Besides choosing the gate resistor suitable for a given application, a user will need to calculate whether or not a heatsink is needed. While Bob Harper covered this in depth in Issue 24, including explaining how to calculate thermal rise due to resistance and current load, information from the datasheet will be needed as well. The ‘Thermal Characteristics’ table has thermal resistances in it. You’ll also need the Drain-Source Resistance from the ‘Electrical Characteristics’ table, along with the On-Resistance Variation vs Temperature and Current Drain vs Case Temperature graphs. You may notice in the photographs that our build design does not have a heatsink - it actually does, but was omitted where possible for photographic clarity, showing as much of the board as possible, and the device temperature monitored. Heatsinks are installed for permanent use.

If driving inductive loads such as a motor or coil, a flyback diode must be included to handle the back-EMF when power is removed (either at shut-down or during switching). The device will not handle this at all without the diode, and failure is nearly certain. There is a diode depicted in the datasheet schematic of many modern devices, but it is there for static protection, and there is no guarantee it can handle the current involved in back-EMF. Capacitive loads also need special consideration, but are rare in maker circuits.

A note on pin assignments

As a general rule, TO220 packaged MOSFETs have a common pin order. When looking at the printed front face, left to right are gate, drain, and source. Many of the datasheets consulted in writing this, in fact, do not show the pinout. However, some exceptions exist, and other packages may have other conventions or no convention at all. Always check the datasheet, but if your device is a TO220 package and no pinout is listed, it is likely standard.

It is best to verify this information before you purchase a MOSFET, but if you already have one and are not sure of its pinout, you really have two choices: Buy another, or try it and see. If you try it and destroy it, you're no worse off.

Example Build:

Analogue RGB LED Strip ControlleR

To demonstrate the use of MOSFETs with an Arduino, we decided to solve a problem we had in the office. Our sign is illuminated with analogue RGB LED strip, and the controller that comes with it does not have the fixed colour we want for our display. While there are plenty of digital options today, with control protocols like WS2812B, these are all 5V supplied by their nature. This presents problems for long runs, as the current required through the thin PCB tracks becomes significant. By running the LED strip at 12V, the current is more than halved for the same power consumption and brightness. The series-parallel arrangement used is also more efficient than individual LEDs with one resistor each. There are three resistors visible on the strip of three LEDs, but they are for each colour, so brightness can be matched and the specifications of each separate-coloured die on the chip can be met.

This system relies on a common positive supply, with each colour in the LED chips separately grounded. This means that the brightness of each colour can be varied by using a PWM control. As we’re switching the load via its ground, this is ideal territory for an N-channel enhancement-mode MOSFET. Our design will use three MOSFETs, one for each colour, as well as three potentiometers to vary the colours. It will be built around an Arduino Uno.


Start by laying out the flat components: The resistors and the wire links. Of note, the ground rail in the middle of the board is linked to the upper ground rail, the one closest the potentiometers. The three 240R resistors straddle the middle power rails, while the 100kR resistors go in the same row, ending in the upper ground rail.

We found it helpful to mark the position of the pots, transistors, and screw terminals with marker before we assembled any components. The wire links we used are the same ones used on solderless breadboards, and the colour indicates the length, not the function such as power or ground connection. In other words, you may have a red wire connecting to ground because of its length. If this is the case, we recommend you mark the red wire with a black marker.

There are also six uninsulated links. One connects each MOSFET's source to the centre ground rail, while one connects one end of each potentiometer to the +5V rail.

Parts Required:JaycarAltronicsCore Electronics
1 x Prototyping BreadboardHP9570H0701^ADA1609^
1 x Pack of Breadboard Wire LinksPB8850P1014ACE05631
9 x Plug to Plug Jumper WiresWC6024P1022PRT-12795
3 x 240Ω Resistors*RR0557R7543CE05096
3 x 100kΩ Resistors*RR0620R7606CE05096
3 x 10K Linear 16mm PotentiometersRP7510R2243ADA562
2 x 2-screw Terminal BlocksHM3172P2034APOLOLU-2440
1 x Arduino Uno or Compatible BoardXC4410Z6280A000066
3 x Clip-On Heatsinks#HH8503H0600FIT0191
1 x Red Potentiometer KnobHK7705H6020ADA2047
1 x Blue Potentiometer KnobHK7709H6022ADA2047
1 x Green Potentiometer KnobHK7711H6028ADA2047
2 x PCB PinsHP1250H0804A-
3 x IRLZ44N MOSFETsElement 14 Part # 8651418

Parts Required:

* Quantity required, may only be available in packs.

# your heatsink requirements may vary depending on load. See text.

^ Not identical

Install the potentiometers next. The go on the upper side of the board when referring to the photos. Two PCB pins go to the far right near the centre. One connects to the centre ground rail, and the other to the row earmarked for the 12V screw terminal. It is also a good idea to mark the locations for the jumper wires from the circuit to the Uno now, as they are hard to locate after this.

One of these wires goes from each potentiometer wiper. Another goes from the junction of the 240Ω and 100kΩ resistors. Two join the centre ground and +5V rails to the GND and +5V pins on the Uno, and one more joins the 12V input from the screw terminal row to the Vin pin on the Uno.

You can now install the jumper wires from this board to the Uno.

Make a note of which colour goes where. In addition to the locations marked earlier, there are two wires to be installed in the centre power rails, one in each. This will supply the positive rail with +5V from the Uno’s onboard regulator, and tie the Uno to ground. The only other power connections are to the screw terminal, where one jumper wire is soldered to take 12V to the VIN terminal on the Uno.

You can now install the jumper wires from this board to the Uno. Make a note of which colour goes where. In addition to the locations marked earlier, there are two wires to be installed in the centre power rails, one in each. This will supply the positive rail with +5V from the Uno’s onboard regulator, and tie the Uno to ground. The only other power connections are to the screw terminal, where one jumper wire is soldered to take 12V to the VIN terminal on the Uno.

When installing the screw terminals, you may find that you need to drill out the holes in the PCB. We needed a 1.5mm drill bit. This comes close to cutting the track entirely, but because we’re using the very end of the track, you should still be able to gain a good solder connection to one side at least. The other side may have no copper left.

Finally, observing electrostatic discharge precautions, install the MOSFETs and solder them in. From here on out, be careful when handling the assembled board.

We used clip-on heatsinks for our build, as the MOSFETs are not going to be particularly heavily loaded. We would consider more rugged options if higher loads were to be drawn. You won’t see heatsinks in the photos, however, because we found that they obscured too much board detail. Now, you can upload the example (and very simple) code, and explore.


The code for this build is simple indeed and comes straight from the Arduino Basics 101 handbook. You can download it from our website to save you from typing it out.

It starts by defining a bunch of integers that can be called later in the code. These are labelled and the comments beside each explain them.

int ledOut1 = 3;     //assigns name ledOut1 to digital pin 3
int ledOut2 = 9;     //assigns name ledOut2 to digital pin 9
int ledOut3 = 10;    //assigns name ledOut3 to digital pin 10
int potIn1 = A0;     //assigns name potIn1 to analogue pin 0
int potIn2 = A1;     //assigns name potIn2 to analogue pin 1
int potIn3 = A2;     //assigns name potIn2 to analogue pin 2

Next down the list, in the setup section, the three digital pins used are defined as outputs. Many example codes skip this step, and in most simple codes, that’s fine. However, it’s good practice for later.

void setup() {
  pinMode(ledOut1, OUTPUT); //declares ledOut1 pin to an output
  pinMode(ledOut2, OUTPUT); //declares ledOut2 pin to an output
  pinMode(ledOut3, OUTPUT); //declares ledOut3 pin to an output

The heart of the code happens in the loop. Here, three nearly identical blocks of code control the brightness of the individual LED channels. The only difference between the blocks is the integers assigned, so that the three identical channels are controlled.

A delay of 100ms follows, as this is a ‘set and forget’ circuit where you will be making an adjustment, looking for a result, then leaving it set once you’re happy with the colour.

void loop() {
  int potValue1 = analogRead(potIn1);  
   // reads the voltage of pot 1 and stores
   // the value as an integer
  int brightnessLed1 = map(potValue1,0,1023,0,255);    
   // scales the integer value from above, which is
   // between 0 - 1023 to an integer between 0 - 255,
   // and assigns it to the integer brightnessLed1
  analogWrite(ledOut1, brightnessLed1);                    
   // writes the brightnessLed1 integer to
   // the ledOut1 pin in PWM form
   // program stops for 100 milliseconds
   // before proceeding to the next block


Now you should have a working RGB LED strip controller. Connect a 12V DC power supply to the PCB pins. The ground goes to the rail pin, while 12V DC goes to the terminal block pin.

Solder wires to your LED strip, for connection to the terminal blocks. Any wire that will carry the current will do. Screw the common power connection to the 12V terminal, and the other three in a right-to-left order. At this point, having loaded your code, you should be able to turn on the power and see some light from the LED strip. If you can’t, turn the potentiometers first.

If you still don’t have light, the next port of call is to check the jumper wires between the Uno and the circuit board. These are notorious for being unreliable, however, there is no practical alternative. In the longer term, a screw terminal or solder pad shield would be the best approach.

If the problem persists, disconnect power and check all of your circuit board connections carefully. In particular, make sure nothing has ended up in the wrong row or rail, and that all the wire links are in the right place.


From here, you could up the ante on the RGB lights, even going for spotlights. Hopefully, however, you can take the ideas here and use them in other power control applications.