Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
ermicroblog Microcontrollers Microcontrollers and Electronics Project Blog
Hom e
About
Copyright and Disclaimer
Contact Us
Blog Entry
Search This Site
Custom Search
Using Transistor as a Switch December 23, 2008 by rwb rwb,, under Electronics Electronics..
Stepper motor parts Replacement Motors For HMC/VM MC/VMC/CNC C/CNC Machines, Machines, Express Delivery www.GlobalMachineParts.com/Motors
Future Post Controlling the Motor is one of interesting topics in the embedded world especially for the robotics enthusiasts, on the next post we will learn the basic of motor electronic circuit as well as how to control it with microcontroller.
Most of microcontrollers 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 m icrocontroller’s icrocontroller’s I/O port to d ifferent voltage level circuit or to
Therefore don't miss it, stay tun e on
drive devices with more than 20mA; we need to use the interface circuit. One of the popular method is to
this blog !
use the Bipolar Junction Transistor (BJT) or we just called it transistor in this tutorial. I h ave to make clear on this BJT type t o differentiate among the other types of transistors family such as FET (Field Effect Transistor), Transistor), MOSFET (Metal Oxide Semiconductor FET), V MOS (Vertical MOSFET) and UJT (Uni-Junction Transistor).
ISO Standard CBN Inserts
A. The Switch
China Manufacturer Of PCD CBN Tools Various Model, Free Sample
The transistor actually works as a current current gainer; any current applied t o the base terminal will be multiplied by the current gain factor of the transistor which known as hFE . Therefore transistor can be
www.china-wsq.com
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.
Led power Meet quality suppliers in Hong Kong Int'l Lighting Fair 2012. 6-9 Apr www.hktdc.com
Generator parts We offer Stamford og AvK parts Regulators, diodes, bearings. www.generatorsolutions.no
Ic Circuit Design News Read the Latest Electronics Design News & Daily Updates from EWwww.ElectronicsWeekly.com…
Electronic PWM AVR All the transistors have three state of operation:
Off state: state: in this state there is no base current applied or IB = 0. 0. On active state: state: in this state any changes in I B will cause changes in I C as well or IC = IB x
1 to 45kVA Single & Three-Phase Precise Regulation Outdoor & Custom www.tsipower.com
hFE. This type of state is s uitable when we use t ransistor 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
Subscribe
Using Transistor as a Switch | ermicroblog
we increase the I B from 10uA to 100uA; this will cause the IC to swing from 1000uA to 10000uA (1 mA to 10 mA).
http://www.ermicro.com/blog/?p=423
Posts |
Comments
On saturate state: state: in this state any changes in I B will not cause changes in I C anymore (not linear) or we could say I C 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 t hat we are looking for on this tut orial.
Recommended Books
Programming 8-bit PIC Microcontrolle... Martin P. Bates Best Price $30.99 or Buy New $37.11
From the picture above we could see th e voltage and current condition of transistor on each state; if you notice when transistor is in off st ate the voltage across collector and emitter terminal is equal t o the supplied voltage, this is equivalent to the open circuit and when transistor is in saturate state the
Privacy Information
collector to emitter voltage is equal or less then 0.2 Volt which is equivalent t o the close circuit. Therefore to use transistor as a switch w e have to make transistor OFF which equivalent to the logical “0 “0” and SATURATE which is equivalent to the logical “1 “1 “. One of the famous diagrams that show the t ransistor operating operating state is called the transistor static characteristic curve as shown on this following picture: C Programming for Microcontrolle Fea... Joe Pardue Best Price $29.34 or Buy New $36.46
Privacy Information
C Programming Language Brian W. Kernighan... Best Price $22.84 or Buy New $41.51
Privacy Information
Feature Product
When we operate transistor as the class A common emitter amplifier usu ally we choose to bias the transistor (apply voltage on V BE 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 (i.e. near 0.2
PICJazz 20PIN Board
Categories
Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
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).
Blogroll ermicro shop ermicroblog Amazon Store ermicroblog on YouTube ermicroblog video on Metacafe
Archives
Ads by Google
Transistor PNP Transistor
The above diagram show a typical microcontroller interface circuit using NPN t ransistor; the RB resistor is used to control the cu rrent 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 induct or 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 h igh voltage generated by the in ductor component, you can use any general purpose diode with capable on handling minimum 1 A of current such as 1N4001, 1N4002, etc.
On the picture shown above you could see how we connect the transistor as the high active switch (logical high) also known as low side switch using NPN transistor and the low active swit ch (logical low) also known as high side switch using PNP transistor. Ok let’s calculate each of the RB and RC value on this following NPN transistor circuit:
Using Transistor as a Switch | ermicroblog
On the circuit above we are going to use 2 N3904 (t he 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 following information: IC max = 200mA (this is m aximum value that will make your transistor smoked, in 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 satu rate) and VCE = 0 Volt. Using the 5 volt power supply (VCC ) and assuming V LED = 2 Volt, with each of them consuming 15 mA, we could calculate the RC value using t he Ohm’s law as follow: IC = 5 x 15 mA = 75mA (0.075 A), this current is still far bellow the maximum I C allowed by 2N3904 transistor. RC = (VCC – VLED ) / I C = (5 – 2) / 0.075 = 40 Ohm Power Dissipation on the R C resistor will be P = (VCC – VLED ) x I C = (5 – 2) x 0.075 = 0.225 Watt Base on the above calculation we could use the n earest higher value available on the market; w hich is 47 Ohm, 0.5 watt resistor (for heat dissipation usually we use twice of the watt value calculated). Assuming the hFE minimum is 100; the minimum current required in the transistor’s base terminal to drive the LED is: IC = hFE x I B IB = I C / 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) / I B 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 supp ly): 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
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
Base on the result you could use 4K7 Ohm, 0.25 Watt resistor (this is th e 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 realRB value could be lower than 4K7 if you really want to drive the transistor into its fu lly 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 thi s following testing circuit base on the RC and RB calculated value 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 4 7 Ohm resistor or if you only have 150 Ohm as I did, you could use them as I did.
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
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
#include int main(void) { // Initial I/O DDRB |= (1<
// Set PB3 as Output, Others as Input // Reset the PB3 // // // // //
Loop Forever Port PB3 High Delay 3 Second Port PB3 Low Delay 1 Second
// 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:
Using Transistor as a Switch | ermicroblog
As you’ve seen from the result t here 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 i s 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 th e PWM (Pulse Width Modulation) with p eriod for about 300 us (micro second) and duty cycle for about 2%, the reason to use this very short pulse period method in the measurement is because they don’t want to overheat the transistor junction; where this junction heating will vary the transistor hFE measurement significantly. On my test circuit above; I used 3 second t o make the 2N3904 transistor ON (saturate, VBE = 0.81 Volt, VCE = 0.4 Volt) and 1second t o make it OFF. The other factor that make the t est result differ is the various manufacture specification even th ough 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 to use 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) by 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 for 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 = I C / hFE = 0.075 / 10 = 0.0075 A (7.5 mA) RB = (4.2 – 0.7) / 0.0075 = 466.66 Ohm By 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 its full saturate state (i.e. VCE = 0.2 Volt), because when the transistor is fully saturate, it tend to have a longer switching tim e (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 sti ll take advantage of the low transistor base current (i.e. I B = 0.88 mA). You could see this test circuit on the video at the end of this article. B. Driving the Relay Relay perhaps is one of the oldest electronic components that could be tracked back from the early years when we first use the electricity in our life. A relay basically is an electrical switch that uses the electromagnetic solenoid to control a switch contact. Because it use the solenoid (inducti ve load), therefore we need to use a diode to protect the transistor against the EMF. The main advantage of using a relay is that we could “relaying” or pass on the switch effect from a low power side on its solenoid t o the high power side on its metal contact by using the electromagnetic effect, where both of the solenoid
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
and contact has its own separate electrical specification.
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 60m A operating current: RC = 0 Ohm (not use, connect relay directly to VCC) IB = I C / hFE = 0.06 A / 100 = 0.0006 A RB = (VPORT – VBE) / I B = (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 i s 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 t o have small hFE mostly less then 20, so it’s mean we have to sup ply bigger base current. We know that most microcontrollers I/O port can only supply a current up to 20mA, therefore by using this type of transistor the maximum current that we could achieve in the collector terminal is about 400mA; which is far bellow our expectation. The solution for this situation is t o use what known as Darlington pair circuit:
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
By using the Darlington pair circuit we could combine two transistors; one with high hFE2 factor usually has a low collector current and the one with high collector current usually has a low hFE1factor. This will give you a total hFE of hFE1 x hFE2. In the Darlington pair circuits the V BE will be twice the normal transistor saturated voltage which is about 1.4 Volt. One of the popular ready made Darlington pair transistors on the market are TIP120 (NPN type) and TI P125 (PNP type) which could handle the collector current up to 3 A (max 5 A), and has the hFE minimum of 1000.
The TIP120 and TIP125 is called a pair Darlington transistors as they have similar characteristic but have an opposite type (i.e. NPN and PNP), this Darlingt on transistor pair is popular used in motor controller with the H- Bridge circuit. Remember when you use a power transistor to drive a large collector current, you need to supply the transistor with the adequate heat sink to help cooling the transistor by dissipating heat through the heat sink surface int o the surrounding air. Using the same principal we’ve learned b efore, we could easily calculate the RB value of the DC motor circuit interface bellow:
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
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 = I C / hFE = 1 A / 1000 = 0.001 A RB = (VPORT – VBE) / I B = (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 p air transistor you could use th e Texas Instrument ULN2803A which is contain 8 Darlington p air 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 u p to 1 A (2 x 500mA, remember this is a maximum datasheet value, for practical application use just half or 2 x 2 50 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:
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
Thanks to the build in internal 2K7 base resistor and the two 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 t o isolate our microcontroller circuit from the i nterface circuit especially in the environment that generating a lot of n oise which could disturb our m icrocontroller operation. When we use a relay from the above example, the driver ground is still directly connected to the m icrocontroller 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 t he hFE of 500 (in the optocouplers terminology this is also known as the t ransistor 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; inst ead we use the int ernal 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 t o drive this infrared LED will effect more current to fl ow on the phototransistor collector; This effect is known as the current transfer ratio (CTR). The 100% CTR means that all the cu rrent flow on the infrared LED will be t ransferred 100% to the phototransistor collector. Therefore by driving the internal infrared LED with 15 mA (in the optocouplers terminology this is also
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
known as the input diode static forward current), we could assure that the phototransistor will be in the saturate state (ON), because the minimu m current to make the ph ototransitor on is about 10 mA. The following circuit is use optocoupler to int erfacing the relay:
By using 5 Volt power supply and relay with 5 Volt and 60m A 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 ) / I B = (4.2 – 2) / 0.015 = 146.66 Ohm, use 150 Ohm resistor P = (VPORT – VLED) x I B = (4.2 – 2) x 0.015 = 0.033 watt, use 0.25 Watt resistor If you need to drive m ore 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 t he relay to switch the DC m otor’s voltage polarity, but using this method means t he DC motor will always ON and we can not control the DC m otor speed using digi tal 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:
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
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 g oing 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 th rough TR3, passing the DC motor in reverse polarity and going int o the TR2 transistor (red color). By not applying current to both I B1 and I B2 all the transistors will be t urned 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 suppl y and DC motor with 5 Volt and 1 A maximum operating current rating; assuming the TIP120 Darlington transistor hFE is 1000, the RB1 and RB2 resistors could be calculated as follow: IB = I C / hFE = 1 A / 1000 = 0.001 A, for each of the transistor base current RB1a,b = (VPORT – VBE) / I B = (4.2 – 1.4) / 0.001 = 2800 Ohm, use 2K2 Ohm resistor RB2a,b = (VPORT – VBE) / I B = (4.2 – 1.4) / 0.001 = 2800 Ohm, use 2K2 Ohm resistor P = (VPORT – VBE ) x IB = (4.2 – 1.4) x 0.001 = 0.0028 watt, use 0.25 Watt resistor for RB1 and RB2 To test the TIP 120 Darlington transistors H-Bridge circuit above I used this following circuit using Atmel AVR ATTiny13 microcontroller as shown on this following pic ture:
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
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.2; avr-libc 1.6.2 (WinAVR 20090313) // IDE : Atmel AVR Studio 4.17 // Programmer : Atmel AVRISPmkII // Last Updated : 18 June 2010 //*************************************************************************** #include #include int main(void) { // Initial I/O DDRB |= (1<
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
PORTB &= ~(1<
// Reset PB3 (OFF) // Reset PB4 (OFF)
for(;;) { PORTB |= (1<
// // // // // // // // //
Loop Forever Turn ON PB3 Delay 3 Second Turn OFF PB3 Delay 2 Second Turn ON PB4 Delay 3 Second Turn OFF PB4 Delay 2 Second
// Standard Return Code
} /* EOF: trhbridge.c */
One of the advantage using all NPN transistors in the H bridge circuit i s the NPN transistor tends to h ave faster turn on time comparing t o 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. Actually most of the modern H-Bridge circuit design for higher voltage (e.g. more than 9 volt) is rarely use the BJT anymore; instead we use the MOSFET because MOSFET is more efficient on higher voltage (i.e. less power dissipation) compare to the ordinary BJT. The other advantage of using MOSFET is t hat it has very high input impedance, therefore we could easily connect parallel a couple of the same MOSFET to achieve the higher current output and at the same time we could decrease the output resistance of the MOSFET (Rds), which m ean we could get more lower power dissipation as shown on thi s following picture:
The “The Line Follower Robot with Texas Instruments 16-Bit MSP430G2231 Microcontroller” article is a good example of how we use the N -Channel MOSFET to control the DC motor. Driving the Stepper Motor One type of the brushless electric motor that is designed specifically for digital signal input is called the stepper motor. The stepper motor usually is used when we need to control the precise rotation movement and speed with t he open loop control. These advantages make the stepper motor is widely found in many applications such as print ers, scanners, disk drives, automotives, CNC machines, toys, and many more. Today the most common used stepper m otor types are Unipolar Stepper Motor and Bipolar Stepp er Motor. The unipolar stepper motor usually has two windings with a center tap on each of windings, therefore the current could move from the center tap either to the left winding or to the right winding. Usually the unipolar stepper motor comes with 5 or 6 t erminal leads. On the other hands the b ipolar stepper motor actually is similar to the unipolar type but without the center tap.
Using Transistor as a Switch | ermicroblog
Therefore the unipolar stepper motor has advantage of more simple driving circuit over the bipolar stepper motor but has a torque less t han the bipolar motor for the same size. The following circuit uses four BC639 transistors to drive the unipollar stepper usin g the Atmel AVR ATTiny13 microcontroller to provide the required stepping signal to the unipolar stepper motor:
From the schematic above you could see that each t ransistor is connected to half windings of t he unipolar stepper motor phase. You need to experiment with your own stepper motor to get the right windings connection. Assuming maximum 100 mA unipolar stepper motor current on 5 volt supply, and using minimum BC 639 transistor hFE of 40, we could calculate the RB (the base resistors) as follow: IB = IC / hFE = 100 mA / 40 = 0.0025 A RB = (VPORT – VBE) / IB = (4.2 – 0.7) / 0.0025 = 1400 Ohm, use 1K5 Ohm resistor P = (VPORT – VBE) x IB = (4.2 – 0.7) x 0.0025 = 0.0086 watt, use 0.25 watt resistor Two clamp diodes on each transistor are required because the win ding has a center tap. Therefore when one end of winding is high (Vcc) the other end is low (GND) the lower diode will bypass the back EMF (Electromotive Force) voltage that appear on BC 639 transistor collector and emitter terminals. This following is the C code is used for testing the circuit above: //*************************************************************************** // File Name : upstepper.c // Version : 1.0 // Description. : Transistor as Switch: Simple Unipolar Stepper // Motor Driver - Full Step Method // Author : RWB // Target : ATTiny13
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
// Compiler : AVR-GCC 4.3.2; avr-libc 1.6.2 (WinAVR 20090313) // IDE : Atmel AVR Studio 4.17 // Programmer : Atmel AVRISPmkII // Last Updated : 03 Nov 2010 //*************************************************************************** #include #include #include // Unipolar Stepper Motor CW/CCW Stepping Sequence #define MAX_STEP 4 unsigned char cwstep_seq[MAX_STEP]= {0b00000110, 0b00000011, 0b00001001, 0b00001100}; unsigned char ccwstep_seq[MAX_STEP]= {0b00001100, 0b00001001, 0b00000011, 0b00000110}; volatile unsigned char step_index; volatile unsigned int ovftimes; volatile unsigned char status; ISR(TIM0_OVF_vect) { static unsigned int count=1; count++; if (count >= ovftimes) { cli();
// Disable Interrupt
// Stepping Output if (status) PORTB = ccwstep_seq[step_index++]; else PORTB = cwstep_seq[step_index++]; if (step_index >= MAX_STEP) step_index=0; count=0; TCNT0=0; sei();
// Reset Count // Start counter from 0 // Enable Interrupt
} } int main(void) { // Initial I/O DDRB = 0b00001111; PORTB = 0b00000000;
// Set PB0, PB1, PB2, and PB3 as Output, Others as Input // Reset PORTB Output
// Set ADCSRA Register on ATTiny13 ADCSRA = (1<
// // // //
step_index=0; ovftimes=10; status=0;
// 0 - CW, 1 - CCW
sei();
// Enable Interrupt
Timer/Counter 0 Normal Operation Use prescaller: Clk/8 with 9.6 MHz Internal Clock Start counter from 0 Enable Counter Overflow Interrupt
for(;;) { // Loop Forever // Start conversion by setting ADSC on ADCSRA Register ADCSRA |= (1< Complete
Using Transistor as a Switch | ermicroblog
while (ADCSRA & (1< 800) status^=0x01; _delay_ms(50); } return 0;
// Toggle the Direction
// Standard Return Code
} /* EOF: upstepper.c */
The method to rotate this u nipolar stepper motor rotor is known as the full step mode method; in full step mode we always excite two windings at the same time, with the right current sequence we could rotate the stepper motor in 4 repeatable steps. Reversing the step sequences will make the stepper motor to turn into opposite direction. In this tutorial I used NMB-PM20S-020 permanent magnet motor where the step required is shown on this following diagram:
You could easily adapt the st ep sequence to your own unipolar stepper motor by changing both the cwstep_seq (clockwise rotation) and ccwstep_seq (counter clockwise rotation) array variables data in the program. Using the AVR ATTiny13 microcontroller TIMER0 interrupt we could easily supply th e required output
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
steps to the st epper motor. I used the ADC (Analog to Digit al Conversion) to control the stepper motor step sequence delay as well as to change t he rotation direction by adjusting the 10K t rimport. For more information about AVR ADC and TIMER0 you could read these blog’s articles “Analog to Digital Converter AVR C Programming” and “Working wit h AVR microcontroller Communication Port Project “. You could also replace th e BC639 t ransistor with the Darlingt on pair transistor array such as ULN2803A from Texas Instrument mention above. Using this Darlington pair transistor array make the unipolar stepper circuit become simpler (less components) because it has the required clamp diode on each Darlington transistor pair and you could take advantage of higher current gainer provided by the Darlington pair transistors. To drive the bipolar stepper motor; each of the two windings will require the H-Bridge ci rcuit similar to the H-Bridge circuit for driving the DC motor mention above. Therefore we need at least 8 t ransistors to drive the bipolar stepper motor (4 t ransistors on each windings). By forwarding and reversing the current flow on each winding we could achieve the required steps sequence t o drive the bipolar motor.
And by supplying the correct steps sequence logic to IN1, IN2, IN3, IN4, IN5, IN6, IN7 , and IN8 input from the microcontroller output port we could make this bi polar stepper motor to rotate. The opposite direction (counter clockwise) rotation could be achieved by reversing the steps sequence (i.e. 4, 3, 2, and 1). Using Transistor as switch Testing Circuit Video 1. This following video show you of how to drive a transistor which connected wit h 5 red LED using the Atmel AVR ATTiny25 microcontroller.
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
2. The TIP120 H -Bridge Testing Circuit video usin g Atmel ATTiny13 Microcontroller:
3. The Unipolar Stepper Motor Testing Circuit video using Atmel ATTiny13 Microcontroller:
http://www.ermicro.com/blog/?p=423
Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
Bookmarks and Share
Related Posts H-Bridge Microchip PIC Microcontroller PWM Motor Controller Working with the Comparator Circuit Build Your Own Microcontroller Based PID Control Line Follower Robot (LFR) – Second Part Build Your Own Transistor Based Mobile Line Follower Robot (LFR) – First Part Simple and Easy Light Emitting Diode (LED) Tester
Stepper motor parts Replacement Motors For HMC/VMC/CNC Machines, Express Delivery www.GlobalMachineParts.com/Motors
16 Responses to “Using Transistor as a Switch” 02.09.09
#1
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 c onnections 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 R B2 then it does work. I’m not sure I understand why yet maybe somebody can explain.
02.09.09
Comment by rwb. The original TIP120 H-Bridge schematic has been changed; now I used resistor on each of t he TIP120 base terminal as
#2
Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
you did. Thank you
23.01.10
#3
Comment by mandomoose. Thankyou for this great post. I was wondering about using the transistor as a swit ch with my avr.
Keep having fun
20.07.10
#4
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
20.07.10
#5
Comment by rwb. You should h ave 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 th e 12 volt which is used t o power the Darlington t ransistor and the DC motor. In order to make the d arlington 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.
21.07.10
#6
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 th ese resources… kansai
Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
21.07.10
#7
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 m otor (without or have a very small load). When using t he Toshiba TA7291S bridge all you need is t o 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).
21.09.10
#8
Comment by ajoyz124. Very illustrative, basic and simple facts required to work with micro controls. Thanks.
08.05.11
#9
Comment by topx666. Lhank you Mr. Besinga for creating this tu torial. please let me introduce my self. my name Taufiq Sunar. I am a student of Electronics and I nstrumentation in Gadjah Mada University, Indonesia. Currently I’m trying to make a switch using MOSFET with input from the AVR microcontroller PWM. I’m planning to use MOSFET series IRF740, IRF9530, IRF9540, or IRFZ44. Whether working principle and the calculation of MOSFET is similar to BJ T transistor? Thank you very much for your answer!
08.05.11
#10
Comment by rwb. Nice to know you Taufiq. Bipolar Junction Transistor (BJT) is different compared to Metal Oxide Semiconductor Field Effect Transistor (MOSFET), therefore all the calculation for BJT could not be applied to MOSFET. BJT will amplify the input current (IB) by the current gain factor (hFE) on it’s output (IC) as follow: hFE = IC / IB On the other hand MOSFET will amplify the input voltage (V GS ) by the transconductance gain factor (gfs ) on it’s output (I DS ) as follow: gfs = (change in IDS / change in VGS) Where VDS : gate to source voltage and I DS : drain to source current. As you notice there is no I G (gate current) on the MOSFET
Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
gain factor formula; because of the issulation oxide on the MOSFET gate terminal design, therefore there is no current flow on the MOSFET Gate terminal (MOSFET is also known as a very high input impedance transistor). The IDS (drain to source current) will start t o increase when the VGS voltage is above the VGS threshold voltage. Any small VGS voltage change applied to the MOSFET Gate input terminal will be amplified by the factor of g fs and reflected as a Drain current on the MOSFET Drain terminal side. Therefore by supplying VGS much greater than VGS threshold voltage, we could easily push the MOSFET into it s saturate region. This make using the MOSFET is more simple compared to BJT as all you need is to connect the MOSFET gate terminal directly to the AVR microcontroller output port. The AVR microcontroller output port voltage (high logic) could easily drive the MOSFET into its saturate region, but m ake sure you always read the MOSFET datasheet especially t he VDS , IDS , gfs , and V GS threshold when using different type of MOSFET. I hope this answer will clarify the differences between BJT and MOSFET
10.05.11
#11
Comment by topx666. Thank you very much for t he explanation Mr. Besinga. Actually, I want to make a high-speed switch from AVR microcontroller PWM. PWM output is squarewave. Whether after connected into the MOSFET, its output also a squarewave? Because I’ ve tried usin g a IRF 740 MOSFET, and 8-bit fast pwm. But the results was not squarewave and its currents and voltages are very small. The PWM in put was connected by 4k7 resistor to its gate. And its source supplied by 12V with 10k resistor.
10.05.11
#12
Comment by rwb. Yes the output should be a square wave when the input is a square wave. You could read more about AVR Fast PWM on “Working with Atmel AVR Microcontroller Basic Pulse Width Modulation (PWM) Peripheral” article. Actually you don’t need to use 4K7 resistor (current limiter) in series with t he MOSFET Gate terminal, because there is no “Gate Current”, therefore you don’t have to “reduce” the current as it in BJT. If you want t o use resistor use the voltage divider circuit (2 resistors) instead of one resistor. As long as the VGS > VGS threshold and VGS > VDS , the MOSFET will turn ON.
28.06.11
#13
Comment by drogge. I’ve been trying to make a H-Bridge circuit using 4 TIP120′s as per the above diagram and everything works fine if the motor voltage is around 5V. However if I raise the motor voltage to 9 volts I only see about 3.5 volts at the motor. The problem seems to be that the TIP120 can switch the motor to ground but it can’t switch the full 9 volts when it’s base is 5
Using Transistor as a Switch | ermicroblog
http://www.ermicro.com/blog/?p=423
volts. In other words the circuit labled Using TIP120 Darlington Transistor for Driving Motor works but if I move the transistor between V and the motor it doesn’t work. It looks like I need to use something like TIP125′s for the top transistors in the H-Bridge.
29.06.11
#14
Comment by rwb. Yes, the all TIP120 H-Bridge circuit only work when the base and collector has the same power supply voltage. Because the top TIP120 (NPN) transistor is in common collector configuration and in order t o turn ON th e transistor, we need to forward bias the VBE , therefore if the base voltage is less then the collector voltage it will not turn ON. When you use the TIP125 (PNP) for the top transistor then all the transistors will have the same common emit ter configuration. I will do some correction and add example on the article above, thanks for your comment.
06.12.11
#15
Comment by akhb. Hi there, This is a very informative post. Your hard work is very appreciated by everyone who luckily l ands here. I receive an active low error signal for a chip. The signal and Vcc on board is +5V. I want to light a blue LED if the signal is low. So I guess we use PNP transistor. I was looking at digital transistors. (they have biased resistors pre-built-in). Can you suggest something like PDTA143E w here the resistors and t heir ratio is all correct for +5V power supply? Thank you in advance.
06.12.11
#16
Comment by rwb. You could use any PNP potential di vider type of digital transistor as long as you choose a suitable Ic max for your need.
Leave a Comment
You must be logged in to post a comment. Copyright © 2008-2012 By ermicro. Powered by Word Press.