Home / Learn / Electronics basics

How to use a transistor as a switch

New to this? Read from the top. Already know the basics? Jump to Going deeper for intermediate and advanced detail.

An NPN transistor switching an LED: a small base current from a microcontroller pin turns on a larger collector current
Beginner

Microcontroller pins can only supply a few milliamps. That's enough for a small LED, but not for a motor, a relay or a strip of lights. The answer is a transistor: a tiny current from the pin switches a much larger current from a separate supply.

Two kinds you'll meet

  • Bipolar transistors (BJTs), such as the BC547 or 2N2222. A small current into the base lets a larger current flow from collector to emitter. Simple and cheap, best for loads up to a few hundred milliamps.
  • MOSFETs. A voltage on the gate lets current flow from drain to source, with almost no current into the gate. Better for bigger loads, from hundreds of milliamps to many amps.

The low-side switch

+12 V Load Pin 1 kΩ NPN Diode 0 V
The transistor sits between the load and ground. The diode protects it when the load is a motor or relay.

The most common arrangement is a low-side switch: the load connects to the positive supply, and the transistor connects the other end of the load to ground when switched on. The load's supply can be higher than the microcontroller's, for example 12 V for a motor, as long as the grounds are connected together.

Choosing the base resistor (BJT)

A BJT needs enough base current to switch fully on, or "saturate". A transistor's gain (hFE) might be 100 or more, but to saturate it reliably the usual rule of thumb is to supply about a tenth to a twentieth of the load current into the base.

Worked example. A 3.3 V Raspberry Pi Pico switching a 50 mA load with a BC547 or 2N2222. Aim for a base current of 50 ÷ 20 = 2.5 mA. The base-emitter junction drops about 0.7 V, so R = (3.3 − 0.7) ÷ 0.0025 = 1,040 Ω. A 1 kΩ resistor gives 2.6 mA, which the pin can comfortably supply.

Check the transistor's maximum collector current in its datasheet. The BC547 is rated for 100 mA, and the 2N2222 for several hundred.

Using a MOSFET instead

For larger loads, an N-channel MOSFET is usually the better choice. The key thing to check is that it is a logic-level MOSFET that fully turns on at your microcontroller's voltage. In the datasheet, look for the on-resistance (RDS(on)) specified at a gate voltage of 2.5 V to 3.3 V for a 3.3 V board, or 4.5 V for a 5 V board. Many popular MOSFETs only turn on fully at 10 V, and will get hot if driven from a 3.3 V pin.

  • Connect the gate to the pin through a small resistor, around 100 Ω, to limit the brief charging current.
  • Add a 10 kΩ resistor from gate to ground, so the MOSFET stays off while the microcontroller is starting up.

The flyback diode: don't skip it

Motors, relays and solenoids contain coils. When you switch a coil off, its collapsing magnetic field produces a voltage spike that can easily destroy the transistor. A diode across the load, with its stripe (cathode) towards the positive supply, gives that energy a safe path. A 1N4007 or 1N4148 is fine for small relays. For larger motors, use a fast or Schottky diode rated for the motor's current.

Common mistakes

  • No shared ground. The load supply's ground must connect to the microcontroller's ground, or the transistor never switches properly.
  • No base resistor. Connecting a pin straight to a BJT's base draws too much current and can damage both.
  • A MOSFET that isn't logic-level, which only half turns on and overheats.
  • Powering motors from the board's 5 V pin. Use a separate supply. See powering your projects safely.
Intermediate and advanced

Going deeper

How hot will the MOSFET get?

When fully on, a MOSFET behaves like a small resistor, RDS(on), and dissipates P = I² × RDS(on). At 2 A with 50 mΩ, that's 0.2 W. At 10 A it's 5 W, which needs a heatsink. RDS(on) also rises as the MOSFET warms (often by 50% or more at high temperatures), so check the datasheet graph and leave margin.

Threshold voltage is not "fully on"

VGS(th), the gate threshold voltage, is where the MOSFET only just starts to conduct, usually at a tiny test current such as 250 µA. A part with a 2 V threshold may need 4.5 V or 10 V on the gate to reach its rated RDS(on). Always read RDS(on) at the gate voltage you'll actually apply.

Switching fast: gate charge and losses

The gate behaves like a capacitor that must be charged and discharged at each switch. During each transition the MOSFET is half on, with both voltage and current, which wastes power. At high PWM frequencies these switching losses can exceed the conduction losses. A microcontroller pin can only supply a small current, so for fast PWM of large MOSFETs, use a gate driver chip, which charges the gate in nanoseconds.

High-side switching

Sometimes the load must connect to ground and be switched on its positive side, for example when the load's ground is shared with something else. A P-channel MOSFET can do this: it turns on when its gate is pulled below its source. If the load supply is higher than your logic voltage, add a small NPN or N-channel transistor to pull the P-channel's gate down, because a 3.3 V pin can't turn it fully off. Integrated "high-side switch" chips are another tidy option.

BJT, Darlington or MOSFET?

  • A saturated BJT still drops about 0.1 to 0.3 V from collector to emitter, more at high currents.
  • A Darlington pair (such as each channel of a ULN2003) needs very little base current, but drops around 1 V or more, so it gets hotter than a MOSFET at the same current.
  • A logic-level MOSFET with low RDS(on) usually wastes the least power, which is why it's the default choice for loads above a few hundred milliamps.

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.