In previous entries, we have seen how to use the digital outputs and PWM analog outputs of Arduino to perform actions in the world.
However, outputs in Arduino are limited in both voltage and current. The maximum voltage they can supply will be the supply voltage (5V or 3.3V depending on the model). On the other hand, the maximum allowable current is 40mA, with a recommended value of less than 20mA.
Beyond turning on a small load or running a few example programs, the truth is that in the real world this is not enough to operate most loads, not even a small DC motor or a relay.
This behavior is common, in general, in all automata. The function of an automaton is not to provide enough current to perform actions. Its function is to read inputs, perform calculations, communicate with other automata, and command actions.
Remember, the processor is the “brain” of a machine, not the muscle.
To perform actions, processors delegate to amplification stages or drivers, which act as adapters between the power level used in the controller and that required by the actuator.
One of the simplest and most common ways to perform this adaptation is by using transistors. There are two main families of transistors: BJT (bipolar junction transistor), which were the first to appear, and FET (field effect transistor).
In this entry we will learn to use BJT transistors to handle larger loads than we can manage with Arduino’s digital or analog outputs.
In the next entry, we will see how to do this with MOSFET transistors, a subfamily of FET transistors.
What is a BJT transistor?
Transistors are electronic devices that form the basis of modern electronics.
In very brief terms, we find two distinct applications or modes of operation.
- Amplifying an electrical signal
- Acting as an electrically controlled switch
There are many models of transistors, each with different electrical characteristics, to meet the various needs of our electrical circuits. Therefore, an important part of the design phase when using a transistor is the choice of the appropriate model.
Externally, the same transistor can be manufactured in different standard packages. Normally, larger packages allow for better heat dissipation and, therefore, better support for higher currents.
The packages we will use most frequently in our projects are TO-92 and TO-220.
Therefore, it is not possible to easily distinguish the characteristics of a transistor simply visually. We will need to look at the model number, engraved on it, and consult its corresponding Datasheet.
How does a BJT transistor work?
Transistors are relatively complex devices, and it requires knowledge of electronics to understand them in depth. Therefore, instead of conducting a detailed analysis, we will limit ourselves to providing a series of guidelines to understand their fundamentals and allow us to use them in our setups.
If you need more information, you can consult any electronics book, or feel free to leave a comment asking whatever you want.
A good way to understand the operation of a transistor is to look at its “grandfather” in electronics, the vacuum tube.
A vacuum tube is an electrical component that has three terminals inside a tube from which air has been evacuated. One of the terminals acts as the cathode and another as the anode. The third terminal is connected to a metal grid located between the two.
When electricity is applied to the grid terminal, an electric field is generated that “pulls” electrical current from the cathode. Electrons cross the vacuum, pass without colliding with the grid, and finally reach the anode.
The result is that we have an “electrical contact” between the cathode and anode terminals, where we can regulate the amount of electricity flowing between the two by acting on the grid (the third terminal). This gives us an electrical amplifier.
In BJT transistors, the glass bulb and terminals are replaced by semiconductor junctions with different levels of doping (hence their name, bipolar junction), forming a solid integrated component. This allows them to be much smaller and more durable than vacuum tubes.
In a transistor, we also have three terminals, called Emitter, Collector, and Base. The “electrical contact” we are interested in is made between the emitter and collector, while the base is the element that controls the amount of electricity that passes through the component.
Operating modes in a BJT
A BJT transistor has three operating modes.
- Cut-off: In this mode, the transistor behaves as if the collector and emitter were disconnected, so we say it is similar to having an open circuit.
- Saturation: In this mode, the transistor behaves as if the collector and emitter were connected by a low-voltage diode. Therefore, we say it is similar to having a short circuit, with a certain voltage drop.
- Active: In this mode, the current between the collector and emitter is proportional to the base current.
The active mode is the mode used to amplify signals (for example, to make an audio amplifier). In this entry, we will not use this mode.
The cut-off and saturation modes can be used together to form an “electrically controlled switch.” It is these two modes that we will use to turn our load on and off, achieving an effect similar to using a physical switch, with the difference that this “switch” will be controlled by an Arduino output.
The state in which a BJT transistor operates depends on the current flowing through its base. For this reason, it is said that a BJT transistor is a current-controlled device (in contrast, vacuum tubes and FET transistors are voltage-controlled devices).
Connection diagram
There is more than one way to connect the transistor. In this entry, we will use the common emitter configuration, as it is the simplest for switching the transistor.
There are two subfamilies of BJT transistors, PNP and NPN types. The difference between them is the type of junctions and semiconductors used in their manufacturing.
Mnemonic rule: You can use the word “Pincha” (which we will abbreviate as “PN”), imagining that the arrow is a needle that either pierces or does not pierce the interior of the transistor. If the arrow “Pierces,” it’s PNP. If it “Does Not Pierce,” it’s NPN
Both types of transistors, PNP and NPN, are similar in terms of construction and operation, but they differ in their application.
First, the placement of the BJT transistor in the circuit influences its setup.
Mnemonic rule: The PNP transistor (P) is placed on the “Positive” side of the circuit. The NPN transistor (N) is placed on the “Negative” side of the circuit
On the other hand, the NPN conductor conducts when VB is HIGH (greater than VE). However, a PNP conducts when VB is LOW (less than VE). Therefore, the output with a PNP transistor is an inverting output.
Finally, if Vcc is greater than VB (for example, with a 5V Arduino trying to switch a 12V load) we will not be able to switch the transistor because VB will never be greater than VE. And better yet, because we would apply Vcc to the Pin, and we could damage it. In that case, we would need pre-amplification, for example with an NPN.
Regarding the base resistor Rb, it serves to regulate the current flowing through the base of the transistor. Choosing its value is critical for the correct functioning of the circuit. We will calculate the value of this resistor next.
Lastly, it is important to note that not all transistor models and packages assign the same terminals to each pin. Therefore, you will need to consult the pinout in the Datasheet of the device before making the setup.
Calculation of the BJT transistor
You can perform all these calculations in this section automatically using the BJT transistor switch calculator
To use the transistor as a switch, we will use the cut-off and saturation regions of the transistor, so we do not need to perform calculations in the active region.
In this way, the calculation simplifies and reduces to calculating the necessary base resistance to ensure that at the desired operating point, the transistor switches between cut-off and saturation.
In a BJT transistor, the current flowing through the collector Ic is proportional to the current at its base Ib. This relationship is called hFe (sometimes Beta). Typical values are between 100 to 200.
On the other hand, in saturation, the transistor behaves like a diode between collector and emitter with a voltage Vce, and a diode between base and emitter with a voltage Vbe.
For example, let’s assume we want to power a load of 200mA with a nominal voltage of 12V, with a certain transistor for which we have obtained from its Datasheet that
- hFe(min) = 100
- Vce(sat) = 0.2
- Vbe = 0.7
In the example, we will assume a constant hFe, but in the real world, the hFe relationship varies between transistors of the same model due to differences in the manufacturing process. Additionally, it has a dependence on the operating temperature and the operating point. Therefore, in the component Datasheets, you will observe curves of this factor’s dependency.
Applying Ohm’s law to the device, we calculate that its equivalent is a resistance of 60 Ohm.
Thus, we can calculate the current flowing through the collector.
The required base current is the collector current reduced by a factor of hFe(min), so
Thus, the necessary base resistance is less than
We choose the nearest lower normalized resistance and we would check by looking at the curves in the Datasheet that the base voltage is sufficient to drive the transistor into saturation.
Another way to ensure the saturation of the transistor is to increase the base current slightly. In this example, Arduino has to provide less than 2 mA to the transistor, so we have room to increase the current a bit.
We would choose the closest normalized resistor. In this example, where we do not have more details about the transistor, a base resistor of 1k would be reasonable.
You can choose the nearest normalized resistor with the Normalized Resistor Calculator
Finally, we must verify that all elements of the system can withstand the current and power flowing through them (including the resistors and the transistor itself).
Some models of transistors allow for dissipating higher powers by coupling an external heatsink.
BJT Models in Arduino
There is a wide variety of BJT transistors that we can use in our Arduino setups. In general, they are very cheap components. Their price varies depending on the model and characteristics, but a common price is 0.01 - 0.02€.
Among the many available models, some common ones are N2222, BC547, BC337, BC556, or TIP41C.
Connecting inductive loads
When connecting inductive loads, coils, electromagnets, motors, relays, we must put an additional protection device, the Flyback diode.
Inductive loads present an opposition to variations in the current flowing through them, generating an induced current that opposes changes in intensity. These induced currents can damage the transistor or even the Arduino pin.
The flyback diode provides a low-resistance path that allows dissipating the currents induced by inductive loads, protecting the rest of the devices.
Therefore, in the case of inductive loads, the setup would look as follows.
Darlington Pair
To achieve higher amplification ratios than those of a BJT, we can use a Darlington pair, a device based on BJT transistors, widely used in electronics.
Essentially, a Darlington pair is a set made up of two BJTs in a single integrated circuit.
The overall behavior of a Darlington pair is similar to a BJT in which the hFE values multiply, reaching values of 5000-20000.
On the other hand, the base voltage adds up, so the typical values of Vce are around 1.4V.
Similarly, there are multiple models of Darlington pairs. Their price is higher than that of a BJT, but they are still cheap devices. The usual range is from 0.10 to 0.20€.
Among the many available models, some common ones are TIP140, BC317, and the ULN2003 integrated circuit, which has 7 Darlington pairs in a single package.
BJT Transistors in PWM
BJT transistors are suitable for PWM outputs, so they can be controlled with the PWM analog outputs of Arduino.
The result will be a pulsed wave between Vcc and GND, with the same frequency as the PWM.
We only need to check that the switching frequency of the chosen transistor is higher than the frequency of the PWM we are applying.
Example Code
The code to be used is exactly the same as the one we saw when looking at the digital outputs and PWM analog outputs in Arduino, so we refer you to the examples in these entries.
The transistor stage only allows us to adapt the output voltage and current to higher values than we could provide directly with the outputs of Arduino.