There are a lot of options when it comes to using your favourite microcontroller (be it an Arduino, Pico, ESP32 or micro:bit) to switch something on and off. In this post I will compare some of the various options available.


Option 1. Direct connection to GPIO pin
It's quite common to connect LEDs (usually with a series resistor to limit the current) directly to the GPIO pin of a microcontroller. Pretty much any microcontroller will let you draw up to 5mA (enough for an LED to light) some (Arduino Uno for example) will let you draw tens of mA without any problem, and some like the ESP32 can be configured for really quite high currents (85mA) see my earlier post on this.
However, even datasheets can be vague about things like total current budgets for all GPIO pins. So, if you are making something for fun, by all means push the boundaries of your GPIO pins and in the worst case, be prepared to replace your microcontroller.
Using the GPIO pins directly also limits the voltage you can switch to the microcontroller's supply voltage and whatever you are switching will share that supply which could lead to problems with electrical noise.
If, on the other hand, you are making a product or need a robust reliable design for other reasons, then you should probably move on to the options that follow.
Option 2. Low-side MOSFET Switching
Here is the basic schematic for using an N-channel MOSFET as a 'low-side' switch. It's 'low-side' switching because the lower voltage end of the load is switched to ground (GND) allowing current to flow through the load powering the load as the current flows to ground through the MOSFET.
Low-side switching is more common than 'high-side' because the MOSFET is turned on and off using a voltage relative to ground rather than +V. So 'high-side' switching of voltages above the logic level would need an extra transistor, or specialised MOSFET gate-driver IC.
Once the voltage at the MOSFET's gate exceeds the threshold voltage (usually a couple of V) the MOSFET will conduct.
You may be wondering, about R1 and R2. Let's look at these in turn:
R1. Do you need a resistor R1 between the GPIO pin and the MOSFET's gate? Well, the thing is, the MOSFET's gate looks to the microcontroller like a capacitor to GND, in the schematic above, so when the voltage is first applied to the gate when the GPIO pin goes high, current will rush into the capacitor filling it up. A typical MOSFET might have a maximum equivalent capacitance less than 1 nA. Including a resistor slows the charging of the gate capacitance, reducing the peak current. Most GPIO pins are very tolerant of very short duration currents and in any case, there will be come resistance in the form of PCB tracks and wires between the GPIO pin and the MOSFET. So, while it is good practice to include R1 of perhaps 1kΩ. For hobby designs, you are unlikely to have problems if you omit R1 entirely.
R2. Which might typically have a value of 100kΩ pulls-down the gate to GND, keeping the MOSFET off. Again, the need for this may not be immediately obvious.
Imagine the situation where the MOSFET is controlling a high-power motor. If your microcontroller GPIO pin, rather than being an output, was configured by accident as an input, then the gate of the MOSFET would be 'floating'. Just touching the track connected to the gate would likely be enough to turn the MOSFET on. Most likely, your body would act as an antenna, picking up AC electrical noise at 50Hz or 60Hz depending on where you live. This will be turning the load on and off 50 times a second without any control from the microcontroller. Another scenario where the gate could be left floating would be where the MOSFET and microcontroller were on separate PCBs joined by a connector that might be unplugged to leave the gate floating. In any case, a 100kΩ resistor for R2 will make things safe, and will be easily overpowered by the GPIO pin when it goes high to turn the MOSFET on.
By now, some of you may be screaming "WHAT ABOUT BIPOLAR TRANSISTORS?". My answer is "Meh!". To elaborate on this a little. Yes you can, but when it comes to switching, I prefer to stick to one type of very cheap transistor, rather than two types of very cheap transistor. And for me that's an n-channel MOSFET.
The thing is, for low currents an NPN bipolar transistor is fine, but as the current you are switching increases, you need more current to turn them on and they start to get hot.
If you are using a MOSFET, switching is essentially voltage not current controlled and as the current you are switching gets higher, all you need to do is buy a MOSFET, for a few cents more, that has a lower on-resistance.
For example, the MOSFETs used in the popular MonkMakes
MOSFETTI board and the
Switch for micro:bit (see below) only cost a few cents each, but can switch 3A at 60V, with an 'on' resistance of just 0.1Ω they barely even get warm most of the time.
Referring back to the schematic, it is important to note that, although the MOSFET 'turns-on', it is not a pure switch. By 'turning on' we mean that it will allow a current to flow (confusingly) from the drain to the source, but NOT the other way around. So we have to be aware of the polarity of the supply voltage.
Option 3. Electromechanical Relay
A traditional relay is an
electromechanical device that uses an
electromagnet to actually move a pair
of contacts.
When a current of a few tens of mA flows through the coil, it
pulls the contacts together making an
electrical connection, just as if you had
flipped a switch.
The important thing here is that there is NO electrical connection between the coil
and the contacts.
The contacts are free to operate at a different voltage and switch
a completely different circuit to whatever the coil is controlled by (typically a GPIO pin of a microcontroller (using some extra circuitry)).
Electro-mechanical relays are cheap, but switch slowly and require quite a high
current through the coil to activate them. This means that generally you should use a MOSFET or bipolar transistor to switch the current to the coil. In fact you could use the MOSFET example above to drive the relay coil.
Low-cost relay modules such as the one shown below, have this extra circuitry built-in.
Option 4. Solid State Relay ICs
In many ways, it is amazing that electromechanical relays are still in use. For low voltage, low current applications, they can be replaced a a solid state relay IC. These chips behave just like an electromechanical relay, but without an moving parts.
Instead of a coil they have an LED, that is optically coupled to a pair of MOSFET phototransistors. So that when the LED is energised (only requiring a couple of mA) the MOSFETs turn on. The MOSFET pair are arranged in such a way as to be able to switch low-voltage AC or DC. The schematic above is take from the
MonkMakes Dual Relay, which has two such relay chips on a single board.
The advantage of a SSR over an electromechanical relay, is that you can use Pulse Width Modulation with an SSR to perhaps control the brightness of an LED lamp or the speed of a motor. It's not wise to try and do this with an electromechanical relay, as opening and closing the contacts rapidly wouldn't be fast enough for say dimming an LED lamp, and would soon ware-out the contacts.
Making a Choice
Use Option 1. Direct connection to GPIO pin -- if you are only switching a few mA of load and you know your microcontroller's GPIO can handle it, and you are switching a voltage that is the same as the microcontroller's supply voltage.
Use Option 2. Low-side MOSFET Switching -- if you are in control of the design of the load being switched and you know that it's ok for it to share a ground with your microcontroller. In other words, no electrical isolation between the microcontroller and load is needed.
Use Option 3. Electromechanical Relay -- if you do NOT need PWM and you do need electrical isolation between the microcontroller and the load. Also, electromechanical relays usually have both normally-open (N.O.) and normally-closed (N.C.) contacts, which can sometimes be useful.
Use Option 4. Solid State Relay -- if you might need PWM and need electrical isolation between the microcontroller and the load.