Blog Entry
Using Transistor as a Switch
December 23, 2008 by rwb, under Electronics.
Most of microcontrollers work within 5 volt environment and the I/O port can only handle current up to 20mA; therefore if we want to attach the microcontroller’s I/O port to different voltage level circuit or to drive devices with more than 20mA; we need to use the interface circuit. One of the popular method is to use the Bipolar Junction Transistor (BJT) or we just called it transistor in this tutorial. I have to make clear on this BJT type to differentiate among the other types of transistors family such as FET (Field Effect Transistor), MOSFET (Metal Oxide Semiconductor FET), VMOS (Vertical MOSFET) and UJT (Uni-Junction Transistor).
A. The Switch
The transistor actually works as a current gainer; any current applied to the base terminal will be multiplied by the current gain factor of the transistor which known as hFE. Therefore transistor can be used as amplifier; any small signal (very small current) applied to the base terminal will be amplified by the factor of hFE and reflected as a collector current on the collector terminal side.
All the transistors have three state of operation:
- Off state: in this state there is no base current applied or IB = 0.
- On active state: in this state any changes in IB will cause changes in IC as well or IC = IB x hFE. This type of state is suitable when we use transistor as a signal amplifier because transistor is said is in the linear state. For example if we have a transistor with gain of 100 and we increase the IB from 10uA to 100uA; this will cause the IC to swing from 1000uA to 10000uA (1 mA to 10 mA).
- On saturate state: in this state any changes in IB will not cause changes in IC anymore (not linear) or we could say IC is nearly constant. We never use this state to run the transistor as a signal amplifier (class A amplifier) because the output signal will be clamped when the transistor is saturate. This is the type of state that we are looking for on this tutorial.
From the picture above we could see the voltage and current condition of transistor on each state; if you notice when transistor is in off state the voltage across collector and emitter terminal is equal to the supplied voltage, this is equivalent to the open circuit and when transistor is in saturate state the collector to emitter voltage is equal or less then 0.2 Volt which is equivalent to the close circuit. Therefore to use transistor as a switch we have to make transistor OFF which equivalent to the logical “0” and SATURATE which is equivalent to the logical “1“.
One of the famous diagrams that show the transistor operating state is called the transistor static characteristic curve as shown on this following picture:
When we operate transistor as the class A common emitter amplifier usually we choose to bias the transistor (apply voltage on VBE and VCE) in such a way (Q-Point) that IC and VCE (output) will swing to its maximum or minimum value without any distortion (swing into the saturation or cut-off region) when the IB (input) swing to its maximum or minimum value; but when we operate the transistor as switch we intentionally push the transistor into its saturation region to get the lowest possible VCE (e.g. near 0.2 volt) when we need to make the transistor ON (switch ON) and into its cut-off region when we need to make the transistor OFF (switch OFF).
The above diagram show a typical microcontroller interface circuit using NPN transistor; the RB resistor is used to control the current on base terminal that make transistor OFF and ON (saturate); while the RC resistor is the current limiter for the load. if the load operate with the same voltage as the supplied power (Vcc) you can by pass the RC (not use).
Notice the diode (also known as the clamp diode) in the inductive load circuit is needed to protect the transistor again the EMF (Electromotive Force) voltage generated by the inductor component when the transistor is switched on and off rapidly, this voltage is oppose the source voltage. The diode will act as a short circuit to the high voltage generated by the inductor component, you can use any general purpose diode with capable on handling minimum 1 A of current such as 1N4001, 1N4002, etc.
Ok let’s calculate each of the RB and RC value on this following circuit:
On the circuit above we are going to use 2N3904 (the cheap general purpose transistor where you could easily found on your local market) to drive 5 LED from microcontroller port, from the 2N3904 datasheet we get this information:
IC max = 200mA (this is maximum value that will make your transistor smoked, for more practical application always use just half of the maximum value mentioned on the datasheet), hFE = 100 to 300, VBE saturate = 0.65 Volt, VCE saturate = 0.2 Volt
For most transistor in general we can use VBE = 0.7 Volt (should be saturate) and VCE = 0 Volt. Using the 5 volt power supply (VCC) and assuming VLED = 2 Volt, with each of them consuming 15 mA, we could calculate the RC value using the Ohm’s law as follow:
IC = 5 x 15 mA = 75mA (0.075 A), this current is still far bellow the maximum IC allowed by 2N3904 transistor.
RC = (VCC - VLED) / IC = (5 - 2) / 0.075 = 40 Ohm
Power Dissipation on the RC resistor will be
P = (VCC - VLED) x IC = (5 - 2) x 0.075 = 0.225 Watt
Base on the above calculation we could use the nearest higher value available on the market; which is 47 Ohm, 0.5 watt resistor (for heat dissipation usually we use twice of the watt value calculated).
With the hFE minimum of 100; the minimum current required in the transistor’s base terminal to drive the LED is:
IC = hFE x IB
IB = IC / hFE = 0.075 / 100 = 0.00075 A (0.75 mA)
This current can easily be supplied by most microcontroller I/O port; which is capable to drive up to 20 mA output current. Again by applying the Ohm’s law we could calculate the RB value as follow:
RB = (VPORT - VBE) / IB
Assuming the minimum average voltage of microcontroller I/O port (VPORT) with logical “1” is about 4.2 volt (the microcontroller is powered by 5 volt supply):
RB = (4.2 - 0.7) / 0.00075 = 4666.66 Ohm
Power dissipation on the RC resistor will be
P = (VPORT - VBE) x IB = (4.2 - 0.7) x 0.00075 = 0.002625 Watt
Base on the result you could use 4K7 Ohm, 0.25 Watt resistor (this is the common resistor which you could easily found on the local market i.e. 0.25 watt and 0.5 watt).
Use this RB calculation as your maximum reference value; in the real world most of the transistors hFE is vary and being measured (tested) with different VCE and IC value not to mention different specification even though you use the same transistor type. Therefore the real RB value could be lower than 4K7 if you really want to drive the transistor into fully saturate mode where the VCE near 0.2 volt.
Now the question is how we determine the exact value? To answer to this question I build this following testing circuit base on our RC and RB calculation above using the Atmel AVR ATTiny25 microcontroller to blink the five LED:
Note: the reason I used RC = 3×150 Ohm because at that time I run out the required 47 Ohm resistor, therefore you could use just single 47 Ohm resistor or if you only have 150 Ohm as I did, you could use them as I did.
Bellow is the C Program that I used to test this circuit:
//*************************************************************************** // File Name : trswitch.c // Version : 1.0 // Description : Transistor as Switch: Simple LED Blinker // Author : RWB // Target : Atmel AVR ATTiny25 Microcontroller // Compiler : AVR-GCC 4.3.0; avr-libc 1.6.2 (WinAVR 20090313) // IDE : Atmel AVR Studio 4.17 // Programmer : Atmel AVRISPmkII // Last Updated : 1 November 2009 //*************************************************************************** #include <avr/io.h> #include <util/delay.h>
int main(void)
{
// Initial I/O
DDRB |= (1<<PB3); // Set PB3 as Output, Others as Input
PORTB &= ~(1<<PB3); // Reset the PB3
for(;;) { // Loop Forever
PORTB |= (1<<PB3); // Port PB3 High
_delay_ms(3000); // Delay 3 Second
PORTB &= ~(1<<PB3); // Port PB3 Low
_delay_ms(1000); // Delay 1 Second
}
return 0; // Standard Return Code
}
/* EOF: trswitch.c */
The program simply blink all the LED by toggling the AVR ATTiny25 microcontroller PB3 output port high for about 3 second and low for about 1 second and here is the test result when the PB3 port swing to the logical high:
As you’ve seen from the result there is about 0.4 volt drop on the collector to emitter (VCE) terminal instead of 0 Volt as we assume on the above calculation and the DC current gain is about 58 instead of 100 again as we assume on the above calculation. Now you understand there are tremendous different result between the 2N3904 transistor datasheet and my test circuit, this is because the 2N3904 datasheet is measured using the PWM (Pulse Width Modulation) with period about 300 us (micro second) and duty cycle about 2%, the reason to use this very short pulse period in the measurement is because they don’t want to overheat the transistor junction; where this junction heating will vary the transistor hFE significantly.
On my test circuit above; I used 3 second to make the 2N3904 transistor ON (saturate, VBE = 0.81 Volt, VCE = 0.4 Volt) and 1second to make it OFF. The other factor that make the test result different is the various manufacture specification even though we used the same transistor type. Therefore the answer to the above question is; there is no exact value for RC and RB; is depend on your application but it save to use the above method to calculate the RC and RB and then do the circuit prototyping to test your design, next adjust your RC and RB value accordingly.
Some calculation suggestion is using the collector to base current ratio of 10 (regardless of the transistor hFE value) to force the transistor into fully saturate (VCE = 0.2 Volt, as shown on the datasheet above) using this following formula:
IB = IC / hFE = IC / 10
This is what I called a “maximum saturate calculation method” (also known as worst-case design procedure), again as you’ve seen from the real test circuit result above even though we drive the VBE more than 0.7 volt, we still get the hFE for about 58 and IB about 0.88 mA which is useful in the microcontroller application (for more information you could read “Powering Your Microcontroller’s Base Project” on this blog), therefore for practical application I would suggest; if you want to use this maximum saturate calculation method to determine the base resistor (RB) value, make sure at least you double the calculated value. For example to determine the RB on the test circuit above using this maximum saturate calculation method:
IB = IC / hFE = 0.075 / 10 = 0.0075 A (7.5 mA)
RB = (4.2 - 0.7) / 0.0075 = 466.66 Ohm
Using twice the calculated value you will get 933.32 Ohm, or you could use the 1K Ohm standard resistor.
In typical rapid switching transistor application actually we don’t drive the transistor into fully saturate (i.e. VCE = 0.2 Volt), because when the transistor is fully saturate, it tend to have longer switching time (i.e. from ON to OFF to ON again). The VCE = 0.4 volt as shown on the real test circuit above is already adequate for most switching application, while we could still take advantage of the low transistor base current (i.e. IB = 0.88 mA). You could see this test circuit on the video at the end of this article.
B. Driving the Relay
Now using the same principal we could easily calculate the RC and RB value on this following circuit:
By using 5 Volt power supply and relay with 5 Volt and 60mA operating current:
RC = 0 Ohm (not use, connect relay directly to VCC)
IB = IC / hFE = 0.06 A / 100 = 0.0006 A
RB = (VPORT - VBE) / IB = (4.2 - 0.7) / 0.0006 = 5833.33 Ohm, use 5K6 Ohm resistor
P = (VPORT - VBE) x IB = (4.2 - 0.7) x 0.0006 = 0.0021 watt, use 0.25 Watt resistor
C. Increasing the Collector Current
What if the load current is more than 1 A, let’s say you want to drive a DC motor? Perhaps you will think to use bigger transistor such as 2N3055 power transistor; unfortunately the big power transistor tends to have small hFE mostly less then 20, so it’s mean we have to supply bigger base current. We know that most microcontrollers I/O port can supply up to 20mA, therefore using this kind of transistor the maximum current we could achieve in the collector terminal is about 400mA which is far bellow our expectation. The solution for this situation is to use what known as Darlington pair circuit:
By using the Darlington pair circuit we could combine two transistors; one with high hFE factor usually has a low collector current and one with high collector current usually has a low hFE factor. Notice that on the Darlington pair circuits the VBE will be twice as the normal saturated voltage which is about 1.4 Volt. One of the popular ready made Darlington pair transistors on the market is TIP120 (NPN type) which can handle current up to 3 A, and has the hFE minimum of 1000.
Using the same principal we’ve learned before, we could easily calculate the RB value of the DC motor circuit interface bellow:
By using 5 Volt power supply and DC Motor with 12 Volt and 1 A maximum operating current:
RC = 0 Ohm (not use, connect directly to the 12 Volt power)
IB = IC / hFE = 1 A / 1000 = 0.001 A
RB = (VPORT - VBE) / IB = (4.2 - 1.4) / 0.001 = 2800 Ohm, use 2K7 Ohm resistor
P = (VPORT - VBE) x IB = (4.2 - 1.4) x 0.001 = 0.0028 watt, use 0.25 Watt resistor
D. The Darlington Transistor Array
For more compact version of the Darlington pair transistor you could use the Texas Instrument ULN2803A which is contain 8 Darlington pair transistors with has build in 2K7 base resistor and clamp diode for each Darlington pair transistors. This makes this Darlington transistor array suitable for driving the relay or motor up to 500mA (this is a maximum datasheet value) directly from the microcontroller output.
To increase the output current up to 1 A (2 x 500mA, remember this is a maximum datasheet value, for practical application use just half or 2 x 250 mA) you could simply use two Darlington transistor array connected in parallel, the following is the sample circuit for driving two DC motors using the ULN2803A Darlington transistor array:
Thanks to the build in internal 2K7 base resistor and the clamp diode, you don’t need any external component when using ULN2803A to drive the DC motor from your microcontroller port. The Darlington transistor array ULN2803A could be used to drive up to 50 volt voltage load.
E. Isolating your Circuit
Sometimes we need to isolate our microcontroller circuit from the interface circuit especially in the environment that generating a lot of noise which could disturb our microcontroller operation. Using just a relay from the above example; you could see the ground is still directly connected to the microcontroller circuit, so there is a change the noises will interfere the microcontroller circuit.
To completely isolate the circuit we could use the optocouplers (also called optoissolator) circuit, this circuit will completely isolate your microcontroller from the interface circuit:
The popular optocouplers circuit available on the market is 4N35 which has the hFE of 500 (in the optocouplers terminology this is also known as the transistor static forward current transfer ratio, Texas Instrument SOES021C, measured with infrared LED current = 0) and maximum collector current of 100mA.
Differ from the ordinary transistor in the optocouplers we don’t use the transistor base terminal for driving the collector current; instead we use the internal infrared LED to transfer the infrared LED light intensity to the phototransitor; based on this infrared LED light intensity the phototransistor will be turned ON or OFF; giving more current to drive this infrared LED will effect more current to flow on the phototransistor collector; This effect is known as the current transfer ratio (CTR). The 100% CTR means that all the current flow on the infrared LED will be transferred 100% to the phototransistor collector.
Therefore by driving the internal infrared LED with 15 mA (in the optocouplers terminology this is also known as the input diode static forward current), we could assure that the phototransistor will be in the saturate state (ON), because the minimum current to make the phototransitor on is about 10 mA. The following circuit is use optocoupler to interfacing the relay:
By using 5 Volt power supply and relay with 5 Volt and 60mA operating current:
RC = 0 Ohm (not use, connect relay directly to 5 Volt)
Idiode = 15 mA (0.015 A), VLED = 2 Volt
RB = (VPORT - VLED) / IB = (4.2 - 2) / 0.015 = 146.66 Ohm, use 150 Ohm resistor
P = (VPORT - VLED) x IB = (4.2 - 2) x 0.015 = 0.033 watt, use 0.25 Watt resistor
If you need to drive more current you could use the Darlington pair circuit above or you could use the high gain Darlington optocopuler such as 4N45 (CTR minimum about 350 %).
F. Controlling your DC motor direction
Using just one transistor to control the DC motor as the above example; we only can turn the DC motor in one direction if we want to change the direction than we also have to change the DC motor voltage polarity. The other way to work around this condition is to use the relay to switch the DC motor’s voltage polarity, but using this method means the DC motor will always ON and we can not control the DC motor speed using digital signal or known as the PWM (Pulse Width Modulation).
The best and popular way to solve this issue is to use the H-bridge circuit:
When we apply current (IB1) to the TR1 and TR2 transistors, IB2=0 to the TR3 and TR4 transistors, then TR1 and TR4 transistors will be turned ON, TR2 and TR3 will be turned OFF; this will cause the current to start flow through TR1 transistor, passing the DC motor and going into the TR4 transistor (blue color). When we apply current (IB2) to the TR3 and TR4 transistors, IB1=0 to the TR1 and TR2 transistors, then the TR3 and TR2 transistors will be ON while TR1 and TR4 transistors will be turned OFF; this will cause the current to flow through TR3, passing the DC motor in reverse polarity and going into the TR2 transistor (red color). By not applying current to both IB1 and IB2 all the transistors will be turned OFF.
Again by applying the Ohm’s law we could easily calculate the RB1 and RB2 on this following circuit (Updated! Thanks for the nice discussion and correction from the All About Circuits Forum discussion here, in order for this circuit to work you have to put a resistor on each of the TIP 120 Darlington transistors base terminal):
The above H-Bridge circuit use 5 Volt supply and DC motor with 12 Volt and 1 A maximum operating current; assuming the TIP120 Darlington transistor hFE is 1000, the RB1 and RB2 resistors could be calculated as follow:
IB = IC / hFE = 1 A / 1000 = 0.001 A, for each of the transistor base current
RB1a,b = (VPORT - VBE) / (2 x IB) = (4.2 - 1.4) / 0.002 = 1400 Ohm, use 1K5 Ohm resistor
RB2a,b = (VPORT - VBE) / (2 x IB) = (4.2 - 1.4) / 0.002 = 1400 Ohm, use 1K5 Ohm resistor
P = (VPORT - VBE) x 2 x IB = (4.2 - 1.4) x 0.002 = 0.0056 watt, use 0.25 Watt resistor for RB1 and RB2
To test the TIP 120 H-Bridge circuit above I used this following circuit using Atmel AVR ATTiny13 microcontroller as shown on this following picture:
Bellow is the C Program that I used to test this circuit:
//*************************************************************************** // File Name : trhbridge.c // Version : 1.0 // Description. : Transistor as Switch: Simple All TIP120 H-Bridge // Author : RWB // Target : Atmel AVR ATTiny13 Microcontroller // Compiler : AVR-GCC 4.3.0; avr-libc 1.6.2 (WinAVR 20090313) // IDE : Atmel AVR Studio 4.17 // Programmer : Atmel AVRISPmkII // Last Updated : 18 June 2010 //*************************************************************************** #include <avr/io.h> #include <util/delay.h>
int main(void)
{
// Initial I/O
DDRB |= (1<<PB3)|(1<<PB4); // Set PB3,PB4 as Output, Others as Input
PORTB &= ~(1<<PB3); // Reset PB3 (OFF)
PORTB &= ~(1<<PB3); // Reset PB4 (OFF)
for(;;) { // Loop Forever
PORTB |= (1<<PB3); // Turn ON PB3
_delay_ms(3000); // Delay 3 Second
PORTB &= ~(1<<PB3); // Turn OFF PB3
_delay_ms(2000); // Delay 2 Second
PORTB |= (1<<PB4); // Turn ON PB4
_delay_ms(3000); // Delay 3 Second
PORTB &= ~(1<<PB4); // Turn OFF PB4
_delay_ms(2000); // Delay 2 Second
}
return 0; // Standard Return Code
}
/* EOF: trhbridge.c */
One of the advantage using all NPN transistors in the H bridge circuit is the NPN transistor tends to have faster turn on time comparing to the PNP transistor, beside by using the same transistor type we could have similar transistor characteristic in the circuit. You could read more example of using all NPN transistor H-Bridge in “H-Bridge Microchip PIC Microcontroller PWM Motor Controller” on this blog.
Using Transistor as switch Testing Circuit Video
1. This following video show you of how to drive a transistor which connected with 5 red LED using the Atmel AVR ATTiny25 microcontroller.
2. The TIP120 H-Bridge Testing Circuit video using Atmel ATTiny13 Microcontroller:
Bookmarks and Share
Related Posts
7 Responses to “Using Transistor as a Switch”
Comment by rwb.
The original TIP120 H-Bridge schematic has been changed; now I used resistor on each of the TIP120 base terminal as you did. Thank you
Comment by mandomoose.
Thankyou for this great post. I was wondering about using the transistor as a switch with my avr.
Keep having fun ![]()
Comment by kansairobot.
Thanks for the great blog
I am sorry if this sounds like a total newbie question but in your pics (in which you dont use a optocoupler) you connect both the 12V circuit and the micro ground to a common ground.
My question is how to implement the 12V and 5V part? I mean let’s say I am using common batteries (8 batteries= 12V) I connect the motor to the 12V part but where do I connect the 5V pin of the micro to??
I dont know if my question make sense sorry
Kansai
Comment by rwb.
You should have two separate DC power sources (e.g. 3 AA batteries for 4.5 volt and 8 AA batteries for 12 volt); the first one is the 5 volt or 4.5 volt which is used to power the microcontroller circuit and the second one is the 12 volt which is used to power the Darlington transistor and the DC motor. In order to make the darlington transistor work (ON) we have to provide adequate voltage between the base and the emitter terminal; this voltage is provided by the microcontroller I/O port (powered by 5 Volt source), that is why we have to connect these two voltage sources on the same common ground.
Comment by kansairobot.
thank you very much for your reply.
For the H-bridge part i was thinking of using 2N2222’s (since my motor only needs around 280mA).
or use Toshiba TA7291S bridge circuit.
These circuits are made of transistors it seems but how can you see if they generate enough current C-E (as we did for transistors in this tutorial)? I cant seem to understand their datasheet.
Sorry for all the questions but I am learning a lot with your tutorials. Thanks always for these resources…
kansai
Comment by rwb.
When you choose the transistor (e.g. 2N2222A); from datasheet Ic max = 800 mA, remember this is the maximum value, usually in real application we only use just half of its maximum capacity which is 400 mA. When you measure the DC motor make sure you also take into the consideration the DC motor stall current (i.e. motor on heavy load, where its almost stop) not reach the 400mA limit; unless you only use the DC motor as a free running DC motor (without or have a very small load).
When using the Toshiba TA7291S bridge all you need is to supply the correct standard logic voltage to the IN1 and IN2 input pins; the integrated circuit inside TA7291S will make sure you get the saturate transistors condition on its output (average 0.9 volt). The input current on the IN1 and IN2 pins is very low (about 3 to 10 uA with Vin = 3.5 volt).




















Posts
Comment by slowjoe.
I’ve had a go at making the final H-bridge circuit shown here using TIP120 darlington pairs and had a bit of trouble. If I split the connections from the microcontroller after the resistors RB1 and RB2 (as shown in the circuit diagram) then it doesn’t seem to work, however if I split the signal before the resistors and use 2 resistors for each of RB1 and RB2 then it does work. I’m not sure I understand why yet maybe somebody can explain.