What is a transistor? An electronic switch controlled by a small current
Atlasbot · 20/8/2026 · 10 min read
The GPIO pin can only output a maximum current of a few tens of mA — not enough to directly control a relay or motor that requires much larger current. Transistor solves exactly this problem: using a very small control current to open/close a much larger current, acting as an electronic switch controlled by digital signals.
Three pins of Transistor BJT
Bipolar Junction Transistor (BJT) is most common in simple control circuits with 3 pins:
- Base (B) — control pin, receives small current from GPIO (via current limiting resistor).
- Collector (C) — usually connected to the load side (e.g. one end of a relay coil, or a small motor).
- Emitter (E) — usually connected to GND (with NPN type, most common in control circuits from MCU).
There are 2 types of BJT: NPN (current flows from Collector to Emitter when Base has positive control current) and PNP (vice versa) — NPN is much more common in load control circuits with GPIO because of the "simple, forward" control logic.
How does a transistor work like an electronic switch?
The core idea: a very small current flowing into the Base pin "unlocks" a much larger current to flow between the Collector and Emitter. The ratio between Collector current and Base current is called gain current (hFE or β) — with common transistors like the 2N2222, this coefficient is usually several dozen to several hundred times.
In switch applications (different from analog signal amplification applications), the transistor only operates in two states:
- Cutoff (off) — no Base current → no Collector-Emitter current → equivalent to open switch (open circuit).
- Saturation (saturation) — enough Base current → maximum Collector-Emitter current allowed by the circuit → equivalent to a closed switch (almost short circuit).
In short: With GPIO indicating 20-40mA but the load requires a few hundred mA to several Ampere, the transistor is the bridge: GPIO controls a small current at the Base, the transistor automatically opens/closes a much larger current on the load side.
Technical details
Formulas
Two core formulas when designing a circuit using a transistor as a switch — calculating the maximum Collector current and calculating the required Base resistance:
Ic = hFE × Ib (Collector current = gain × Base current)
Rbase = (Vgpio − Vbe) / Ib (Base resistance, Vbe ≈ 0.7V)
For example: Ic = 100mA is needed for the relay to operate, the transistor has hFE = 100
Ib needs ≥ 100mA / 100 = 1mA (in fact, surplus should be provided, for example: 2-3mA to ensure saturation)
With 5V GPIO: Rbase = (5V − 0.7V) / 2mA = 2150Ω → choose 2.2kΩ
Parameters
Important parameters when choosing a BJT transistor for a load control circuit — taken from the datasheet, not a fixed number for all types:
| Parameters | Typical value |
|---|---|
| hFE (gain current) | 50 - 300 (depending on transistor, depending on working current) |
| Vbe when leading | ~0.7V (BJT silicon) |
| 2N2222 — Ic max | ~800mA |
| 2N2222 — Vce max | ~40V |
| TIP120 (Darlington, larger series) — Ic max | ~5A |
Practical applications
In addition to controlling a single relay, a BJT transistor is often used to control an entire row of status LEDs or warning buzzers on the robot control panel - each signal only requires 1 GPIO pin + 1 transistor + 1 Base resistor, the cost is much lower than using a specialized IC driver for such small power loads. With larger current loads (several Ampere or more, eg: robot's main motor), the MOSFET in the next article is often a more efficient choice than the BJT.
Practical example: NPN circuit controls relay from GPIO
GPIO ──[ Base resistor, eg 1kΩ ]── Base (NPN transistor)
VCC (relay power) ── Relay coil ── Collector
Emitter ── GND
+ Protection diode connected in parallel with the relay coil (see Diode article)
// MCU side code — just turn on/off 1 normal GPIO pin
digitalWrite(RELAY_CONTROL_PIN, HIGH); // Base has current → transistor conducts → relay turns on
digitalWrite(RELAY_CONTROL_PIN, LOW); // Base has no current → transistor is off → relay is off
Calculate Base resistance
The Base resistor needs to be small enough to bring the transistor into saturation, but large enough not to exceed the maximum withstand current of the GPIO pin - the same formula for calculating the LED resistor as mentioned in the Voltage/Current/Power article, the only difference is that the reference voltage drop is the Base-Emitter voltage (~0.7V for silicon BJT).
Common mistakes
- Forget the Base resistor — connect the GPIO directly to the Base pin without passing a resistor, causing the current to exceed the tolerance limit, possibly burning the transistor or burning the GPIO pin.
- Wrong pins B-C-E — each type of transistor has a different pin order depending on the shell shape (TO-92, TO-220...) — always look up the correct datasheet of the specific model, do not guess based on another transistor.
- Forget the protection diode when controlling a load with a coil — as mentioned in the Diode article, the relay/motor creates a reverse voltage pulse when the current is interrupted, which can destroy the transistor if there is no protection diode connected in parallel with the coil.
- Choose a transistor that does not have enough current/voltage tolerance — each transistor has a maximum Collector current limit (Ic max) and maximum Collector-Emitter voltage (Vce max) written in the datasheet — using a small signal transistor (eg: 2N2222, Ic max ~800mA) to control a larger current load will burn out the transistor.
Conclusion
The BJT transistor is the cheapest and simplest way to use digital signals from the GPIO to control a power load larger than the GPIO pin is capable of — combined with the correct Base resistor and protection diode. For loads that require even larger currents or need higher switching efficiency, MOSFETs (next article in this category) are often a more suitable choice.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.