An Arduino or ESP32 pin can only supply a few milliamps: enough for an LED, but not for a motor, a relay or an LED strip. The answer is to use a transistor as a switch. In this guide you’ll learn how to choose between an NPN and a MOSFET, how to calculate the base resistor and how to protect the circuit with a flyback diode.
In short
- The microcontroller pin only turns on the transistor; the load current comes from another supply.
- NPN (BC547, 2N2222): cheap and simple for loads up to a few hundred mA. Needs a base resistor.
- Logic-level MOSFET (IRLZ44N at 5 V, AO3400A at 3.3 V): for currents of several amps, with almost no losses.
- Every load with a coil (motor, relay, solenoid) needs a flyback diode in parallel.
Why not connect the load straight to the pin
Arduino Uno pins have an absolute maximum of 40 mA, and it’s best not to go above 20 mA. A small 5 V motor can draw several hundred mA, and much more when it starts.
On top of that, many loads run at a different voltage: a 12 V LED strip, a 6 V motor, and so on. The transistor lets a 5 V or 3.3 V signal control a circuit with a different voltage and current.
General layout: low-side switch
The most common setup places the transistor between the load and GND (low-side switch):
+V load (5 V, 12 V…)
│
[LOAD]
│
├─── collector (NPN) or drain (MOSFET)
│
pin ─[R]── base / gate
│
└─── emitter (NPN) or source (MOSFET)
│
GND ← shared with the Arduino GND

The golden rule: the GND of the load’s power supply and the GND of the microcontroller must be connected. Without a common ground, the transistor has no reference and won’t switch.
Option 1: NPN transistor
An NPN conducts between collector and emitter when current flows into the base. To use it as a switch you have to drive it into saturation: give it more base current than strictly needed so it’s “fully on”.
Common NPN transistors
| Model | Max. collector current | Typical use |
|---|---|---|
| BC547 | 100 mA | Small relays, buzzers, LEDs |
| 2N2222 / P2N2222A | 600 mA | Relays, very small motors |
| TIP120 (Darlington) | 5 A | Motors; but it drops more voltage (1–2 V) and gets hot |
Calculating the base resistor
It takes three steps. Example: a 5 V relay whose coil draws 70 mA, driven by a 2N2222 from an Arduino Uno.
- Collector current:
Ic = 70 mA. - Base current for saturation: for switching, use a forced gain of 10 (even if the datasheet says 100 or more), so
Ib = Ic / 10 = 7 mA. - Resistor: the base-emitter junction drops about 0.7 V.
R = (Vpin - Vbe) / Ib
R = (5 V - 0.7 V) / 0.007 A ≈ 614 Ω → standard value: 560 Ω
With 560 Ω the base current ends up at about 7.7 mA, well within what the pin can supply.
With an ESP32 (3.3 V) the math changes:
R = (3.3 V - 0.7 V) / 0.007 A ≈ 371 Ω → standard value: 330 Ω
For small loads (under 50 mA), a 1 kΩ resistor is usually enough in both cases. If you need a refresher on the formula, check out Ohm’s law with a calculator.
Option 2: logic-level MOSFET
An N-channel MOSFET conducts between drain and source when the voltage between gate and source (Vgs) exceeds a certain value. The gate draws practically no current at rest, and when it’s on its resistance (Rds(on)) is just thousandths of an ohm.
The “logic-level” trap
Many popular MOSFETs, such as the IRF540N or the IRFZ44N, are designed for Vgs = 10 V. With an Arduino’s 5 V they only half turn on and heat up; with 3.3 V, barely at all.
Check the datasheet to make sure Rds(on) is specified at your pin’s voltage:
| MOSFET | Package | Specified Rds(on) | Suitable for |
|---|---|---|---|
| IRLZ44N | TO-220 | 28 mΩ at Vgs = 5 V (also specified at 4 V) | 5 V Arduino |
| AO3400A | SOT-23 (SMD) | Less than 48 mΩ at Vgs = 2.5 V | ESP32 and 3.3 V boards |
| IRFZ44N | TO-220 | Specified at 10 V | Not recommended without a driver |
Notice the L in IRLZ44N versus IRFZ44N: it’s the logic-level version. Don’t rely on Vgs(th) (threshold) alone: that’s the voltage at which the MOSFET starts to conduct, not the one at which it conducts well.
MOSFET resistors
- Gate resistor (100–220 Ω) between the pin and the gate: limits the current spike when charging the gate capacitance.
- Pull-down resistor (10–100 kΩ) between the gate and GND: keeps the MOSFET off while the microcontroller boots and its pins are still floating. We explain it in pull-up and pull-down resistors.
NPN or MOSFET: which to choose
| Criterion | NPN | Logic-level MOSFET |
|---|---|---|
| Controlled by | Base current | Gate voltage |
| Loss when on | ~0.2 V fixed (Vce(sat)) | I² × Rds(on), very low |
| Comfortable current | Up to a few hundred mA | Several amps |
| Price and availability | Very cheap, in every kit | Cheap, but you have to pick the right model |
| With fast PWM | Fine | Very good |
Rule of thumb: NPN for relays, buzzers and single LEDs; MOSFET for motors, LED strips and any load over 0.5 A.
The protection (flyback) diode
Coils store energy in their magnetic field. When the current is cut suddenly, the coil tries to keep it flowing and generates a voltage spike that can far exceed the supply voltage. That spike destroys transistors.

The fix is a diode in parallel with the load and reverse-biased:
- Cathode (the stripe) toward +V.
- Anode toward the collector or drain.
In normal operation the diode doesn’t conduct. When switching off, it gives the coil current a path and absorbs the spike.
| Load | Common diode |
|---|---|
| Relay | 1N4007 or 1N4148 |
| DC motor with PWM | Schottky (1N5819, SS34): faster |
| Solenoid or electromagnet | 1N4007 or Schottky depending on current |
| LED strip | Not needed (it isn’t inductive) |
Many relay modules for Arduino already include the transistor and the diode; if you use a bare relay, you have to add them yourself.
Example: controlling a 12 V LED strip with a MOSFET
Wiring with an IRLZ44N and an Arduino Uno:
- +12 V from the supply to the LED strip’s positive.
- Strip negative to the MOSFET drain.
- Source to the supply’s GND and to the Arduino’s GND.
- Pin 9 → 220 Ω resistor → gate.
- 100 kΩ resistor between gate and GND.
const int TIRA = 9;
void setup() {
pinMode(TIRA, OUTPUT);
}
void loop() {
digitalWrite(TIRA, HIGH); // on
delay(1000);
digitalWrite(TIRA, LOW); // off
delay(1000);
}
Since pin 9 supports PWM, you can dim the strip with analogWrite(TIRA, 128). We explain it in PWM with Arduino and ESP32.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| No common GND | The load never turns on | Connect the supply’s GND to the Arduino’s GND |
| NPN without a base resistor | The pin or the transistor gets damaged | Calculate the resistor (560 Ω–1 kΩ is typical) |
| Non-logic-level MOSFET at 5 V or 3.3 V | It heats up and the load runs weakly | Use one with Rds(on) specified at your voltage |
| Motor or relay without a diode | The transistor dies after a few switches, resets | Reverse-biased diode in parallel with the load |
| Diode backwards | Short circuit at power-on | Stripe (cathode) toward +V |
| Mixed-up pins | It doesn’t switch or it heats up | Check the pinout in your model’s datasheet |



