Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 microcontrollers 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 (UniJunction 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 h FE and reflected as a collector current on the collector
terminal side.
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
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 RCresistor 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.
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 switch (logical low) also known as high side switch using
PNP transistor.
Ok lets calculate each of the RB and RC value on this following NPN transistor 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
following information:
IC max = 200mA (this is maximum
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
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 Ohms law as follow:
IC = 5 x 15 mA = 75mA (0.075 A), this current is still far below 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).
Assuming the hFE minimum is 100; the minimum current required in the transistors 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 Ohms 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
Now the question is how we determine the exact value? To answer to this question I build this 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 = 3150 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
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 youve 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 of100 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 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 dont 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 to make the 2N3904 transistor ON (saturate, V BE = 0.81 Volt, VCE = 0.4 Volt) and
1second to make it OFF. The other factor that make the test result differ 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 to use the collector to base current ratio of 10 (regardless of the transistorhFE 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 youve seen from the real test circuit result above even though we drive the V BEmore 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 Microcontrollers 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
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 dont drive the transistor into its full saturate state (i.e. V CE = 0.2
Volt), because when the transistor is fully saturate, it tend to have a 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. I B = 0.88 mA). You could see this test circuit on the video at the
end of this article.
A. The Switch
B. Driving the Relay
C. Increasing the Collector Current
D. The Darlington Transistor Array