# How to keep a transistor's base current constant with varying input voltage?

The problem:
In the circuit displayed below, I have a microcontroller supplying current to the base of a NPN 2N2222 transistor. When the battery is at 9V, this setup works great: the microcontroller can supply 5mA and fully open the transistor.

When the battery drops voltage over time, however, this causes a massive change the amount of current required by the microcontroller/base to open the transistor. (see diagrams below)

My setup when the battery is at 9V

My setup when the battery is at 8V

What my goal is:
I want to use PWM supplied from the microcontroller to the base to control my 6V motors, which are being powered from a 9V battery, using somewhere around 2-12mA of current from the microcontroller. I have read up on base biasing, but I have not been able to reproduce the results I want on my own. I have a feeling that this will all boil down to some basic V=IR logic, but despite my best efforts I haven't been able to think of a way to do so.

Does anyone have any advice on how they would change this circuit to achieve that goal? I really appreciate your time.

• What is the reason for R2? Mar 19, 2023 at 6:32
• Use a base resistor to control base current. As depicted, you use an emitter resistor, controlling emitter = base+collector current. Use a diode to protect the transistor from back EMF. Depending on motor current, have an eye on current gain and collector-emitter voltage: multiplied by current, the dissipated power may be too much. Mar 19, 2023 at 8:00
• To supplement @graybeard's comment: remove the 5-ohm resistor R2 and connect the transistor's emitter directly to ground. Add a resistor between the microcontroller's digital I/O (DIO) pin and the transistor's base. This resistor's value must be chosen to (a) supply enough current into the transistor's base to drive the transistor into hard saturation, and (b) limit the DIO pin's output current to a level that is well below the DIO pin's maximum output current rating. See also: electronics.stackexchange.com/a/488355/79842 Mar 19, 2023 at 10:14
• What does this mean --> open the transistor? Are you using a hydraulic/pneumatic analogy for a semiconductor device? Mar 19, 2023 at 10:49