Two common ways of using a transistor is as an analog amplifier, or a digital switch. We are most interested in the digital switch mode of a transistor in our projects.
Many times, an Arduino, or other microcontroller, is not able to drive a load directly. We commonly will use a transistor to drive the load, and trigger it with a microcontroller’s output pin.
For MOSFET examples, see our companion article.
Lets use an example. Say we have a 20v dc motor, that requires 500ma of current at load.
The Arduino UNO can output 5v, at 40ma or less. Let’s use a 2N2222 transistor. From the spec sheet, we see it can handle 600ma continuously, so current requirements are met. We also see it can handle around 30vdc, so the voltage requirements are met. Looks like a good match.
Being a NPN transistor, the load attaches between the collector and the positive supply, the emitter connects to a common ground with the Arduino.
To prevent the transistor from pulling too much current from the Arduino, we need a resistor between the Arduino output pin, and the base of the transistor. The resistor must be sized to limit current to no more than 40ma, and to make sure the transistor “opens” fully under load. The more current drawn by the load, the more current is needed by the base.
Download spreadsheet with following calculations:
The 2N2222 has a DC gain of about 30, so the current of the load (500 ma) divided by the gain (30) means we need a base current of around 16ma.
If we assume a Arduino HIGH being around 4.5v, and the diode voltage of the transistor is 0.7v, we get a base voltage of 3.8v. The maximum size of the resistor to maintain a 500ma load will be 228 Ohms. We found this by taking the needed base current of 16ma and dividing by the base voltage of 3.8v (and multiplying by 1000). If you go higher, the transistor may not fully open, and the motor won’t perform properly. We could go as low at 100 Ohms without exceeding the max current of the Arduino pin (3.8v / 100 Ohms = 38ma), but there’s no need to draw the excess current, so keep the resistor value near the max of 228 Ohms to reduce Arduino power consumption, and leave capacity on other pins. The total current supply capability of the UNO is 200ma (per ground) across all the pins.