A transistor as a switch
A GPIO pin can give you about 20 milliamps. A motor wants three hundred. A transistor is the part that lets a small current control a large one — and the base resistor is the one number you have to get right.
Turn the pin high, then drag the base resistor. There is a value above which the motor runs weak and the transistor cooks, and nothing on the bench tells you that is happening.
The idea, in one sentence
A small current into the base allows a much larger current from collector to emitter. Used as a switch, you are not interested in the ratio — you want the transistor fully on (saturated), because a fully-on transistor drops only about 0.2 V and therefore stays cool.
Half on is the dangerous state. It works, the load runs, and the transistor is dropping several volts at a few hundred milliamps — a third of a watt in a part the size of a lentil, which is enough to feel and enough to shorten its life.
Sizing the base resistor
Give the base a tenth to a twentieth of the load current. Datasheets promise a gain of 100 or more; ignore it. Deliberately over-driving the base is what guarantees saturation across temperature and across parts.
For 300 mA of motor and a 3.3 V pin:
- Aim for about 15 mA of base current (a twentieth of 300 mA).
- The base sits at ~0.7 V, so the resistor sees 3.3 − 0.7 = 2.6 V.
- R = 2.6 ÷ 0.015 = 173 Ω, so fit the 150 Ω below it, not the 220 Ω above.
Check the result against the pin: 15 mA is comfortable for an ESP32, which sources about 20 mA before it starts to sag. If the load needs more base current than the pin can give, you have outgrown a single transistor — use a MOSFET or a driver IC.
MOSFETs, briefly
A logic-level MOSFET is driven by voltage, not current, so the gate takes almost nothing to hold on and the on-resistance is milliohms instead of a 0.2 V drop. For anything over about half an amp, that is the right part. The word that matters when buying one is logic-level: an ordinary MOSFET needs 10 V on the gate and will only half-open on 3.3 V, which is the same silent overheating as above.
The next four pages are that part in full, starting with what a MOSFET is.
The four-part checklist
- Base resistor sized for a twentieth of the load current, not the datasheet gain.
- A flyback diode across anything with a coil in it — see diodes and flyback.
- A common ground between the two supplies.
- The load on its own supply if it is a motor, with a bulk capacitor, so it cannot brown out the board.
Edit this page — content/fundamentals/electricity/a-transistor-as-a-switch.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.