The interfacing of power devices like BJTs, and MOSFETs with Arduino output is a crucial configuration which allows switching high power loads through low power outputs of an Arduino.
In this article we elaborately discuss the correct methods of using or connecting transistors like BJTs and mosfets with any microcontroller or an Arduino.
Such stages are also referred to as "Level Shifter" because this stage changes the voltage level from a lower point to a higher point for the relevant output parameter. For example here the level shift is being implemented from Arduino 5V output to MOSFET 12V output for the selected 12V load.
No matter how well programmed or coded your Arduino may be, if it's not correctly integrated with a transistor or an external hardware, could result in inefficient operation of the system or even damage to the components involved in the system.
Therefore, it becomes extremely important to understand and learn the right methods of using external active components like mosfets and BJTs with a microcontroller, so that the final outcome is effective, smooth and efficient.
Before I have explained the interfacing methods of transistors with Arduino, it would be useful to learn the basic characteristics and working of BJTs and mosfets.
Electrical Characteristics of Transistors (Bipolar)
BJT stands for bipolar junction transistor.
The basic function of a BJT is to switch ON an attached load in response to an external voltage trigger. The load is supposed to be mostly heavier in current compared to the input trigger.
Thus, the basic function of a BJT is to switch ON a higher current load in response to a lower current input trigger.
Technically, this is also called biasing of the transistor, which means using current and voltage to operate a transistor for an intended function, and this biasing has to be done in the most optimal way.
BJTs have 3 leads or 3 pins, namely base, emitter, collector.
The base pin is used for feeding the external input trigger, in the form of small voltage and current.
The emitter pin is always connected to the ground or the negative supply line.
The collector pin is connected to the load via the positive supply.
BJTs can be found with two types of polarities, NPN and PNP. The basic pin configuration is the same for both NPN and PNP as explained above, except the DC supply polarity which becomes just the opposite.
The pinouts of a BJT could be understood through the following image:
In the image above we can see the basic pinout configuration of an NPN and an PNP transistors (BJTs). For the NPN the emitter becomes the ground line, and is connected with the negative supply.
Normally when the word "ground" is used in a DC circuit, we assume it to be the negative supply line.
However, for a transistor the ground line associated with the emitter is with reference to its base and the collector voltages, and the emitter "ground" may not necessarily mean the negative supply line.
Yes, for an NPN BJT the ground could be the negative supply line, but for an PNP transistor the "ground" is always referenced to the positive supply line, as shown in the figure above.
The switching ON/OFF function of both the BJTs is basically the same, but the polarity changes.
Since the emitter of a BJT is the "exit" passage for the current entering through and base and the collector, it has to be "grounded" to a supply line which should be opposite to the voltage used at base/collector inputs. Otherwise the circuit won't complete.
For a NPN BJT, the base and the collector inputs are associated with a positive trigger or switching voltage, therefore the emitter must be referenced to the negative line.
This ensures that the positive voltages entering the base and collector are able to reach the negative line through the emitter and complete the circuit.
For a PNP BJT, the base and the collector are associated with a negative voltage input, therefore naturally the emitter of a PNP must be referenced to the positive line, so that the positive supply can enter through the emitter and finish its journey from the base and the collector pins.
Note that the flow of current for the NPN is from base/collector towards emitter, while for the PNP, it's from the emitter towards the base/collector.
In both the cases, the objective is to switch ON the collector load through a small voltage input at the base of the BJT, only the polarity changes that's all.
The following simulation shows the basic operation:
In the simulation above, as soon as the button is pressed, the external voltage input enters the base of the BJT and reaches the ground line via the emitter.
While this happens the collector/emitter passage inside the BJT opens up, and allows the positive supply from top to enter the bulb, and pass through the emitter to ground, switching ON the bulb (load).
Both the switching happen almost simultaneously in response to the pressing of the push button.
The emitter pin here becomes the common "exit" pinout for both the input feeds (base and collector).
And the emitter supply line becomes the common ground line for the input supply trigger, and also the load.
Which means that, the supply line connecting with the BJT emitter must be also strictly connected with the ground of the external trigger source, and the load.
Why we use a Resistor at the Base of a BJT
The base of a BJT is designed to work with low power inputs, and this pin cannot take in large current inputs, and therefore we employ a resistor, just to make sure that no large current is allowed to enter the base.
The basic function of the resistor is to limit current to a correct specified value, as per the load specification.
Please Note that, for BJTs this resistor must be dimensioned as per the collector side load current.
Why?
Because BJTs are current dependent "switches".
Meaning, the base current needs to be increased or decreased or adjusted in accordance with the load current specs at the collector side.
But the switching voltage required at the base of a BJT can be as low as 0.6V or 0.7V. Meaning, BJT collector load could be switched ON with a voltage as low as 1V across base/emitter of a BJT.
Here's the basic formula for calculating the base resistor:
R = (Us - 0.6)Hfe / Load Current,
Where R = base resistor of the transistor,
Us = Source or the trigger voltage to the base resistor,
Hfe = Forward current gain of the transistor (can be found from the datasheet of the BJT).
Although the formula looks neat, it is not absolutely necessary always to configure the base resistor so accurately.
It is simply because, the BJT base specifications has a wide tolerance range, and can easily tolerate wide differences in the resistor values.
For example, to connect a relay having a 30mA coil resistance, the formula may roughly provide a resistor value of 56K for a BC547 at 12V supply input....but I normally prefer using 10K, and it works flawlessly.
However, if you are not following the optimal rules there could be something not good with the results, right?
Technically that makes sense, but again the lose is so small compared to the effort spent for the calculations, it can be neglected.
For example using 10K instead of 56K may force the transistor to work with a slightly more base current, causing it to warm up slightly more, may be a couple degrees higher... which doesn't matter at all.
How to Connect BJT with Arduino
OK, now let's come to the actual point.
Since we have so far comprehensively learned regarding how a BJT needs to be biased and configured across its 3 pinouts, we can quickly grasp the details regarding its interfacing with any microcontroller such as Arduino.
The main purpose of connecting a BJT with an Arduino is usually to switch ON a load or some parameter at the collector side, in response to a programmed output from one of the Arduino output pins.
Here, the trigger input for the BJT base pin is supposed to come from the Arduino. This implies the end of the base resistor simply needs to be attached with the relevant output from the Arduino, and the collector of the BJT with the load or any intended external parameter.
Since a BJT requires hardly 0.7V to 1V for an effective switching, 5V from the Arduino output pin becomes perfectly adequate for driving a BJT and operating reasonable loads.
An example configuration can be see the following image:
In this image we can see how a programmed Arduino is used for operating a small load in the form of relay via BJT driver stage. The relay coil becomes the collector load, while the signal from the selected Arduino output pin acts like the input switching signal for the BJT base.
Although, a relay becomes the best option for operating heavy loads via a transistor driver, when mechanical switching becomes an undesirable factor, upgrading BJTs becomes a better choice for operating high current DC loads, as shown below.
In the above example a Darlington transistor network can be seen, configured for handling the indicated high current 100 watt load without depending on a relay. This allows seamless switching of the LED with minimum disturbance, ensuring a long working life for all the parameters.
Now let's proceed further, and see how mosfets can be configured with an Arduino
Electrical Characteristics of MOSFET
The purpose of using a mosfet with an Arduino is usually similar to that of BJT as discussed above.
However, since normally MOSFETs are designed to handle higher current specs efficiently compared to BJTs, these are mostly used for switching high power loads.
Before we comprehend the interfacing of a mosfet with Arduino it would interesting to know the basic difference between BJTs and mosfets
In our previous discussion, we understood that BJTs are current dependent devices, because their base switching current is dependent on the collector load current. Higher load currents will demand higher base current, and vice versa.
For mosfets this is not true, in other words mosfets gate which is equivalent to BJT base, require minimal current to switch ON, regardless of the drain current (drain pin of mosfet is equivalent to collector pin of BJT).
Having said this, although the current is not the deciding factor for switching a mosfet gate, voltage is.
Therefore mosfets are considered as voltage dependent devices
The minimum voltage required for creating healthy biasing for a mosfet is 5V or 9V, 12v being the most optimal range for switching ON a mosfet fully.
Therefore we can assume that in order to switch ON a mosfet, and a load across its drain, a 10V supply can be used across its gate for an optimal outcome.
Equivalent pins of Mosfets and BJTs
The following image shows the complementing pins of mosfets and BJTs.
Base corresponds to Gate-Collector corresponds to Drain-Emitter corresponds to Source.
What Resistor should be Used for a Mosfet Gate
From our earlier tutorials we understood that the resistor at base of a BJT is crucial, without which the BJT can instantly get damaged.
For a MOSFET this may not be so relevant, because MOSFETs are not affected with current differences at their gates, instead a higher voltage could be considered dangerous. Typically anything above 20V can be bad for a MOSFET gate, but current may be immaterial.
Due to this, a resistor at the gate is not relevant since resistors are used for limiting current, and mosfet gate is not dependent on current.
That said, MOSFETs are hugely vulnerable to sudden spikes and transients at their gates, compared to BJTs.
For this reason a low value resistor is generally preferred at the gates of MOSFETs, just to ensure no sudden voltage spike is able to go through the MOSFET gate and tear it apart internally.
Typically any resistor between 10 and 50 ohms could be used at MOSFET gates for safeguarding their gates from unexpected voltage spikes.
Interfacing a MOSFET with Arduino
As explained in the above paragraph, a mosfet will need around 10V to 12V for properly switching ON, but since Arduinos work with 5V its output cannot be directly configured with a mosfet.
Since an Arduino runs with 5V supply, and all of its outputs are designed to produce 5V as the logic high supply signal. Although this 5V may have the ability to switch ON a MOSFET, it may result in an inefficient switching of the devices and heating up issues.
For effective MOSFET switching, and to transform the 5V output from Arduino into a 12V signal, an intermediate buffer stage could be configured as shown in the following image:
In the figure, the MOSFET can be seen configured with a couple of BJT buffer stages which allows the MOSFET to use the 12V from the power supply and switch ON itself and the load effectively.
Two BJTs are used here since a single BJT would cause the MOSFET to conduct oppositely in response to every positive Arduino signals.
Suppose one BJT is used, then the while the BJT is ON with a positive Arduino signal, the mosfet would be switched Off, since its gate would be grounded by the BJT collector, and the load would be switched ON while the Arduino is OFF.
Basically, one BJT would invert the Arduino signal for the mosfet gate resulting in an opposite switching response.
To correct this situation, two BJTs are used, so that the second BJT inverts the response back and allows the mosfet to switch ON for every positive signals only from the Arduino.
Final Thoughts
By now you should have comprehensively understood the correct method of connecting BJTs and mosfets with a microcontroller or an Arduino.
You might have noticed that we have mostly used NPN BJTs and N-channel mosfets for the integrations, and have avoided using the PNP and P-channel devices. This is because NPN versions work ideally like a switch and is easy to comprehend while configuring.
It's like driving a car normally in the forward direction, rather than looking behind and driving it in the reverse gear. In both ways the car would operate and move, but driving in the reverse gear is much inefficient and doesn't make sense. The same analogy applies here, and using NPN or N-channel devices become a better preference compared to PNP or P-channel mosfets.
If you have any doubts, or if you think I may have missed something here, please use the comment box below for further discussion.
Bhakti says
Hi!
Thank you for sharing your work with everybody.
I am wondering why it is not possible to have just one BJT to fire the MOSFET, by taking the output from the emitter of the BJT, which is kept positive when BJT turned on, by a resistor from emitter to ground?
Thanks
Swagatam says
Hi, the emitter will produce around 0.6V less than the applied base voltage. Since the Arduino output to the BJT base will be 5 V, the effective voltage reaching the mosfet gate will be around 4.4 V which will not be sufficient enough for the mosfet to switch ON efficiently.
Stephen Adegboro says
Thank you so much for your explanation. I would like to know how to get the transfer function of such a circut if 12 volts is applied to power a 12v heater/load. Your assistance would be very much appreciated.
Swagatam says
You are welcome, glad you liked the post. You can use the first circuit for operating a 12V heater. The heater can be connected with the relay contacts and the 12 supply.
Bob Found says
I have a similar circuit…that doesn’t work. I’m using an Atmega328 (same mpu as UNO) driving a 74HC138 decoder for my 8×32 LED pixel (RGB) matrix. The outputs (one at a time) activate the 8 rows of the matrix though P-channel MOSFETS (Vgs 2-4 volts). I have 12 volts on the source of the mosfets. When I use 5 volts, the matrix works properly, i.e. I can light individual pixels or LEDS. As I turn the voltage up (I need 12 volts because they are 1 watt LEDS), at about 8 volts other rows besides the two that I actually activate TURN on. At 12 volts all rows turn on, yet I’m only activating rows 1 and 2. I’ve tried the NPN transistor, the NPN inverter (as shown above), an N-channel MOSFET to isolate the P-channel, various resistor changes, all to no avail. I intend to get rigorous today and instead of hacking, actually taking measurements of what works, what doesn’t work, but I’d appreciate any insight you could give. I have schematic, PCB layout, and photos if requested. I’ve ordered some 74HC238 (sourcing o/p instead of sink) to try that with my N-channel-to-P-Channel driver! It is driving me nuts.
Swagatam says
P channel FETs will require a negative voltage across their gate/source,so if the sequence output is positive, then it might not work?
Can you upload the schematic in any of the free image hosting sites and provide the link to me??
James says
Excellent article.
Shouldn’t the E be on the top of the picture for the PNP BJT?
Swagatam says
Thanks for pointing it out, I’ll correct the diagram soon
Vinicius Senna says
When possible teach us how to do calculate the exactly current the mofet in some future tutorial. For us control the current in BJT is very easy. But in mosfet this is more dark. And don’t have nobody teach us more clearly about this.
In BJT All know that just control the current base of BJT more some calcule, arrive in the current wished allow follow current the collector to emissor. But in mosfet how to do this? How calcule the correct voltage in the gate when my wish is allow just 1.76A by exemple ?
Swagatam says
Thank you Vinicius, calculating current in mosfets can be easily done by adding another BJT to its gate pin as shown at the bottom section of this post. You can apply this technique and achieve the required current restriction on any mosfet