sunnuntai 19. kesäkuuta 2016

Basics: FET as a switch


This time I'll talk about electronics basics, using transistors as switches.

Often you will encounter a situation where you want to control another device - be it I/O line, another chip, a high-power LED, a motor or whatever - with a MCU pin. Occasionally this can be done directly, without any external components, but often either the voltage of device driven exceeds the MCU's voltage (say you need to drive 12v device with 3.3v I/O) or current is too high (typical MCU pin can source about 10-30mA) or maybe you want the line to have a bit better protection (short circuit, ESD, component failure...) from failures in this device. In essence, you need a switch.

A mechanical relay might seem very tempting, as they are very easy to understand - they work like controllable mechanical switch. They however are bulky, slow(ish), expensive and power-hungry (that MCU current source limit again), so a transistor (these days a FET) is often the superior choice.

Transistors however require a bit more understanding to get them working properly. Here I concentrate on FETs - more specifically N and P channel enchantment mode FETs as they are common and fairly easy to use in your circuit, and whenever I mention FET or transistor below I mean these types specifically.

I won't go into details of FET operation - there are lots of resources available for that - and instead I give you just a few simple use cases and try to explain the reasons why design was done as it is.

FETs have (usually) three pins; Drain, Source and Gate. Gate is not directly connected to either Drain or Source and is used to control the amount of current passing through the transistor. When using N-channel FETs, current direction (in typical design) is from Drain to Source; in P-channel FET from Source to Drain.

Gate voltage is used to control resistance of the FET. When transistor is off, resistance between drain and source is very high and no current is passed through. When transistor is on, resistance is very small and current can pass through it. The resistance depends on exact gate voltage (to be more specific, gate-source voltage; the voltage difference between gate and source pins), but when used as switch you can mostly ignore this; in circuit transistor is either fully off or fully on.

In case of MOSFETs there typically is parasitic body diode that allows current to pass through the transistor in reverse direction (if, say, in case of NFET Source voltage is higher than Drain voltage). In some cases this may be a problem as it may allow your circuit to be powered in unexpected ways, but for simple cases it can be ignored.

Let's start with fairly common and simple case: toggling a string of LEDs on or off. LEDs need relatively large amount of current and high voltage to drive them; let's assume that LED string you have need +12v supply and has built-in current limiting (i.e. resistors) in them, so you don't have to worry about those details yourself.

This could be accomplished with following circuit: I've drawn LED string as resistor here.
BSS138 isn't greatest N-FET available as its current limit is relatively low, but for demonstration purposes it'll do (I just had it handily available in libraries).

Control signal is connected directly to MCU pin. R1 is series resistor used to limit current to gate when switching transistor on or off. Smallish value - like 1k - usually is acceptable. In some cases it can be omitted but I strongly recommend that you use one in your designs.

If FETs gate is unconnected (when MCU pin is in high impedance/input mode, as it usually is during reset and programming) FET is in essentially random state. For this reason you should always have a pull-down (NFETs) or pull-up (PFET) resistor; R2 in this case. Its value can be quite high; here I used 1M ohm. Don't count on MCU's pull-ups or pull-downs for this; they almost always are enabled with software and thus disabled when MCU is in reset.

Ratio of R2:R1 needs to be fairly large so that they won't act as voltage divider and bring the gate voltage down too much.

So what is happening in this circuit then?

Remember that I said that FETs resistance is controlled with gate-source voltage. Here source is connected to ground (naturally MCU must be connected to same ground here), so MCU pin's voltage is directly FET's gate-source voltage.

This is enchantment mode FET, so if gate voltage is zero, no current is passed through (resistance is high). In that case switch is essentially open/disconnected, no current is passed through LEDs and they're off.

When we bring control line high to say 3.3v, gate-source voltage grows past transistor's threshold voltage and current is allowed through, turning LEDs on.

Quite simple so far, yes?

Now, on FET selection then. When you open a FETs datasheet, there are huge amount of parameters. What of those are important when choosing one for this application?

One very important parameter is found in graphs; Drain current vs Gate to Source voltage (Vgs) . Some (older) FETs require higher Vgs, like 4 volts before they even start to turn on. If your MCU provides just 3.3v out, this obviously is impossible to reach. BSS138 however turns on below 2 volts and at 3.3v it is very close to maximum current already.

FETs may have fairly low maximum drain-source voltage, and if that is exceeded the FET can  be damaged. For this circuit maximum Drain-Source voltage in our circuit here can be assumed to be 12v when FET is off. Actual voltage is a bit smaller due to LEDs voltage drop but it's always safest to assume maximum voltage found on your circuit. So just make sure FETs Drain-Source voltage is larger than your circuits maximum voltage.

Maximum gate-source voltage of a FET can be surprisingly low. In this circuit Gate-Source voltage is (assuming 3.3v MCU) 3.3v, which just about any FET can handle easily.

Drain current for BSS138 (in SOT-23 package) is just 0.22A; like I said, not best transistor there is, but it can drive some LEDs. If you were dealing with high-current LEDs, you'd want a FET with large current capacity.


At the next part; what if you want the switch to be on the positive side of switched circuit?



1 kommentti: