An Arduino or ESP32 pin can supply only a small current, a few milliamps safely (the exact limits are in each chip's datasheet). Enough to light an LED, yes, but not a relay, a motor or an LED strip. The classic solution: a transistor acting as a switch, controlled by the microcontroller with a small current.
The idea
In an NPN transistor:
- A small current flowing into the base...
- allows a much larger current to flow from the collector to the emitter.
- The load connects between the supply and the collector, and the emitter goes to ground.
When the pin is HIGH the transistor closes the circuit and the load runs. When it's LOW, the transistor switches off.
Calculating the base resistor
Example: a 12 V relay whose coil draws about 70 mA, a 2N2222 transistor, controlled from an ESP32 (3.3 V).
- Minimum base current: this transistor's gain (hFE) is at least about 100 at this current (from the datasheet). So Ib = 70 ÷ 100 = 0.7 mA.
- Add margin: to make sure the transistor goes into saturation (switches fully on and runs cooler), multiply by 2 to 5. Make it about 2.5 mA.
- The resistor: the base voltage is about 0.7 V, so Rb = (3.3 − 0.7) ÷ 0.0025 ≈ 1040 Ω → 1 kΩ (current ≈ 2.6 mA).
That's a comfortable current for a microcontroller pin.
A 10 kΩ resistor to ground
A resistor from base to ground (pull-down) makes sure the transistor stays off while the microcontroller is still booting or its pin is undefined. Without it, the relay may click by itself at power-up.
The protection diode: the most important part
A relay or motor coil stores energy. The moment the transistor switches off, the coil produces a very high reverse voltage, tens or even hundreds of volts, which is enough to destroy the transistor.
The fix: a diode (such as a 1N4007 or 1N4148) reversed across the coil: cathode towards +12 V, anode towards the collector. In normal operation it does nothing; at switch-off it safely dissipates the coil's energy. Most ready-made relay modules include this diode.
Which transistor?
The most common transistors you'll find in shops; figures are approximate, from the datasheets:
| Transistor | Max collector current | Max voltage | Notes |
|---|---|---|---|
| BC547 | 100 mA | 45 V | For LEDs and small signals |
| 2N2222 / PN2222 | 600 mA | 40 V | The favourite for relays and small loads |
| S8050 | 500 mA | 25 V | Found on many modules |
| TIP120 (Darlington) | 5 A | 60 V | High gain, but runs hot |
Never run at the maximum. Keep the actual current at half the maximum or less, because the figure in the table is where the transistor gets destroyed, not a comfortable working value. The pinout also differs between types (the BC547 isn't the same as the 2N2222, for example), so check the datasheet before soldering. Reading datasheets has its own article.
Checking it's fully on (saturation)
Once wired, run the load and measure the voltage between collector and emitter (Vce) with a multimeter:
- Below about 0.3 V: the transistor is saturated and working properly as a switch.
- 1 V or more: the transistor isn't fully on, the load isn't getting its full voltage, and the transistor is heating up. Lower the base resistor so the base current rises, or use a bigger transistor.
The heat in the transistor is roughly P = Vce × Ic. In the relay example: 0.2 × 0.07 = 14 mW, nothing at all. But if the same transistor drives 500 mA while not saturated, with 1 V across it, that's half a watt in a tiny TO-92 package, hot enough to burn your finger.
Example: a 12 V LED strip drawing 1 A
This example shows the limits of an ordinary transistor:
- A 2N2222 won't do: 1 A is above its limit.
- A TIP120 will drive it, but a Darlington drops about 1 to 2 V when on, so the heat = 1.5 × 1 ≈ 1.5 W. It needs a heatsink, and the strip gets 10.5 V instead of 12.
- A MOSFET like the IRLZ44N has an on-resistance of about 0.02 Ω, so the heat = 1² × 0.02 = 0.02 W, with no heatsink at all.
So from roughly half an amp upwards, a MOSFET is the easier choice.
Common mistakes
- No common ground: if the load runs from a 12 V supply and the Arduino from USB, the 12 V negative must connect to the Arduino's GND, otherwise the base current has no return path and the transistor won't work. Read what ground is and why it must be shared.
- No base resistor: the base-emitter junction is a diode, so connecting the pin directly makes the current rise until the microcontroller pin or the transistor burns out.
- Load on the emitter instead of the collector: that circuit is an emitter follower, and the emitter stays about 0.7 V below the base. So from a 3.3 V pin the load gets only about 2.6 V, whatever the supply voltage. The load belongs between the supply and the collector.
- Forgetting the diode: the transistor may work for days and then suddenly fail from the voltage spikes the coil produces.
When to use a MOSFET instead
A BJT is excellent for small and medium loads. But with large currents (amps), it heats up, because it drops about 0.2 V even when fully on. Here an N-channel MOSFET is better:
- Its on-resistance (Rds(on)) can be just milliohms, so it heats far less.
- The gate draws no steady current.
- But you must choose a logic-level MOSFET that turns fully on with 3.3 or 5 V on the gate, such as the IRLZ44N. The popular IRFZ44N needs about 10 V to turn on properly. Choosing one is covered in detail in the logic-level MOSFET article, and the MOSFET itself in what is a MOSFET.
What if the load must stay connected to ground?
In the method above (low-side) the transistor switches the load's negative. Most loads don't mind. But if the load is another device grounded elsewhere, or a sensor with signal wires, you need to switch the positive instead (high-side), which is done with a PNP transistor or a P-channel MOSFET. The difference and how to drive them from a microcontroller are in the NPN or PNP article.
Summary
- Load between the supply and the collector, emitter to ground.
- Rb = (Vpin − 0.7) ÷ (Ic ÷ hFE × 2 to 5).
- 10 kΩ from base to ground.
- A protection diode across any coil (relay, motor, solenoid).
- For large currents: a logic-level MOSFET.
If you want to understand why the current divides like this, revisit Ohm's law. We work with transistors and MOSFETs hands-on in the electronics course.
FAQ
Can I use a ready-made relay module instead of all this? Yes, a relay module has the transistor, diode and resistors built in. But there are details such as active-low inputs and JD-VCC, explained in the relay module article.
Can a transistor do PWM to control a motor's speed? It works at normal Arduino frequencies, but a MOSFET is better at high speeds and large currents, and in both cases don't forget the diode across the motor. See the PWM article.
How do I calculate all this quickly? The transistor base resistor calculator gives you the resistor, the pin current and the approximate heat in the transistor.