Sunday, 30 September 2012

How do you use NPN Transistors

After doing a post on how to use Field Effect Transistors I suppose I should really do a post on Bipolar transistors (BJTs) and how they are used.  I'm keeping things very simple....I have seen a fair few of these tutorials in my time and I thought I would add my efforts to the mix.  This post concentrates on NPN transistors.  I'll talk about PNP transistors in the next post.

So what are Base Junction Transistors?  These are the original and best semi-conductors ever (in my opinion).  They were invented in 1947 by a team at Bell Labs....Here is the wikipedia entry -

So BJTs are three terminal devices made up of three pieces of semi-conductor material - a type of silicon with less electrons and therefore more positive (P-Type) and a type of silicon with more electrons (N-type).  There are two main groups of transistor - NPN and PNP.  NPN transistors are made up of a layer N-type material and then a layer of P-type material and then another layer of N-type material.  PNP transistors are the same only in reverse.

Check out the diagrams:

So.....what are transistors used for? Well they can be used for two things - amplifying current or as a semi-conductor switch.  They are used in literally thousands of electronic circuits.....They can be used for controlling devices, amplifying signals or turning on other parts of circuits...

So how does a transistor work?  I'm keeping this simple if people require more detail and the physics then check out the awesome sites below:

For NPN transistors greater than 0.7V is applied to the base terminal.  This allows current to flow from the collector to the emitter.  Check out the diagrams below:

When there is less than 0.7V applied to the base no current can flow between the collector and the emitter.

So...why does this matter?  Well when 0.7V is applied to the base of the NPN not only does it allow current flow (switching action) it also allows more current to flow (amplification).  Lets try and give an example, check out the circuit diagrams below:
NPN transistor circuit with Voltages 

NPN Transistor circuit with Currents
So we can see from the diagrams that the voltage applied to the base controls the amount of current that can flow from the collector to the emitter.  The two resistors on the left of the diagram are connected as a voltage divider to the base terminal.  This is to ensure that there was a voltage at the base of the transistor greater than 0.7V.  This is known as 'biasing' the transistor on. It means that the transistor will always be conducting between the collector and emitter.  The resistors at the collector and emitter were selected to control the amount of current 'gain' the circuit will have.  If we change those resistors we can vary the amount of gain or amplification that the circuit will have.

The current in the base terminal of a transistor is known as Ib or I base (I being the symbol for DC current and b for base).  The current in the collector of a transistor is known as Ic (DC current at the collector).  The amount of 'gain' the transistor has is based on it's internal resistance.  When the base is provided with 0.7V the resistance from the first semi-conductor junction is transferred to the other junction.  This is why transistors are sometimes known as transfer-resistors and then the name was shortened to transistor.  Anyway so we can see in the above circuit that when there is 8.438 * 10^-6A or 8.438 micro-amps on the base there is 2.554 * 10^-3A or 2.554mA flowing through transistor from the collector to the emitter.  

2.554*10^-3 A / 8.438*10^-6 A = 302.67

So that's an increase in current of 303.  This figure is known as the DC current gain or Hfe.  These are the terms used by manufacturers of transistor to explain to design engineers how a transistor will perform when connected in a circuit.  The physicists and mathematicians out among us will notice that there are no units for Hfe, that is because Amps divided by Amps cancels the units to be dimensionally correct.

If we look at the datasheet for the BC548 transistor we will see what the manufacturer (fairchild) states for the Hfe:

The datasheet shows a range for Hfe for the BC548 transistor.  That is because the amount of current gain will vary between versions and batches or transistors.  Not all batches manufactured will be the same and therefore the amount of gain will differ slightly but the transistors are tested when they are first made and will all be roughly within this range.  The datasheet reads for a BC548B transistor the Hfe will typically be between 200 and 450.  We calculated 303 - within specification then!

So what other parameters do we need to look at on the datasheet to use a transistor?  The list below is by no means complete but its enough to get started:

VCEO - Collector Emitter Voltage - Maximum Voltage you can place at the collector
VCBO - Base Collector Voltage - Maximum Voltage you can place at the base
VBEO - Emitter Base Voltage - Maximum Voltage you can place at the emitter

Ic - Collector Current - Maximum current you can cause to flow through the transistor from the supply

Pc - Power dissipation - Amount of power the transistor will dissipate when fully on

There are other figures and graphs on the datasheet which all provide information on how the transistor will behave when in use.  The stuff I'm normally interested in is VCEO as it tells me what supply voltage I can use; The VCBO is useful as it tells me how much signal I can apply to the base without damaging the transistor and Hfe because it tells me how much gain I can expect if I turn the transistor fully on.  The maximum temperature specification is also important.  

So how do you use an NPN transistor as a switch?

To use a transistor as a switch we need to first know what we are switching on and off.  For this example I'm going to show how to control when an LED turns on and off.  First of all we need to select a transistor - lets go with the BC548B as seen earlier.  It saves me having to provide another link!  Lets choose a supply voltage - 12V.  Lets then choose how bright we want the LED to be - select the LED current limiting resistor, for this example I've used a 1k resistor.  Then all we have to do is create the circuit - check out the diagrams below:

Left - NPN transistor OFF Right NPN Transistor ON

The supply attached to the to the base terminal is the symbol I use for a signal (variable) DC voltage source.  Every other symbol is standard.  

Basically, if you apply a positive voltage to the collector and the item to be switched and connect the 0V to the emitter and a voltage greater than 0.7V to the base the transistor will switch on.  I used 2.5V in the example above.  Here is a video showing the base voltage controlling whether the transistor is conducting (ON) or not conducting (OFF).  

So how do you use an NPN transistor as an Amplifier ?

Using the transistor as an amplifier is more complicated...a lot more complicated.  It all depends on what kind of amplifier is required.  There are many different types of amplifier and it's a subject for another post.  I am going to show a simple amplifier circuit - its called a class A amplifier and its used for amplifying a changing signal like an audio signal or an analogue sensor signal.  Lets set some parameters: Say we have an audio signal coming from a microphone that's at 0.5V.  In order to measure that signal or make it audible we are going to need to amplify it.  So lets set the gain of the amplifier to 6.  That means that the signal we expect to receive at the output of the amplifier is going to be around 3V (the input signal will be increased 6 times).  Lets set the load connected to the output to be 300 ohms. For this example lets use the BC548B again.  The circuit is shown below:
Class A Amplifier circuit using a BC548B with a gain of 6
Lets quickly explain the circuit.  R1 and R2 set the base of the transistor to On by setting the DC voltage applied to the base terminal to 1.6V, This can be verified using Ohms law and the voltage divider rule.  R3 and R4 set the gain of the amplifier.  The capacitors C1 and C3 are DC blocking capacitors which are present to stop the DC signal interfering with our AC input signal.  C2 is an emitter follower capacitor.  Its job is to improve the amplifier output (really a topic for another post!). V1 is the supply voltage and V2 is the simulated voltage signal from our microphone set at 0.5V.

Lets check out the simulation!

Well that's all for now folks.  I will upload some videos showing the actual circuits on a breadboard working in real life - Take care and have fun! 


Thursday, 27 September 2012

Current Limiting for the Motor Driver

Lets recap shall we!

Last post was on Field Effect Transistors (FETS).  We covered the fact that they are many and varied, are a voltage controlled device and can be used to amplify current or as a kind of semiconductor switch.  The key to using FETS is to read the datasheet!  The parameters to play close attention to are:

Vds - Voltage between drain and source
Rds - Resistance between drain and source
Vgs Threshold - Voltage at which device will allow current to flow between drain and source
Ids - Amount of current that device will allow to flow between drain and source
Operating Temperature - pretty self explanatory (do not let device get hot - use a heat sink)
Supply Voltage Range - ensure device is rated for supply!

Type - There are several different types of FET, Junction, MOS Insulated Gate, then there are sub types:

N - Type - Enhancement, N - Type Depletion, P Type Enhancment and P-type Depletion.

The most important things to find out are:

Supply Voltage - Don't overdrive device
Vgs Threshold - know when the device turns on or off for control
Ids - How much current can device provide (source)

Everything you need to know should be on the datasheet - as with all semiconductors.  It would be nice if datasheets were better written but....good application engineers aren't normally very concise or good at technical writing - here is hoping things improve!

I'm not saying that the other parameters aren't important - they are otherwise they wouldn't be on the datasheet....I believe the three parameters above are the most important to get started.  Once the principle works the other parameters can be taken into account.  Simulation is the designer's friend!  It costs nothing (except for the software and time) and no devices are destroyed :-)


So last time I posted the lads at my local Hackspace were looking for an FET to drive an old electric Scooter.  After thinking about things I came up with a simple circuit using an IRF540 N-type Enhancement Mosfet.  Now I'm going to add current control with a pass transistor so that we can ensure that no matter what happens the motor can never draw too much current and destroy components.  This circuit could also be used to add current control to a variable power supply.  Here is the schematic diagram for the current limiting section:

When I was studying electronics at college my lecturer's went over this circuit again and again.  At the time I thought I understood exactly what was going on....Now I'm not so sure.  Here goes:
The current limitation circuit is made up of 4 transistors, a variable resistor and two resistors.  T4 is a high current 'pass transistor', It's specifically designed to control high currents.  We control how it is turned on and off with the three general purpose NPN transistors.   The resistor R1 is known as the current sense resistor and is rated to take a large amount of current flow without being damaged.   

When power is applied to the circuit at the input the NPN transistor T2 is turned on, this turns on T3 which then turns on T4 the 'pass transistor'.  The 4.7k resistor is called a bias transistor which forces the T1 transistor on when a positive voltage is applied to the collector and base.  

Because the 'pass transistor' T4 is now on current can flow to the output.  The current flow causes a voltage drop to occur across the high power resistor R1.  If this voltage drop is greater than 0.7V then the transistor T3 is turned on which turns all of the other transistors off preventing any current flow at the output.  The current limit (point at which T3 is turned on) is set by the 10k variable resistor or potentiometer R6. The other resistor R5 is the biasing resistor for T3.      
Here is a video simulating the circuit:

I hope that made some sense to people....If you get stuck I recommend taking a look at the awesome all about circuits site:

So now what we need to do is incorporate the FET driver section that we designed in the last post.  Here is the schematic with the motor driver incorporated.  You may notice I have connected the potentiometer to the other side of the 'current sense' resistor.  This is because we don't need to limit the current completely down to zero.  I just want to limit the current from going too high.   

Next up is the small PCB that I have designed to test the circuit and maybe use in the scooter.  I haven't decided.  We might want to incorporate the micro-controller onto this PCB also so that we can contain the electronics all in one place.     

Well...that's about it for now.  I hope to get this PCB made and tested soon! Until then have fun and be careful - Alex

Monday, 24 September 2012

Using Field Effect Transistors!

Over the weekend I was at my local Hackspace -

We were all working on different projects and generally helping each other out - hackspaces are great places for some communal assistance, banter and access to workshop space and tools!  If you have one in the local area and are interested in any kind of science or engineering I recommend having a look!

Anyway the lads were trying to get an old electric scooter working.  We knew that the motor was working but that the motor driver PCB had failed.  So what we needed to do was to make a new driver circuit.  We decided to try a few options but without information as to how much power the motor used when in operation and under load we were a bit stuck.  So what with being excited and impatient we connected an off the shelf motor driver PCB (I think it was one of these - and wired it up.  It did work with a test sketch.  We made the motor go forwards for three seconds and backwards for three seconds.  There were problems though.  The L298 motor controller device became EXCEEDINGLY hot!  This was because the motor we were driving had a much higher power rating than the motor driver could provide (Whoops!).  We also may have performed a locked rotor test which made the magic smoke appear!!

So after letting the steaming pile of junk (motor controller) cool down we removed it and powered the motor straight from a high current DC power supply.  We inserted an ammeter into the circuit so we could measure the current draw of the motor when it was running.  We also measured the current draw when the motor was stalled (locked rotor test)  We found that when the motor was running it drew about 3A which dropped as the motor ran for a while.  We found that when we stopped the wheel turning (locked the rotor) we found that the motor drew 19A!!!  The circuit diagram for those that are interested is below:

So....what does all this mean? 

It means that when the motor is not loaded (Nobody standing on the scooter and the accelerator pushed) the motor will draw 3A of current from the battery (supply).  When someone is stood on the scooter the motor will draw more current (lets say 10A - at a guess) and when someone brakes or is going up a steep hill the motor will draw 19A.  We need a way of providing at least 10A to the motor but in a controlled manner (we need to be able to control the speed of the scooter!).  We also need a way of making sure that the motor cannot draw more than say 16A - this is to prevent the battery being run down in a hurry and to stop any control electronics being burnt out when someone has to brake in a hurry.

Our scooter only needs to go forward so we don't need to use a motor H bridge controller (like the L298 motor driver circuit) we can use a simple transistor driver circuit.  We could use a high current bipolar transistor but in this case it is better to use a Field effect transistor.  

For those that don't know much about transistors I recommend taking a look at the following websites:

The differences between Bipolar Transistors and Field effect transistors are many and varied. The main differences are:

Bipolar Transistor - also known as a bipolar junction transistor or BJT 
A BJT is a current controlled device
A BJT is made up of three different pieces of silicon - with three terminals: Base, Collector and Emittor
Switching speed is slow compared with a FET
Bipolar transistors use less power than a FET
Bipolar transistors are not affected by Static Electricity
More commonly used for low current switching and amplifiers although there are plenty of devices which will perform high current operation.

Field Effect Transistor
A FET is a voltage controlled device
A FET is made up of a single piece of silicon with a channel in the middle (unipolar) but still has three terminals called Gate, Drain and Source.
FETS can switch more quickly than BJTS
FETS are more commonly used for high power operation and switching.
FETS are affected by Static electricity and care should be taken when using them.

So we need an FET to provide the current control for our scooter motor - Lets pick one!  How do we go about deciding which one to go for?  There are literally thousands.....Lets define some parameters and learn to use the datasheets and parametric searches and choose one that way.  This is something every good electronics engineer should be able to do when designing circuits.

So what do we have in our circuit:

A 12V supply
A 200W motor - just a guess

What do we need the circuit to do:

A device to provide current control - it must be able to provide (source) 16A continually between drain and source (IDS) and be able to work with a 12V supply (voltage between drain and source or Vds).  In our case we wanted to control the FET from a microcontroller (arduino based) so it has to turn on (voltage between gate and source or Vgs threshold) with 5V.  As we were not intending to do any switching with this FET the other parameters aren't too important at this time.  Although keeping an eye on the maximum operating temperature and power dissipation is always a good plan!  For this device I thought we would use an N-channel enhancement MOSFET.  That's a metal oxide semiconductor field effect transistor that requires a positive voltage at the gate to make the transistor conduct between drain and source by opening the channel.....the terminology gets complicated quickly.  There is another flavour of MOSFET called N-type depletion that requires a positive voltage to close the channel.  Then you can also get P-type FETS as well.....I understand why people find it hard to use FETS....I know I struggle sometimes!

Anyway we have some parameters:

Vds - 12V or higher
Vgs threshold - up to 5V
Ids - 20A or higher   
N-Type Enhancement MOSFET

Lets do a search on the farnell website for an N-type Enhancement Mosfet:

It came out with loads of responses!  

The cheapest transistor which met our requirements was the IRF540PBF by Vishay Siliconix:

I'm going to use this transistor - Here is the datasheet:

The main reason for me choosing this transistor is....I have some in my junk bin.  It meets nearly all of our requirements:

VDS - 100V 
VGS Threshold - 4V
IDS - 33A
Power Dissipation - 94W - will need a heat sink!

So what we need to do now is make up a test circuit that shows how this device works and then use it to power our motor.  I'm going to use a 12V light bulb as a test load and simulate a circuit.  I'm going to use some generic resistor values and then show how to calculate specific resistor values.  Here is the circuit:

I'm using a 12V battery, a DC voltage source (will be provided by microcontroller), a 100k resistor - to keep the transistor always turned off, a 1k resistor - to set when the transistor will turn on, a diode to prevent back emf - more on this later and a 12V light bulb like a car headlamp.

As can be seen at the moment because there is no voltage applied at the gate of the FET the light bulb is not turned on and there is only leakage current flowing through the light bulb (12uA).  When the gate voltage is increased to 3.9V we can see some current being measured through the light bulb.  We would also be able to see the bulb filament glowing dimly.

If we increase the gate voltage further to 3.9V we now start to see some 'proper' current flow at 1.17A through the light bulb (any current flowing greater than an amp is 'proper' current flow in my book!).

Finally if we increase the gate voltage to more than the threshold voltage the light bulb comes fully on and we measure 4.06A flowing through the light bulb.
If we were to increase the gate voltage further to 10V we would find that we would still only get 4.06A at the light bulb.  This is because the bulb has a fixed power rating of 50W.  It won't draw any more power when supplied with 12V.  Our motor has a power rating of 200W so it can draw more power as required depending on the load or torque applied to the motor.

I don't say this often....lets do some mathematics!  There is a formula for calculating the output voltage from a FET.  Here is the formula:

Vout = Vs - (Ids * R1)


Vout is the voltage through the bulb
Vs is the supply voltage from the battery
Ids is the current between the drain and source of the FET

We know that Vout is also affected by the gate voltage.  When the gate voltage was 1V the current between the drain and the source was 12uA.  Lets plug these values into the formula:

Vout = 12V - (12uA * 1000R)

Vout = 11.988V

If we now increase the gate voltage to say 5V we get more current flow and therefore more voltage available at the output:

Vout = 12V - (12mA * 1000R)

Vout = 12V

What this shows is that if we re-arrange the formula for R1 we can calculate what value we want for a given voltage output, in order to do this though we need to know what Ids will be for a given Vgs.  If we look at the datasheet we can see there is a graph showing Ids over Vgs for a given supply voltage:

R1 = (Vs - Vout) / Ids

So if we want at least 10A (Ids) and 11V (Vout) at the output then we need to select a resistor of:

R1 = (12V - 11V) / 10A

R1 = 0.1R...not really practical, so lets not bother to fit a resistor in this case.  We would then get as much current as the supply can provide....

Most datasheets don't provide the graphs for Ids over Vgs at low voltages so it's a guesstimate really.  The resistor R1 is more often placed to prevent too much current flowing through the FET when it's in the non-conducting state.

In order to prove this is all real I built the circuit and tested it!  Here is the video proof!

Here is a further video of the circuit in operation being used to control a DC motor:

That's it for now.  In the next post we will add some current monitoring to the circuit and then implement this as our scooter motor controller!

Sunday, 23 September 2012

Power Supply in the Box working!

So after some time....I have been busy again!

I have finally placed the power supply in a case and added some volt meters and checked everything works. I have also updated the circuit diagram as I wanted to reflect the extra components that I've added.  Here is the updated diagram:

I added a 3A fuse on the live side of the 230V mains input.  This is to ensure that no matter what happens at the output no more that 3A can be drawn by the power supply at the mains input.  It's purely a safety issue and in my opinion is critical.  I also added a mains rated switch so that it is possible to isolate the power supply from the mains without removing the plug from the wall socket.  Next I earthed the metal parts of the case by connecting the safety earth  from the plug to all of the metal parts of the case and finally I have added some digital voltmeters.  I bought them off ebay from this vendor although there are plenty of them around:

delhanway2009 - ebay listing

Its a standard 3 wire digital voltmeter.  I was hoping to use one as a current meter when combined with a 1 ohm resistor but that hasn't worked.  I need to investigate how these meters works....Anyway they work perfectly for displaying voltage and that is really all we need for this power supply.  When I was building the power supply I took extra care to make sure that all the wires were correctly restrained and none of the low voltage DC wires could every touch the high voltage AC wiring or exposed parts of the circuit - heatshrink and electrical tape all the way!

Here is the completed power supply below:

I'm very happy with how it came out!  It works perfectly and provides plenty of current.  I upgraded the voltage regulator to an LM338 to make sure I can get about 5A if required!  The LM338 is exactly the same voltage regulator as the LM317 but it is able to provide more current.  The datasheet for the LM338 is here:

The power supply gives out at least 30V when loaded at 1A so it works well enough for all my linear power supply needs.  The regulator has built in over temperature and over-current protection so it shouldn't suffer from any of my mistakes and should last me for at least a good twenty years....Happy days!

Here is a quick video showing the power supply in operation.  I should really have connected a light bulb or something to the output to prove it works....I will upload another video later showing it powering a load.

Here is a video of the power supply driving a 12V light bulb:

That's it for now - take care always,


Saturday, 8 September 2012

Realising the Power Supply!

After months of inactivity....

I have etched the PCB for the LM317 variable power supply.  I used the design outlined in the previous post.  It seemed to come out well!  Here are some photographs of the process.

The design printed on glossy paper!

The design ready to be transferred onto the PCB - Get the clothes iron out!

The design transferred onto the PCB ready for etching!

The PCB after etching....Not too bad, some tracks were over etched.

Fanfare!!  The populated PCB, quickly switched on to see it working....I couldn't resist.

So where did things go well?  - The design was well transferred and populating the PCB was very simple.  The spacing between components was excellent!  A professional design would be much harder to put together because space is always at a premium.  Nothing smoked when I switched it on...that's always reassuring!

Where did things go wrong?  - I over etched the PCB (left it too long in the etching fluid)....It meant some of the tracks were broken.  I managed to fix this quickly with some thick braided copper wire.  I also used the wrong package size for the transformer.  I didn't check which transformer I had before I printed the PCB design.  It's a common mistake and one I won't make again for a while. I will probably change the transformer to the correct one as I may well make some of the power supplies as kits for people at my local hack space!    

The underside of the populated PCB - notice the extra holes and repairs!

The next thing to do is put the power supply in an enclosure and get some panel meters.  We need to be able to see what the output voltage and current is and we need to ensure that this is safe to use.  We don't want any accidents or mistakes when there are mains voltages and high current available!  

That's all for now people!  Take care and have fun always....