Home · ← Blog posts

Robotics

Transistor as a Switch: How a Transistor Works, with an Arduino Motor Example

Probot Studio

What we'll do

  1. See what a transistor is and how it works as a switch.
  2. Find the E, B and C pins (two common parts have them in opposite order).
  3. Calculate the base resistor and try it in a simulator.
  4. Drive a motor from an Arduino safely, with a diode.

Why it's useful: an Arduino pin can only supply a few tens of milliamps, while a motor, relay or LED strip wants far more. A transistor lets that small pin signal switch the big current.

Arduino pin 9 connects through a 270 ohm resistor to the base of an NPN transistor, which opens the path between a 12 V battery and a motor. About 16 mA flows from the pin into the base and 150 mA from the battery through the motor

What is a transistor and what does it do?

A transistor is a three-legged semiconductor part that uses a small current to switch, or amplify, a much bigger one.

In short: a small current into the base lets a big current flow from collector to emitter.

How does a transistor work?

Water tap analogy: a thick pipe from collector to emitter has a flap inside; a small flow from the thin base pipe pushes it open. Below, three states: cutoff, active, saturation

In an NPN transistor, a small current (Ib) flowing from base to emitter opens the path between collector and emitter. The big current is roughly Ic ≈ β × Ib, where β (hFE) is the current gain.

State What happens As a switch
Cutoff Ib = 0, path closed Off
Active Path half open, the transistor heats up Avoid
Saturation Path fully open, VCE around 0.3 V On

As a switch you always want cutoff or saturation, nothing in between.

Transistor symbols and types (NPN, PNP, MOSFET)

Three symbols: the NPN emitter arrow points out, the PNP arrow points in; the N-channel MOSFET has gate G, drain D and source S

How do you find the E, B and C pins?

Two TO-92 transistors, flat face toward us, legs down: PN2222A is E, B, C from left to right; BC547 is C, B, E. The P2N2222A is C, B, E too

The pin order depends on the part and the maker. Flat face toward you, left to right:

If you are not sure, check the datasheet or test it with a multimeter (below).

How do you calculate the base resistor?

The base resistor (Rb) protects the pin and turns the transistor fully on. The rule:

Ib = Ic / 10                 (so it stays fully on)
Rb = (5 V - 0.7 V) / Ib

Example: a motor drawing 150 mA. Ib = 15 mA, Rb = 4.3 / 0.015 = 287 Ω. The next standard value down: 270 Ω.

Simulator: switching an LED, a motor and a relay

Try this: pick Motor, set the pin HIGH and drag the base resistor up to 10 kΩ. Watch the motor slow down and the heat in the transistor rise. Then untick the diode and set the pin LOW.

This is an NPN transistor simulator and base resistor calculator; it needs JavaScript. If it does not load, the formula above does the same math.

Driving a motor with a transistor and an Arduino

Wiring: Arduino pin 9 through 270 ohms to the PN2222A base; the motor between the battery positive and the collector; a 1N4001 diode across the motor with its stripe on the positive side; emitter, Arduino GND and battery negative joined

Parts: Arduino Uno, PN2222A, 270 Ω, 1N4001 diode, small DC motor (150 mA running current at most), a separate battery for the motor.

  1. Emitter to GND, base through 270 Ω to pin 9; motor between battery + and the collector.
  2. Diode across the motor, stripe toward +.
  3. Battery negative to Arduino GND.
// Motor speed with a transistor (Arduino Uno)
// Pin 9 -> 270 Ω -> PN2222A base. Motor between +V and the collector.
// 1N4001 diode across the motor: striped end to +V. Emitter and Arduino GND shared.
const int MOTOR_PIN = 9;  // PWM pin (marked ~)

void setup() {
  pinMode(MOTOR_PIN, OUTPUT);
}

void loop() {
  // Speed up slowly: 0 (stopped) ... 255 (full speed)
  for (int speed = 0; speed <= 255; speed += 5) {
    analogWrite(MOTOR_PIN, speed);
    delay(40);
  }
  delay(1000);           // 1 second at full speed
  // Slow down slowly
  for (int speed = 255; speed >= 0; speed -= 5) {
    analogWrite(MOTOR_PIN, speed);
    delay(40);
  }
  delay(1000);           // stop for 1 second
}

On the Uno, analogWrite switches the pin about 490 times a second (PWM); the share of on-time sets the speed. More on motors: How DC motors work

Why the diode? A motor is a coil. When the transistor switches off, the coil current cannot stop instantly, so it produces a high voltage spike. The diode gives that current a loop around the motor to die out in, protecting the transistor. The same goes for relay coils: What is a relay?

When a motor starts or stalls, it draws much more current than while running. Pick the transistor for that current.

Transistor vs MOSFET: what's the difference?

Comparison at a 500 mA load: the PN2222A needs 50 mA from the pin and heats up by about 500 mW; the logic-level IRLZ44N MOSFET draws pin current only while switching and heats up by about 6 mW

NPN (PN2222A) Logic-level MOSFET (IRLZ44N)
Driven by Base current (Ic/10) Gate voltage
When on VCE 0.3-1 V RDS(on) 0.025 Ω (VGS 5 V)
Load from an Arduino pin Up to about 200 mA (Ib at most 20 mA) Amps
Use it for LED, small motor, relay Big motor, LED strip

A logic-level MOSFET's datasheet gives RDS(on) at a gate voltage of 5 V or less.

How do you test a transistor with a multimeter?

Set the meter to diode test. For an NPN:

Red probe Black probe Good transistor
B E About 0.6-0.7 V
B C A slightly lower value
E or C B OL (no conduction)
C ↔ E both ways OL

The pair that reads slightly higher is B-E, which also tells you which leg is which. If any pair reads close to 0 both ways, the transistor is shorted. How to measure: How to use a multimeter

Common mistakes

Symptom Cause Fix
Motor does not turn at all Grounds not joined, or legs swapped Connect battery negative to Arduino GND, check the pin order
Transistor gets very hot Not fully on (Rb too big) or too much current Use the Ic/10 rule for Rb, or a MOSFET
Arduino keeps resetting Motor powered from the Arduino 5 V, no diode Separate motor battery, add the diode
Transistor died after a few tries No diode, voltage spike at switch-off Fit a 1N4001 across the motor, reverse biased
MOSFET module will not fully turn on at 5 V Not logic-level (e.g. IRF520: RDS(on) given only at 10 V) Pick a logic-level MOSFET

Sources

Accessed: 17 October 2026