Transistors as switches: MOSFETs and BJTs from a microcontroller
How to use MOSFETs and bipolar transistors as switches from a microcontroller: low-side switching, logic-level MOSFETs, gate resistor and pull-down, base resistor and flyback diodes.
Why a transistor
A microcontroller pin can supply a few milliamps at 3.3 V. A motor, a relay, a solenoid or an LED strip needs hundreds of milliamps or amps, often at 12 V or more. A transistor lets the small signal control the large current.
The low-side MOSFET switch
The most common circuit: an N-channel MOSFET between the load and ground. When the gate goes HIGH, the MOSFET conducts and the load is on.
Three parts that matter
- Gate pull-down (about 100 kΩ): during reset the GPIO floats, and a floating gate can turn the load on at random.
- Gate resistor (about 100 Ω): limits the current spike into the gate and damps ringing.
- Flyback diode: an inductive load (motor, relay, solenoid) produces a large voltage spike when switched off. Without the diode it destroys the MOSFET.
The logic-level trap
Datasheets list Vgs(th), the gate voltage where the MOSFET barely starts to conduct. It is not the voltage where it is fully on. Look for the on-resistance Rds(on) specified at Vgs = 2.5 V or 4.5 V; if it is only specified at 10 V, a 3.3 V microcontroller will leave the MOSFET half-on, and it will overheat. Power dissipated: P = I² × Rds(on).
Bipolar transistors (BJT)
An NPN transistor works similarly for small loads, but it is driven by current. The base resistor sets that current: for a switch, aim for a base current of about one tenth of the load current (for example 10 mA load → 1 mA base → (3.3 − 0.7) / 1 mA ≈ 2.7 kΩ). For anything above a few hundred milliamps, a MOSFET is usually the better choice.
High-side switching, PWM at high frequency, several amps, motor control or anything where switching losses and heat matter is power electronics. It is our main speciality, and the place where a quick breadboard circuit most often turns into smoke.
Parts and tools to practise
| Item | What to look for | Buy |
|---|---|---|
| Transistor and MOSFET assortment | Include logic-level N-channel MOSFETs and small NPN/PNP transistors. | View on Amazon ↗ |
| Diode assortment | 1N4148 signal, 1N400x rectifier, Schottky and Zener values. | View on Amazon ↗ |
| Resistor assortment, 1 % metal film | E24 or E12 values from 10 Ω to 1 MΩ, 1/4 W. Sorted in labelled bags or a box. | View on Amazon ↗ |
| Solderless breadboard kit with jumper wires | Full-size breadboard, pre-cut jumpers and flexible Dupont wires. | View on Amazon ↗ |
| ESP32 or similar development board | To try pull-ups, ADC readings, buses and PWM for real. | View on Amazon ↗ |
| Adjustable bench power supply with current limit | 0 to 30 V, 0 to 5 A. The current limit saves boards on first power-up. | View on Amazon ↗ |
Links go to Amazon. As an Amazon Associate we earn from qualifying purchases, at no extra cost to you. We only list tools of the kind we use on our own bench.
Send us what you have. We tell you what it takes to finish it.
Answer five questions and see an indicative budget in two minutes. When you are ready, sign our NDA, upload your documentation and get a fixed quote. Everything by e-mail, with a call whenever you prefer.