The Core Answer: What a Transistor Actually Does
At its most fundamental level, a transistor acts as a current-controlled valve. In a standard Bipolar Junction Transistor (BJT), a small current flowing into the Base terminal dictates how much larger current can flow from the Collector to the Emitter. If you are asking what does transistor do in a circuit, the practical answer is that it allows a low-power, fragile signal (like a 3.3V microcontroller GPIO pin) to safely switch a high-power, high-voltage load (like a 12V motor or relay) without electrically connecting the two circuits.
Unlike a mechanical relay that uses an electromagnet to pull a physical metal contact, a solid-state transistor uses semiconductor physics to modulate electron flow. When the base current is zero, the transistor is in cutoff (an open switch). When the base current is high enough, the transistor enters saturation (a closed switch), dropping only about 0.2V across its collector-emitter junction.
Topology Deep-Dive: The Common-Emitter NPN Switch
To understand the transistor in action, we use the Common-Emitter (CE) topology. In this configuration, the Emitter is tied to ground (common), the load sits between the positive supply and the Collector, and the control signal enters the Base.
In an emitter follower, the load is placed on the emitter side. The problem? The emitter voltage will always be about 0.7V lower than the base voltage. If you drive the base with 3.3V, the load only sees 2.6V, and the transistor never fully saturates, wasting power as heat. The common-emitter topology puts the load on the collector, allowing the transistor to pull the collector voltage all the way down to ~0.2V (ground), delivering nearly the full supply voltage to the load.
Node Labels for our CE Switch:
- VCC: The high-voltage supply (e.g., 12V) powering the load.
- Load (Rc): The relay coil or motor connected between VCC and the Collector.
- Collector (C): The transistor terminal that 'pulls' current through the load.
- Base (B): The control terminal, fed through a current-limiting resistor.
- Emitter (E): Tied directly to the shared system Ground (GND).
Design Walkthrough: Driving a 12V Relay with a 3.3V ESP32
Let's build a real circuit. We want to switch a 12V Omron G5V-2 relay (coil resistance ~400Ω) using a 3.3V GPIO pin on an ESP32. We will use a standard PN2222 NPN transistor.
1. Calculate Collector Current (Ic):
Using Ohm's Law, the relay coil draws: Ic = 12V / 400Ω = 30mA.
2. Determine Required Base Current (Ib):
The PN2222 has a typical DC current gain (hFE) of 100 to 300. However, to guarantee the transistor acts as a hard switch (saturation) and doesn't overheat, we force a 'beta' of 10.
Ib = Ic / 10 = 30mA / 10 = 3mA.
This 3mA draw is well within the ESP32 GPIO absolute maximum rating of 40mA.
3. Calculate the Base Resistor (Rb):
The ESP32 outputs 3.3V. The transistor's Base-Emitter junction drops about 0.7V. The voltage across the resistor is 3.3V - 0.7V = 2.6V.
Rb = 2.6V / 3mA = 866Ω.
The nearest standard E12 resistor value is 1kΩ. Using 1kΩ yields a base current of 2.6mA, which is perfectly sufficient to saturate the 30mA load.
A relay coil is an inductor. When the transistor turns off, the collapsing magnetic field generates a massive reverse voltage spike (inductive kickback) that will instantly punch through the PN2222's collector-emitter junction, destroying it. You must place a 1N4148 or 1N4007 diode in reverse bias across the relay coil (cathode/stripe to 12V, anode to Collector) to safely route this spike back into the supply.
Behavior Matrix and Failure Extremes
Understanding how transistors behave under stress is what separates hobbyists from designers. Here is what happens when circuit parameters shift or fail.
| Condition / Change | Circuit Behavior | Result / Consequence |
|---|---|---|
| Base Resistor (Rb) increases to 10kΩ | Base current drops to 0.26mA | Transistor exits saturation, enters active/linear region. Vce rises, transistor dissipates heat, relay may chatter or fail to pull in. |
| VCC increases to 24V | Collector current doubles to 60mA | Relay coil overheats and burns out. Transistor stays saturated but must dissipate slightly more power. |
| Base Resistor shorts (0Ω) | Base current spikes to ~75mA | ESP32 GPIO pin exceeds 40mA limit and permanently burns out the microcontroller silicon. |
| Flyback diode is omitted | Inductive spike hits Collector | Vce exceeds the PN2222's 40V breakdown voltage. Transistor fails short-circuit; relay stays permanently on. |
What breaks at the extremes?
- Open Base: If the base connection breaks, Ib = 0. The transistor cuts off entirely. The Collector node floats up to VCC (12V). The relay turns off safely.
- Short Collector-to-Emitter: If the transistor's internal silicon melts and shorts C to E, the relay receives 12V continuously. The ESP32 loses all control over the load, creating a severe safety hazard if the relay controls a heater or motor.
Step-by-Step Breadboard Verification
Before soldering or deploying this to a PCB, verify the logic on a breadboard.
- De-energize: Ensure both the 12V supply and the ESP32 are completely powered off and disconnected.
- Place the Transistor: Insert the PN2222 into the breadboard. With the flat side facing you, the pins from left to right are Emitter (E), Base (B), and Collector (C).
- Wire the Emitter: Run a jumper from the Emitter pin to the breadboard's ground rail. Connect the ESP32 GND and the 12V supply GND to this same rail (establishing a common ground).
- Install the Base Resistor: Insert a 1kΩ resistor. Connect one leg to the Base pin, and the other leg to a jumper leading to your chosen ESP32 GPIO pin (e.g., GPIO 25).
- Wire the Load and Diode: Place the 12V relay on the board. Connect the relay's Coil+ to the 12V supply. Connect Coil- to the Collector pin. Place the 1N4148 diode across the coil, ensuring the silver stripe (cathode) points toward the 12V supply.
- Verify with a Multimeter: Set your meter to continuity/diode mode. Check that the diode reads open in one direction and ~0.6V in the other. Ensure no shorts exist between VCC and GND.
- Power and Test: Power the 12V supply first, then the ESP32. Drive the GPIO HIGH. You should hear the relay click, and measuring Collector-to-Ground should read roughly 0.2V (saturation).
Frequently Asked Questions
What does a transistor do in a circuit compared to a mechanical relay?
While both act as switches, a mechanical relay provides total galvanic isolation between the control circuit and the load, and can switch AC or DC equally well. A transistor is solid-state, meaning it switches millions of times faster, operates silently, and draws vastly less control current. However, a standard BJT transistor can only switch DC, and the control circuit shares a common ground with the load unless you add an optocoupler.
Why does my transistor get hot when switching a motor?
If your transistor is hot to the touch, it is operating in the active (linear) region rather than the saturation region. This happens when the base current is too low to fully turn the transistor on. The transistor is acting like a partial resistor, dropping several volts across its Collector-Emitter junction and burning that voltage as heat (Power = Vce × Ic). Decrease your base resistor value to force more base current and drive the transistor into hard saturation, where Vce drops to ~0.2V and heat generation becomes negligible.
Can I use a MOSFET instead of a BJT for this circuit?
Yes, and for high-current loads, you absolutely should. A BJT is current-controlled and requires continuous base current to stay on. A MOSFET (like the 2N7000 for small loads or an IRLZ44N for high power) is voltage-controlled. Once the gate capacitor is charged, it draws virtually zero continuous current from your ESP32 GPIO. However, for a simple 30mA relay coil, the PN2222 BJT is cheaper, easier to drive directly from 3.3V logic without worrying about gate threshold voltages, and takes up less board space.






