Learn / Semiconductors / Transistors and MOSFETs

Using a Transistor as a Switch

ReviewedBeginnerFreeAbout 6 min
In short

Using a Transistor as a Switch: the short answer

A microcontroller pin can safely supply only a few milliamps (about 20 mA on an Arduino Uno and less on an ESP32). Relays, buzzers, motors and long LED strings need more. Example: A 560 Ω base resistor and a flyback diode switch the relay reliably. From a 3.3 V pin the same maths gives about 330 Ω.

Level
Beginner
Time
About 6 min
Topic
Semiconductors
You will learn to

Switch a relay, buzzer or LED strip from a microcontroller pin with an NPN transistor, and pick the base resistor.

Before this: Digital Inputs and Outputs, Diodes, LEDs & Polarity

A microcontroller pin can safely supply only a few milliamps (about 20 mA on an Arduino Uno and less on an ESP32). Relays, buzzers, motors and long LED strings need more. An NPN transistor such as the BC547 or 2N2222 lets a small base current control a much larger collector current. Used as a switch, the transistor is either fully off or fully on (saturated).

Wire it like this. The emitter goes to ground. The load goes between the positive supply and the collector. The microcontroller pin drives the base through a resistor. When the pin goes HIGH, current flows into the base, the transistor turns on, and current flows through the load to ground. The load can use a higher voltage than the microcontroller, for example a 12 V buzzer switched from a 5 V or 3.3 V pin, as long as the grounds are connected.

To pick the base resistor, find the load current and the transistor's current gain (hFE, often 100 or more). For a solid switch, give it about ten times more base current than the bare minimum, i.e. aim for a base current of about one tenth of the collector current. The base drops about 0.7 V, so Rb = (Vpin − 0.7) ÷ Ib. With a relay, motor or other coil, always add a diode (such as a 1N4148 or 1N4007) across the load, pointing towards the positive supply, to absorb the voltage spike when the transistor switches off.

Base current controls the switch

An NPN transistor lets a larger current flow from collector to emitter when a smaller current flows into its base. To use it as a switch you drive it hard enough to saturate, so the voltage across it falls to a few tenths of a volt. As a rule of thumb, supply a base current of about a tenth of the collector current, not the full gain figure.

For a 100 mA load, aim for about 10 mA into the base. With a 5 V pin and roughly 0.7 V across the base-emitter junction, the base resistor is (5 − 0.7) ÷ 0.01, about 430 Ω, so a 470 Ω or 330 Ω resistor is a sensible choice.

Where to put the load

For a simple NPN switch, put the load between the supply and the collector, and the emitter to ground. Switching on the ground side is the usual way. For inductive loads such as a relay or a motor, add a flyback diode across the load, with the stripe towards the positive side.

A small transistor like the 2N2222 or BC547 handles a few hundred milliamps. For more current, use a power transistor or a MOSFET.

Check the heat

In saturation the transistor drops a few tenths of a volt, so the power is small. If it is not driven hard enough, it stays in its linear region, drops a lot of voltage and gets hot. Test by feeling for heat only at low power, and measure the collector-emitter voltage, which should be well under a volt when on.

Common mistakes
  • Using no base resistor and burning out the pin or the transistor.
  • Calculating the base current from the gain and under-driving the switch.
  • Leaving out the flyback diode on a relay or motor.
Key formulas
Ib = Ic ÷ hFE
least base current
Rb = (Vin − 0.7) ÷ (k × Ib)
base resistor with overdrive k of 3 to 10

Work it with your own numbers: Transistor base resistor · BJT bias

Worked example

Base resistor for a 5 V relay coil drawing 70 mA

  1. Collector current Ic = 70 mA. Aim for Ib ≈ Ic ÷ 10 = 7 mA for hard saturation.
  2. From a 5 V Arduino pin: Rb = (5 − 0.7) ÷ 0.007 ≈ 614 Ω.
  3. Choose the nearest standard value below: 560 Ω, giving about 7.7 mA, well within the pin's limit.
  4. Add a 1N4007 across the coil with its stripe to +5 V.

Result. A 560 Ω base resistor and a flyback diode switch the relay reliably. From a 3.3 V pin the same maths gives about 330 Ω.

Try it

Switch a 5 V relay module coil or a 12 V buzzer with a BC547 from an Arduino pin. Calculate the base resistor, add the flyback diode for the relay, and measure the voltage between the collector and emitter when on (it should be under 0.3 V).

Check yourself

3 quick questions

  1. In a low-side NPN switch, where does the load go?

  2. Why add a diode across a relay coil?

  3. A load draws 100 mA. Roughly what base current gives a solid switch?

Common questions

What will I learn in "Using a Transistor as a Switch"?

Switch a relay, buzzer or LED strip from a microcontroller pin with an NPN transistor, and pick the base resistor.

How do I base resistor for a 5 V relay coil drawing 70 mA?

Collector current Ic = 70 mA. Aim for Ib ≈ Ic ÷ 10 = 7 mA for hard saturation. From a 5 V Arduino pin: Rb = (5 − 0.7) ÷ 0.007 ≈ 614 Ω. Choose the nearest standard value below: 560 Ω, giving about 7.7 mA, well within the pin's limit. Add a 1N4007 across the coil with its stripe to +5 V. A 560 Ω base resistor and a flyback diode switch the relay reliably. From a 3.3 V pin the same maths gives about 330 Ω.

In a low-side NPN switch, where does the load go?

Between the positive supply and the collector. The emitter is grounded and the load sits above the collector.

Why add a diode across a relay coil?

To absorb the voltage spike when the current is switched off. The coil's inductance produces a spike that can destroy the transistor.

A load draws 100 mA. Roughly what base current gives a solid switch?

About 10 mA. About one tenth of the collector current keeps the transistor saturated.