gwordal

Lesson 5 of 5 · 25 min

Transistors as switches

A microcontroller pin can supply about 20 mA. A small motor wants 500 mA, a relay coil 70 mA, a strip of LEDs several amps. The pin cannot power any of them, but it can control something that can: a transistor. A transistor is a tiny current or voltage at one terminal deciding whether a much larger current flows between two others. Used as a switch, it is the bridge between logic and the physical world.

Two kinds of transistor

Both kinds have three terminals and both do the same job here, but they are controlled differently.

BJT (NPN)MOSFET (N-channel)
TerminalsBase, collector, emitterGate, drain, source
Controlled byCurrent into the baseVoltage on the gate
Example parts2N2222, BC337IRLZ44N, logic-level types
Gate or base currentMilliamps, continuousAlmost zero, except when switching
On stateDrop of about 0.2 V (saturation)Small resistance Rds(on), often milliohms

In both, the load sits between the positive supply and the drain (or collector), and the source (or emitter) goes to ground. This is called a low-side switch: the transistor connects the bottom of the load to ground when on.

BJT as a switch

An NPN bipolar transistor behaves as a current amplifier: collector current is roughly Ic = beta * Ib, where beta (also called hFE) is typically 100 or more. As an amplifier this relation holds. As a switch we want something different: we want the transistor fully on, with as little voltage left across it as possible, because the power it wastes is P = Vce * Ic.

So we deliberately overdrive the base. Instead of trusting beta = 100, we assume a forced beta of about 10 to 20, which guarantees saturation, where the collector-to-emitter drop falls to about 0.2 V.

Base resistor calculation. We want to switch a 100 mA load from a 5 V pin, using a forced beta of 20.

  1. Base current needed: Ib = Ic / beta_forced = 100 mA / 20 = 5 mA
  2. The base-emitter junction is a diode that drops about 0.7 V, so the resistor drops 5 V - 0.7 V = 4.3 V
  3. Resistor value: Rb = 4.3 V / 5 mA = 860 Ω
  4. Pick the nearest standard value that gives at least 5 mA: 820 Ω, which gives 4.3 V / 820 Ω = 5.2 mA

That is within the pin's 20 mA budget. The transistor dissipates Vce * Ic = 0.2 V * 0.1 A = 20 mW, almost nothing. Without the base resistor, the pin would try to push current through a 0.7 V diode junction and be damaged.

MOSFET as a switch

A MOSFET gate is insulated from the channel, so it draws no steady current: the voltage on the gate relative to the source creates an electric field that opens the channel. When fully on the channel behaves like a small resistor, the on-resistance Rds(on), and the power lost is P = I^2 * Rds(on).

The key trap: Rds(on) is only quoted at a certain gate voltage. Many popular MOSFETs, such as the IRF540, need around 10 V on the gate to turn fully on, and at 5 V they are only half open and run hot. Choose a logic-level MOSFET whose datasheet specifies Rds(on) at a gate voltage of 4.5 V or below (and for a 3.3 V board, at 2.5 V or 3.3 V).

For a motor drawing 1 A and a logic-level MOSFET with Rds(on) = 0.03 Ω:

P = I^2 * R = 1^2 * 0.03 = 0.03 W

That is 30 mW, no heat sink needed. A BJT passing 1 A at 0.2 V would waste 0.2 V * 1 A = 0.2 W. That is why MOSFETs are preferred for larger loads.

Two small parts make the MOSFET circuit reliable:

  • A gate resistor of 100 to 330 Ω between pin and gate. The gate is a small capacitor (a few nanofarads), so each switching edge draws a brief spike of current. With 330 Ω the peak is 5 V / 330 Ω = 15 mA, within the pin's limit.
  • A pull-down resistor of 10 kΩ from gate to ground. While the microcontroller boots, its pins float, and a floating gate can turn the motor on at random. The pull-down holds the gate at 0 V until the code takes charge.

The flyback diode

A motor, a relay coil and a solenoid are all inductors: coils of wire. An inductor stores energy in a magnetic field while current flows, and it opposes any change in that current with a voltage:

V = L * dI/dt

When the transistor turns off, the current wants to stop in nanoseconds. Take a motor winding of L = 2 mH carrying 1 A, switched off in 100 ns:

dI/dt = 1 A / 100 x 10^-9 s = 10^7 A/s

V = 2 x 10^-3 H * 10^7 A/s = 20 000 V

In practice the voltage does not reach 20 000 V because something breaks first: the transistor's drain-source junction avalanches or an arc forms across the switch. A logic-level MOSFET rated for 55 V is destroyed long before. The inductor forces the voltage up until the current has a place to go.

The flyback diode (also called a freewheeling or snubber diode) gives it one. Connect it across the motor, cathode (the striped end) toward the positive supply and anode toward the transistor. In normal operation it is reverse-biased and does nothing. When the transistor switches off, the motor's voltage reverses, the diode conducts, and the current circulates harmlessly through the motor and the diode until the stored energy fades. The drain voltage is clamped to the supply plus the diode's 0.7 V, for example 5.7 V.

Use a 1N4007 for small, slow motors and relays, or a faster Schottky diode such as the 1N5819 for fast PWM. Rated current should exceed the motor's current.

Putting it together

Motor positive to the supply, motor negative to the MOSFET drain, source to ground, flyback diode across the motor, a 330 Ω gate resistor from pin 9 and a 10 kΩ pull-down from gate to ground. The motor supply and the Arduino share a common ground; without it the gate voltage means nothing.

const int MOTOR_PIN = 9;       // PWM pin to the MOSFET gate (via 330 ohm)

void setup() {
  pinMode(MOTOR_PIN, OUTPUT);  // pin drives the gate
}

void loop() {
  analogWrite(MOTOR_PIN, 255); // always on: full speed
  delay(2000);
  analogWrite(MOTOR_PIN, 128); // on half the time: about half speed
  delay(2000);
  analogWrite(MOTOR_PIN, 0);   // off: motor stops
  delay(2000);
}

Because a MOSFET switches in nanoseconds, it works naturally with PWM: the motor sees an average voltage proportional to the duty cycle, and the transistor stays either fully on or fully off, so it dissipates very little.

Check yourself

You switch a 150 mA load from a 5 V pin with an NPN transistor and a forced beta of 15. What base resistor is appropriate?

Check yourself

Why is a flyback diode placed across a motor driven by a transistor?