Lesson 1 of 2 in Step 4 · Switching and timing

Using a transistor as a switch

By Mihai Dumitru · · · 7 min read

On this page

A microcontroller pin, like the ones on an Arduino, can only supply about 20 mA. A small motor, a lamp or an LED strip often needs a few hundred milliamps. A transistor solves this: a small current into one of its pins controls a much larger current through the other two. This page explains how that works and how to choose the one resistor it needs. It uses Ohm's law and refers to diodes.

The NPN transistor

A transistor has three legs. On the common NPN type they are called the collector, the base and the emitter. The main current flows from the collector to the emitter. The base is the control input: a small current flowing into the base and out of the emitter allows a much larger current to flow from the collector to the emitter.

The base and the emitter behave like a diode. Before any base current flows, the base has to be about 0.7 V higher than the emitter, the same forward voltage as an ordinary silicon diode.

Transistor switch calculator

Work out the base resistor for a transistor that switches a motor, a lamp or an LED strip from a small control pin.

+load820 Ω5 V
Use this resistor (rounded down)
820 Ω
Exact resistor
860 Ω
Base current needed
1 mA
Real base current
5.24 mA

What hFE means

hFE is the transistor's current gain, the ratio between the collector current and the base current. If hFE is 100, a base current of 1 mA allows up to about 100 mA through the collector. The name comes from the way engineers model transistors, and you can treat it simply as "how many times the base current is multiplied".

The important detail is that hFE is not a precise number. It varies from one transistor to the next, even of the same type, and it changes with temperature and with the current. Datasheets therefore give a range. A BC547, for example, can have an hFE anywhere from about 110 to 800, depending on its version. For a switch, always use the lowest value from the datasheet, so the circuit works with the weakest transistor you might get. 100 is a safe value to start with for common small transistors.

Wiring it as a switch

  • The load (motor, lamp, LED strip) goes between the plus of its supply and the collector.
  • The emitter goes to ground.
  • The control pin connects to the base through a resistor.

When the pin goes high, a small current flows into the base, the transistor lets the load current through, and the load turns on. When the pin goes low, the base current stops and so does the load current.

Calculating the base resistor

  1. Find the load current. Here, 100 mA.
  2. Divide by the minimum hFE to get the smallest base current that could work: 100 mA ÷ 100 = 1 mA.
  3. Multiply by about five. This extra base current makes sure the transistor turns fully on even if its hFE is lower than expected: 5 mA.
  4. Apply Ohm's law to the base resistor. It gets the pin voltage minus the 0.7 V of the base: (5 − 0.7) ÷ 0.005 = 860 Ω.
  5. Round down to a standard value: 820 Ω.

Here the rounding goes the other way than for an LED. A slightly smaller base resistor means slightly more base current, which keeps the transistor firmly on. From a 3.3 V pin the same load needs (3.3 − 0.7) ÷ 0.005 = 520 Ω, so 470 Ω, which draws about 5.5 mA from the pin. That is well within what a pin can supply.

Fully on

A transistor that gets enough base current is fully on, a state called saturation. Only about 0.2 V is then left between the collector and the emitter, so it barely warms up: 0.2 V × 100 mA is 20 mW. A transistor that gets too little base current is only partly on. Several volts remain across it and it turns far more power into heat. Most transistors that fail as switches fail this way.

Things that damage transistors

  • Too much current. A BC547 is rated for 100 mA, so keep well below that. A 2N2222 handles a few hundred milliamps. For currents of an amp or more, a logic-level MOSFET is the better part.
  • No flyback diode on a motor or relay. Switching a coil off produces a voltage spike, as described in the diodes guide. A diode across the coil, cathode towards plus, protects the transistor.
  • No base resistor. The base then draws too much current from the pin, which can damage both the pin and the transistor.
  • Swapped legs. Different packages put the collector, base and emitter in a different order. Check the pinout in the datasheet.

Check yourself

  1. A transistor with a minimum hFE of 50 switches a 200 mA load. What is the smallest base current that could work, and what would you use?

    Show the answer

    200 mA ÷ 50 = 4 mA is the minimum. With the usual factor of about five, aim for 20 mA.

  2. In the example from this page, why do you round the base resistor down rather than up?

    Show the answer

    A smaller base resistor gives slightly more base current, which keeps the transistor firmly in saturation. For an LED resistor you round up, because there less current is the safe side.

  3. A saturated transistor carries 100 mA. How much power does it turn into heat?

    Show the answer

    Only about 0.2 V is left between collector and emitter: 0.2 × 0.1 = 0.02 W, or 20 mW, so it barely warms up.

  4. Why does a transistor with too little base current get hot?

    Show the answer

    It is only partly on, so several volts remain across it while the load current flows. Several volts times the load current is far more power than in saturation.

Read next