Master the Basics: How to use a BJT as a switch
Share
Controlling devices like motors or LEDs using a microcontroller (like an Arduino or Raspberry Pi) is a common task in electronics projects. But connecting something like a motor directly to a microcontroller’s output pin doesn’t work well. Why? The motor likely requires more voltage and current than the microcontroller can provide.
Why Use a Transistor as a Switch?
To safely and effectively control larger devices, we use a Bipolar Junction Transistor (BJT) as an electronic switch. A BJT can act like a valve controlled by a small electrical signal. It allows you to use the microcontroller's digital output to control a larger power source connected to a motor or other load.
NPN Low-Side Switch Setup
To use an NPN transistor (e.g., 2N3904) as a low-side switch:
- Connect the emitter to ground
- Connect the collector to the load’s ground side
- Connect the base to the digital output pin through a 1kΩ resistor
When the base is given ~0.7V or more:
- The transistor turns on (saturation mode)
- Current flows from collector to emitter, turning on the load (e.g., a motor)
When the base is low:
- The transistor turns off (cutoff mode)
- No current flows through the load
Add a flyback diode across inductive loads like motors to prevent damaging voltage spikes.
Example: Arduino + Motor + BJT
Let’s say:
- Arduino provides a 5V signal to the base
- Transistor is 2N3904, rated for 200 mA max collector current
- Motor is powered by a 6V supply and behaves like a 100Ω resistor
When the transistor is on, it drops about 0.2V, leaving 5.8V across the motor:
- Current = 5.8V / 100Ω = 58 mA → Safe for the transistor
- Base current = (5V - 0.7V) / 1kΩ ≈ 4.3 mA → Safe for Arduino
Arduino Demo
Using a simple Arduino sketch to toggle the digital output pin, you can control the motor:
- Pin goes HIGH: Motor turns on
- Pin goes LOW: Motor turns off
This setup is called a low-side switch because the transistor is between the load and ground.
PNP High-Side Switch Alternative
To use a PNP transistor as a high-side switch:
- Connect the emitter to V+ (e.g., 6V)
- Collector connects to the positive side of the load
- Base is pulled low through a resistor to turn the transistor on
Control logic is reversed:
LOW base = transistor ON
- HIGH base = transistor OFF
However, watch out for voltage compatibility:
- If V+ is 6V, but the microcontroller can only output 5V, it can’t turn the PNP transistor off
So unless the supply voltage is equal to or less than the controller’s output voltage, a PNP switch may stay stuck on.
Key Takeways
For most beginner projects using Arduino or Raspberry Pi, the NPN low-side switch is the most reliable choice. It's easy to set up, works with common digital signals, and avoids voltage mismatch issues. Just remember to:
- Include a base resistor (e.g., 1kΩ)
- Add a flyback diode for inductive loads
- Check that your transistor can handle the load current
With just a few components and a little planning, you can safely control motors, lights, and more from your microcontroller.