Skip to content
GEEKnodes

Electronics

Transistor as a Switch: NPN and MOSFET with Arduino and ESP32

How to drive a motor, relay or LED strip from an Arduino or ESP32 with an NPN transistor or a logic-level MOSFET, a flyback diode and a base resistor.

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
Low-side switch diagram: load between +V and the transistor, pin with a resistor to the base, and common GND
The transistor sits between the load and GND, and the supply’s GND is tied to the microcontroller’s 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

ModelMax. collector currentTypical use
BC547100 mASmall relays, buzzers, LEDs
2N2222 / P2N2222A600 mARelays, very small motors
TIP120 (Darlington)5 AMotors; 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.

  1. Collector current: Ic = 70 mA.
  2. 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.
  3. 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:

MOSFETPackageSpecified Rds(on)Suitable for
IRLZ44NTO-22028 mΩ at Vgs = 5 V (also specified at 4 V)5 V Arduino
AO3400ASOT-23 (SMD)Less than 48 mΩ at Vgs = 2.5 VESP32 and 3.3 V boards
IRFZ44NTO-220Specified at 10 VNot 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

CriterionNPNLogic-level MOSFET
Controlled byBase currentGate voltage
Loss when on~0.2 V fixed (Vce(sat))I² × Rds(on), very low
Comfortable currentUp to a few hundred mASeveral amps
Price and availabilityVery cheap, in every kitCheap, but you have to pick the right model
With fast PWMFineVery 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.

Flyback diode reverse-biased in parallel with a relay coil: cathode toward +V and anode toward the transistor's collector
Cathode (the stripe) toward +V: the diode absorbs the voltage spike when the coil turns off

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.

LoadCommon diode
Relay1N4007 or 1N4148
DC motor with PWMSchottky (1N5819, SS34): faster
Solenoid or electromagnet1N4007 or Schottky depending on current
LED stripNot 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:

  1. +12 V from the supply to the LED strip’s positive.
  2. Strip negative to the MOSFET drain.
  3. Source to the supply’s GND and to the Arduino’s GND.
  4. Pin 9 → 220 Ω resistor → gate.
  5. 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

MistakeSymptomFix
No common GNDThe load never turns onConnect the supply’s GND to the Arduino’s GND
NPN without a base resistorThe pin or the transistor gets damagedCalculate the resistor (560 Ω–1 kΩ is typical)
Non-logic-level MOSFET at 5 V or 3.3 VIt heats up and the load runs weaklyUse one with Rds(on) specified at your voltage
Motor or relay without a diodeThe transistor dies after a few switches, resetsReverse-biased diode in parallel with the load
Diode backwardsShort circuit at power-onStripe (cathode) toward +V
Mixed-up pinsIt doesn’t switch or it heats upCheck the pinout in your model’s datasheet

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor