Transistor switch circuit for Raspberry Pi, Arduino and micro:bit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello many of my recent videos have been aimed at  the intermediate to advanced maker so this video   is going to be aimed a bit more at beginners  i'll be covering transistor switches but i'll   also still be covering a little bit more advanced  towards the end including darlington transistor   arrays that you can use for driving multiple  leds so if you want to skip straight to that   look at the chapters on the timeline  or in the description of this video   i'll be creating some more fun projects in the  next few weeks so please check back for those if   you're interested in those this video is going  to look at transistors of switches and how they   can be used in practical circuits including  using the raspberry pi arduino and the micro   and this will cover bipolar transistors i won't be  covering the specifics of electrons and doping of   the semiconductors and the granular detail this is  more of a practical guide i will cover some simple   calculations used to work out the appropriate  resistors for the circuits first i'll give a short   explanation of what the transistor is  and why they're so useful in electronics   the transistor is a semiconductor device this  means that under certain conditions it can allow   current to flow acting like a conductor otherwise  it will resist the flow of current the transistor   revolutionized the electronics industry and much  of what we now take for granted was made possible   through the transistor and development since for  most users transistors can be roughly categorized   into two types bipolar junction transistors which  work as current amplifiers and the mosfet which   works like a voltage amplifier i'll be covering  bipolar junction transistors in this video   but i'll be adding another video on mosfets so if  you're interested in them (and you should be) then   please subscribe to my channel this image shows  some bipolar transistors and the schematic symbol   the bipolar junction transistor was one of the  earlier types of transistor it is still used today   although it has been significantly overtaken  in use by mosfets which will be covered in a   future video the bipolar transistor is still  used today particularly for analog circuits   but also for some digital circuits such as the led  driver circuits it's also still used in education   to explain electronic concepts most transistors  have three pins a selection of different sizes   are shown in this image the transistors shown  here all used through hull technology which can   easily be soldered by hand the smallest transistor  shown on the left is in a plastic to-92 packaging   the next one in a small metal can is a  medium power transistor in a to-39 packages   next there is a transistor in a to-126 packages  which is a high power transistor and can be   attached to a heat sink and then finally  there's a transistor in the to-204 packaging   this last one has only two pins and  the case acts as the third connection   these are all npn transistors which refers to  the way they are created which influences the   way they work the npn is the most common type  used in switching circuits for the practical   example i'll be using the 2n222a which is the one  on the left a transistor has three connections   the base collector and emitter these are labeled  B C and E respectively on the diagram when a small   current flows between the base and the emitter  known as Ib then a much larger current can flow   between the collector and the emitter known as Ic  the amount that the current increases is known as   the hfe which is the current gain or amplification  factor this is typically between about 30 and 100   the bipolar transistor is a current amplifier  but in this case i'm more interested in using it   for digital signals if you apply an appropriate  current to the base this can be used as though   it was a switch with no current entering at the  base then it will be switched off and not allow   current to flow from the collector but then switch  it on with sufficient current and it will turn on   to act as a switch then the current must be  sufficient to place the transistor into the   saturation region which is effectively fully on  that's the high level theory out of the way i'm   now going to move on to a practical example  of how a transistor can be used to switch a   bright led from a raspberry pi this example could  also be used with an arduino or microbit and i'll   include examples of how they can be used in the  case of this particular led then it wouldn't need   a transistor for an arduino or a micro bit you can  follow these same steps for switching larger loads   in this case i wanted to switch on a 10 millimeter  bright white led this could be used as a mini   light to illuminate an area the led requires about  20 milliamps of current to light up the arduino   and the micro bit can both handle more than the  required 20 milliamps from the output pins so   they could drive this led directly although  you can still use this circuit as an example   the raspberry pi can only output a maximum of 16  milliamps so we need a way of increasing that to   turn the led on brightly the schematic diagram for  this is shown this shows the led and transistor   there are then two resistors RL which is the  load resistor used to protect the transistor   and led from too much current and RB which sets  the input current to the base of the transistor   the ground of the transistor needs to be  connected to the same ground as the raspberry pi   or whatever you're using to drive it the 5  volt supply shown at the top does not need   to be the same as the raspberry pi although  that will work in this example the first step   is to choose the transistor i've already  said that i'll be using the 2n222a   but why this transistor this is just a  really common transistor used for hobby   electronic projects so it's a sort of go-to  transistor i don't want to just use it without   first checking it suitable so it's time for a  quick look at the data sheet for the transistor   a quick look at this data sheet you can see the  maximum collector emitter voltage is 40 volts   which is much more than the 5 volt supply the  maximum collector current is 600 milliamps   which is again far more than we need those are  the main characteristics we need to check to   ensure that this transistor will be suitable  and in this case it's more than capable   we're going to need some more information  from the data sheet which we'll go through now   if you look at the next table  you can get the hfe value   in this case it's around 75 also take a note  of the collector emitter saturation voltage   and the base emitter saturation voltage so  we'll need those later for the calculations we now need to do some simple  calculations to work out the resistors   i'll be showing on my working out  so it should be easy to follow   first let's look at resistor rl this resistor  is to limit the current through the led   the led has very low resistance when switched on  and without an external resistor it would risk the   led burning out we will use ohm's law to calculate  the size of the resistor formula for finding a   resistor is that the resistance in ohms is equal  to the voltage in volts divided by the current   in amps this is the formula r equals v divided  by i for that we need to know the voltage across   the resistor and the current through it we know  that the desired current through the resistor   is 20 milliamps can go up to 30 milliamps but  around 20 milliamps provides adequate light   the voltage across the collector  emitter at saturation is 0.3 volts   and then the voltage across the led from the  led data sheet is 3.3 volts this is quite high   for an led but no this is because we're using a  large white led and it has a high voltage drop   a typical five millimeter red led may only  have two volts volt forward voltage drop   if we plug those values into the  formula the voltage across the resistor   is the supply voltage at five volts minus  3.3 for the voltage across the led and 0.3   across the transistor this is divided by the  current of 20 milliamps which is 0.02 amps   this gives the value of 70 ohms there  isn't a standard resistor of 70 ohms   using the e12 series of resistors we  could use either 68 ohms or 82 ohms   to check the current based  on these particular resistors   you can rearrange the formula to see the actual  current we should use i equals v divided by r   this gives 21 milliamps with the 68 ohm resistor  or 17 milliamps with the 82 ohm resistor   i used an 82 ohm resistor in this  circuit but either could be used   for this i've just been using the e12 series  resistors these have a reasonable gap between   resistor values were available as a full set which  is useful when starting with electronics you could   use more accurate resistors from the other series  such as a 75 ohm on the e24 series or even a 69.8   ohm resistor on the e96 series well that last one  is a bit more specialized so you'd have to audit   specifically for the project so i suggest you  stick with either v12 or e24 series resistors   unless those are specific needs for an exact value  now to calculate rb this is to limit the current   going out of the gpio port and into the transistor  base it needs to allow sufficient current to fully   turn on the transistor into its saturation region  without risk of damage to any of the components   to work out the base current we need to know the  gain hfe which we found on the datasheet as 75   can now work out the current required at the  transistor base by dividing the collector current   by the gain which gives 0.3 milliamps we now  multiply the required base current by 10 to ensure   that the transistor is fully saturated this isn't  fixed rule but it's a good figure to work with   this gives three milliamps note i've still used  the original 20 milliamps for the calculation   i could have used the actual current we wanted  instead but it won't make much difference to   the calculation especially after i've added  the 10 times factor for the base current   which will more than cover any differences now we  know the current for the base we need the voltage   difference between the gpio output voltage and  the voltage across the transistor base and emitter   and then we can work out the calculation for  the resistor using ohm's law the same way as   we did for rl i'm using 3.3 volts here for the  gpio of the raspberry pi that would be the same   if using a micro bit but if you're connected to an  arduino such as the uno then you'd use 5 volts for   the voltage across the base and emitter i'm using  a minimum value from the data sheet which was 0.6   volts this works out at 900 ohms the nearest  resistor values are 820 ohms or one kilo ohms   again either could be used here but i went for  the one kilo ohm resistor so now we have the   required values we can build the circuit here is a  breadboard layout diagram based around a raspberry   pi i've used gpio port 22 which is physical pin 15  on the raspberry pi but you can use any suitable   gpio port i've also shown an external power supply  but for this led you could just take it from the   5 volt connection on the gpio in this example we  only increased the current a little more than the   raspberry pi can cope with so if we can accept  a little less brightness then you could just   use this without the transistor at all however  this same process could switch much larger loads   so for example if you wanted to connect  two leds in parallel then you could do so   i'll quickly demonstrate this on this diagram i've  added a second led in parallel each with their own   resistor you may think you could just use a single  resistor to limit the current through the leds   in theory you could but in practice due to  the individual characteristics of the way   that the led is made even leds from the same  batch then you could end up with a situation   where one led switches on and the other doesn't  at which point you'd have too much current   going through one led and the other being off  there are no changes required to resist the rl   the current through each side still needs to be  about the same and the voltage across the led   and transistor is still the same so we can  use an 82 ohm resistor for each of the leds   the calculation for rb will change in this case we  need to understand kirchhoff's current law i won't   go into detail here we've got already got enough  calculations going on effectively if we just add   the two currents from each of the leds together  that will give us the current going into the   transistor collector so using 20 milliamps for  each then we have an ic value of 40 milliamps   this gives you a value of 540 ohms and the nearest  e12 resistor is 560 ohms now to look at the code   to turn the transistor on and off which will  turn the led on and off the first example is   shown using raspberry pi this uses python and the  gpio0 library which is imported on the first line   the led pin is defined as pin 22 which  is physical pin 15 on the gpio connector   it then creates an led object called led within  the for loop it turns the led on and off with the   delay in between this is only very basic code to  show how to turn the output high and low and to   turn the transistor on and off for the arduino  then the good news is that one of the examples   is already a perfect match for this the blink  example is one of the most basic example codes   provided it uses pin 13 on the arduino which  is an on-board led so you can see the led flash   on the arduino as well as the led switch by the  transistor this is written in c plus plus there   are a variety of different languages that can be  used on the micro:bit i'm going to show the make   code block code and then the javascript code this  is the blocks code which has a digital right to   the pin to turn it on and then a digital write  to turn it off and then there's just a delay   for the javascript i've just switched to the  javascript tab which takes the same block code   and converts it into javascript code as you can  see the javascript uses a very similar wording   to that used in the code blocks so that's the  first example using a standard bipolar junction   transistor this works well when switching loads  up to around 100 milliamps it does not work so   well if you have a very large load to switch for  one led we need approximately three milliamps to   switch on the transistor but if we wanted to  switch more than 120 milliamps or about 6 leds   then the base current is going to exceed the 16  milliamps that the raspberry pi gpio can provide   if we can use one transistor to increase  the gain then what about using two   to amplify the current twice this is quite a  common occurrence and there's a specific device   used for this called a darlington pair again  let's use a practical example the lights shown   are ones that i bought from poundland these are  usb lights with the 10 high power wide leds per   light bulb i've removed the cover from the one on  the right so that you can see the individual leds   there's no technical information provided  with the leds but i measured the current   at around 500 milliamps when connected to a  5 volt power supply this is much more than we   can drive using a single transistor from the  raspberry pi arduino or micro bit this image   shows a darlington pair the first stage used  to provide the input signal to the second stage   although you can make this from individual  transistors it's more commonly provided   as a single device which has three connections  for the base collector and emitter as you can   see in the photo i refer to this as a darlington  transistor as in many ways it looks and behaves   like a normal transistor and it's common to  refer to this as though it was a transistor   the gain is much higher than a single transistor  in this case a gain of 750 which is 10 times   that of one transistor here is an example  schematic diagram showing how this is connected   this is shown as with the raspberry pi but it's  the same circuit for the arduino or micro bit   note that this will need a separate power supply  i would not recommend trying to connect this to   the 5 volt power supply from the raspberry pi  or even the arduino for this amount of current   i've shown only five of the leds in the  diagram to reduce the size of the image   the calculations are the same for a single  transistor but with a much higher gain   volume the diameter pair used here is a  bd681 it has a vbe voltage of 1.5 volts   a vce voltage of 1.5 volts and a hfe or gain of  750. the ic maximum collector current is 4 amps   we don't need to work out the load resistors this  time as they are already included in the led bulb   you may need to check that the load  can be switched on by the transistor   in this case the bulb is designed for a 5  volt power supply but due to the voltage drop   across the transistor it will only be running  at 3.5 volt in this case it works although it's   a little dimmer than if it was connected directly  to a 5 volt supply so we just need to work out the   base resistor RB first we need the current of the  base which is 0.5 amps divided by hfe of 750 given   0.7 milliamps adding the 10 times factor to ensure  saturation gives a base current of 7 milliamps   we can now use ohm's law to work out the value of  the resistor using 3.3 volts for the raspberry pi   gpio and one and a half volts dropped across  the darlington transistor this works out a   little under 260 ohms the 220 ohm resistor  is a good fit here so i'll be using this one   this darlington transistor can be used with the  previous code to turn the light on and off other   suggestions are to have a button which you press  which turns the light on for a set period of time   or this example which shows the light being  controlled using a pir sensor the paper covering   the circuit is to hide a light sensor to make it  appear as though it's dark i'm going to leave that   as an exercise you can try yourself have a go  at adding your own code to turn the light on in   different ways you could create a pir sensor and  i'll include some links in the description for   some useful resources for that or you could use  the touch sensor on the new micro bit version 2   to create a touch activated light if you do create  your own circuits please share it in the comments   finally i'd like to mention one more example  where darlington transistors are used to today   which is in the form of integrated circuits  containing an array of darlington transistors   the particular ic is the uln2803a which i've used  in my previous video on edit on driving a seven   segment display if you look at the package you'll  see that it's several darlington transistors in   a row you just connect these up and use them  in the same way that you would just transistor   i'll include a link to a led seven segment display  project on this video and in the description in this video i've covered npn transistors and  how they can be used as a digital switch to switch   larger loads and a raspberry pi arduino or a  microbit i've covered how to find the appropriate   information on the datasheets and how to work out  the resistors needed for both the load and the   base resistors i've also shown how you can use a  darlington pair or darlington transistor to switch   even bigger loads including usb lights and finally  how these are available as an integrated circuit   which you can use for driving multiple  outputs such as a seven segment led display   the practical examples shown are based on  projects in my book Learn Electronics with   Raspberry Pi the second edition of the book  is now available both in softback or ebook   in the next video i'll look at mosfets which  are a different type of transistor how they   can be used and with practical examples such as  even more powerful disco lights i'll also cover   cmos logic output and using them as logic level  converters if you've not yet subscribed please   click on the subscribe button to find out about  my future videos and i hope to see you again soon
Info
Channel: Penguin Tutor
Views: 8,450
Rating: undefined out of 5
Keywords: Raspberry pi, rpi, arduino, micro:bit, microbit, micro, bit, transistor, switch, npn, bipolar, darlington, pair, LED, large, 2n2222A, bc148, uln2803a, pir, bd681, ic, integrated circuit, to-92, to-39, to-126, mosfet, base, collector, emitter, resistor, ohms, ohm, law, calculate, load, hfe, gain, amplifier
Id: CD9-oPzJL0I
Channel Id: undefined
Length: 21min 44sec (1304 seconds)
Published: Mon Jan 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.