2N2222 NPN Transistor as a switch - Arduino example with LED Flash

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 พ.ค. 2024
  • How to use the 2N2222 NPN transistor as a switch. The video will go over the use of the 2N2222 transistor to switch a bright LED Flash on and off. We will first switch the 2N2222 using a push button, and then will transition to test with with an Arduino.
    *Note that the LED flash used in this example is a ready-to-use component with a built-in resistor, which is why a separate resistor is not needed.
    Related Blog Post: www.diyengineers.com/2020/12/17/2n2222-transistor-npn
    Items used in this video (affiliate links):
    *As an Amazon & Ebay Associate I earn from qualifying purchases.
    Arduino UNO:
    amzn.to/3U7xAoT
    ebay.us/HNGxxb
    Hilitchi Transistors Assortment Kit:
    amzn.to/4aI8Mux
    ebay.us/aGLa7N
    Electronic Component Kit:
    amzn.to/3xDtHAn
    ebay.us/vaJBeI
    Amprobe AM-510 Multimeter:
    amzn.to/44gy2WE
    ebay.us/JIqbH3
    Amprobe TL35B Test Leads with Alligator Clips:
    amzn.to/3Utf8s0
    ebay.us/zUua23
    Breadboard (Elenco 9440):
    amzn.to/4b5Onzq
    ebay.us/HJB80g
    Chapters:
    0:00 Intro
    0:29 Example Info
    1:58 Circuit and Breadboard (without Arduino)
    4:35 Math/Analysis
    6:54 Test 1 (without Arduino)
    7:15 Voltage & Current measurements
    10:11 Circuit & Breadboard (with Arduino)
    12:57 Arduino code
    13:48 Test 1 (with Arduino)

ความคิดเห็น • 64

  • @gradygraham1281
    @gradygraham1281 2 ปีที่แล้ว +16

    First of all let me say. Its so refreshing to see the younger generation taking and active interest not only in understanding electronics and circuitry along with the laws that govern its principles, but also taking on a role as instructors and teachers of such. You are the future designers who will change our world in so many innovative and useful ways. As an electrician of 34 years I applaud you. Constructive criticism. Its important to have your math spot on. The numbers wont lie. What might lie is the testing equipment's calibration or the variables of the parts you are using. Things as simple as the resistance of wire size and length along with ambient temperature could cause some of the math not being precise when it comes to the values your digital volt meter was registering as well as the resistor value the resistor claims to be vs the actual resistance when measured.. I would like to see you get to the bottom of this and fully explaining why the measured values did not match the math values. OHM'S law has been around for a long long time and its proven over time and experiments. I would like to see you give explanations based on reality when the testing equipment does not match the math. If you know your testing equipment is sound then perhaps you could give us the actual VD across the LED or the actual values of the transistor. Something had to be the cause for the math not being spot on when tested. And this is where you separate the blind from the leaders of the blind. Personally I have thumbed up and subscribed to your channel. I am so happy to see someone not only give resistor values and the math but then show the circuit being tested to make sure the math was correct as it should be. I will be following you and hope to watch you learn and grow and accomplish great things. You are well on your way. Thank you for an excellent video.

    • @gradygraham1281
      @gradygraham1281 2 ปีที่แล้ว +1

      Example th-cam.com/video/8DMZSxS-xVc/w-d-xo.html

    • @diyengineers
      @diyengineers  2 ปีที่แล้ว +2

      Thanks for watching and for your encouragement!

    • @RexxSchneider
      @RexxSchneider 2 ปีที่แล้ว +2

      The main reason why the maths is not spot-on is because the LED has built-in current limiting, which may be either a resistor or a constant current device. The datasheet for the LED is showing 15mA, but that will be a typical current under a specific set of operating circumstances and in this circuit, nobody should be surprised if it was measured as 17.7mA. Measuring the voltage across the LED will tell you nothing other than the Vce(sat) of the transistor at that current.
      The small error in the measured base current (0.91mA vs 0.94mA) is well within the range of accuracy of a 5% resistor, and it should be noted that the 0.6V for Vbe used in the calculation might be anywhere between 0.6V and 1.2V, except that those values are quoted for Ib=15mA and we only have about 1mA, so we don't have specifications for what Vbe actually will be. Using 0.6V as a "guestimate" is quite reasonable, but then you have to expect some variations from what you measure in a real circuit.

    • @dimyttt1780
      @dimyttt1780 ปีที่แล้ว

      @@RexxSchneider Thanks, that's pretty accurate.

  • @citizengkar7824
    @citizengkar7824 ปีที่แล้ว +2

    Nice presentation-concise & to the point: excellent style, of incorporating the various aspects of design & concept, plus initial & final testing. This is from an electronics noob.

  • @Anton-wc7lb
    @Anton-wc7lb ปีที่แล้ว

    Woho! It's working, thanks! Good idea with the trouble shooting, I did that and found voltage across my LED, that way I realised that my LED was broken.:)

  • @qzorn4440
    @qzorn4440 3 ปีที่แล้ว

    great video... this will work nicely as a pulse amplifier from 3vdc to 15vdc... thanks a lot....:)

    • @diyengineers
      @diyengineers  3 ปีที่แล้ว

      Thank you for watching and taking the time to comment! The potential applications for transistors are endless. Glad you enjoyed watching!😃👍

  • @shakirabdo638
    @shakirabdo638 ปีที่แล้ว +1

    Awesome video ❤, I understand every word you said

  • @luise8781
    @luise8781 2 ปีที่แล้ว

    Thank you, very helpful

  • @Rickety3263
    @Rickety3263 4 หลายเดือนก่อน

    I think this will be my reference for my arduino remote control for 2 nerf machine gun switches

  • @markday3145
    @markday3145 3 ปีที่แล้ว +4

    With typical indicator LEDs, you have to limit current flowing through them, or you will burn them out. Is this LED different in that regard? Will it limit its current draw (thus reducing the collector current)?

    • @diyengineers
      @diyengineers  3 ปีที่แล้ว +4

      Yes. This LED flash is ready to use from the supplier/seller and does not require adding a resistor for current control. If you were using a typical LED, you would need a resistor.

  • @alistairmurray626
    @alistairmurray626 2 ปีที่แล้ว

    Hi, i'm just about to test this circuit, but i'm worried about the 12v going to the Vin or the arduino, should that be there? other examples don't seem to be as detailed as yours. Won't 12v fry the arduino??

    • @diyengineers
      @diyengineers  2 ปีที่แล้ว +1

      Hi, you can power the Arduino UNO multiple ways. The Vin pin is one of them. See the links below:
      www.arduino.cc/en/reference/board
      store-usa.arduino.cc/products/arduino-uno-rev3/?selectedStore=us

    • @alistairmurray626
      @alistairmurray626 2 ปีที่แล้ว

      @@diyengineers Ahh! thanks!

  • @pcbland7850
    @pcbland7850 2 ปีที่แล้ว +1

    Amazing my friend ♥♥♥♥♥
    Nice to meet you ♥♥♥♥♥
    Best wishes for you ♥♥♥♥♥
    Keep it up ............. ♥♥♥♥♥

  • @apratube
    @apratube 3 ปีที่แล้ว +1

    your switch uses pull-down resistor, right? meaning, the pin will see 0 if button not pressed. your code says, if button == 0 (not pressed), then turn the LED on (?).. but your video shows the other way around.. is my understanding correct?

    • @diyengineers
      @diyengineers  3 ปีที่แล้ว +2

      Thank you for your question. I reviewed the circuit and code, and I agree you are correct. When you press the button, a 5V signal will be sent to Digital Input #2, and when you release the button (not pressed) a 0V signal will be sent to Digital Input #2. That means that Button_State = 0 when the button is released (not pushed). The code shows that when Button_State = 0 the LED will turn on, but then the video shows the opposite. I must have changed this last minute and forgot to fix the code. To address this, change the If-statement as follows:
      if (Button_State == 1){
      digitalWrite(LED, HIGH);
      }
      else{
      digitalWrite(LED, LOW);
      }

  • @kentharris7427
    @kentharris7427 2 ปีที่แล้ว

    I am using a Transistor to increase the current at 5 volts. I am making a parking proximity sensor using a HC-SR04 Ultrasonic Module and a 650 NM laser. The HC-SR04 detects the car rolling into the garage and the 650NM Laser is turned on for 30 seconds. The laser draws 30mA at 5 volts. I am going to use a 2.2K resister on the Transistor base.

    • @RexxSchneider
      @RexxSchneider 2 ปีที่แล้ว

      When you drive a transistor into saturation, the rule of thumb is to set the base current at 1/10 of the collector current. have a look at the datasheet for the 2N2222 and you'll see that all of the examples of saturation show base currents one-tenth of the collected current quoted. If the collector current needs to be 30mA, then normal design is to drive the base with 3mA. The value is not critical and if you have 5V available to drive the base, then a 1.5K resistor will give you about 3mA. Your 2.2K resistor will give about 2mA base current and that will also work fine to drive a 2N2222 into saturation.

  • @danielmalatji1813
    @danielmalatji1813 18 วันที่ผ่านมา

    How bout we now using Arduino with the npn and relay to control a 220Vac fan☺️☺️☺️

  • @OrbMan97
    @OrbMan97 2 ปีที่แล้ว

    How can I control a motor using 2 sensors? It works if I wire both sensors in parallel to the same pwm pin. But I want to be able to control the motor from 2 different arduino pins. Is this possible? Would i need to use 2 different bjts?

    • @diyengineers
      @diyengineers  2 ปีที่แล้ว

      Hi. Thanks for watching! I think you should take the sensor signals on separate I/O pins in the Arduino, and use the code (if statements, etc) to determine when to send a given signal to the motor to move.

  • @muhammadabdulla23
    @muhammadabdulla23 ปีที่แล้ว +1

    I am an electronics student, and I am trying to understand how a transistor is used like a switch. Why do we need the transistor to switch on and off the LED while we can just use that button when the LED is connected to a battery?

    • @diyengineers
      @diyengineers  ปีที่แล้ว +1

      Thanks for watching. I am using the signal from the Arduino to turn the light on and off. The issue is that due to the electrical rating of the LED Flash, I am unable to power directly from the Arduino, so I use the Arduino signal to tell the transistor when to allow current to flow through it (basically having the transistor to operate as a switch). This transistor is rated to handle the electrical load from the LED, which is why I am using it.

  • @longdongsilver4719
    @longdongsilver4719 3 ปีที่แล้ว +9

    You should mention that a normal LED would be destroyed if you connect it directly to 12V via a transistor. Most LEDs don't have built-in limiting resistors. So you just forgot the limiting resistor on your schematics.

    • @diyengineers
      @diyengineers  3 ปีที่แล้ว +2

      Thanks for watching and for calling this out!

  • @bricktimelapsebuilds
    @bricktimelapsebuilds ปีที่แล้ว

    one, perhaps very stupid question but why do i have to put an high ohm resistor on the base?

    • @diyengineers
      @diyengineers  ปีที่แล้ว

      Hi. Thanks for watching! A resistor at the base of the transistor enables us to limit the current that flows into the transistor's base, preventing it from getting damaged due to excess current. Not that you also need to allow enough current to flow to fully saturate the transistor and use it as a switch (allowing current flow between collector and emitter whenever we send the signal to the base).

  • @michaelborisow815
    @michaelborisow815 2 ปีที่แล้ว

    What's the point of the base? I was able to do this without any connections there, nor any transistor at all, of course

    • @diyengineers
      @diyengineers  2 ปีที่แล้ว

      So did you just power it directly to the voltage source? The intent is to use the transistor as a switch, which then let's you control the flash with an Arduino (or similar) which operates at a voltage that's below what's needed by the LED.

  • @wadebrewer7212
    @wadebrewer7212 หลายเดือนก่อน

    Why 4.7kohm resistor. I am firing 2N2222 with arduino out and a 1kohm resistor.

  • @easyrobotic9754
    @easyrobotic9754 3 ปีที่แล้ว

    Coooollll

  • @j5892000
    @j5892000 2 ปีที่แล้ว +1

    I'm confused. Why even use a transistor when you can hook the led up straight in series with the switch?

    • @g.stefanstoica
      @g.stefanstoica 2 ปีที่แล้ว +2

      It was used as an example. In this case you could just bypass everything. This video shows how to control a circuit with a higher voltage than your 5V controlling circuit.

    • @diyengineers
      @diyengineers  2 ปีที่แล้ว +3

      Hi NoVakane. Thanks for watching. The transistor let's you switch another power source on/off by using the low power I/O pins. You wouldn't be able to power directly a device that operates at a current or voltage that's above the allowable limits of the Arduino, so the transistor let's you go beyond those limits (as long as the voltage and current don't exceed the transistor limits).
      Thanks for watching!

    • @deang5622
      @deang5622 ปีที่แล้ว

      Because in years gone by LEDs would typically require 15 or 20mA of current to illuminate them to a reasonable level of brightness, and the logic outputs of chips in those days could not source enough current. Their output drive current was typically a few milliamps and so a transistor had to be used.
      Now in more modern times the drive current requirement for an LED can be a lot lower.
      So whilst a transistor may not be strictly necessary in this instance, it is important to be able to design with a transistor on a logic pin output, to teach the design process.

    • @j5892000
      @j5892000 ปีที่แล้ว

      @@deang5622 ooh I see

  • @bassamskafi1384
    @bassamskafi1384 3 ปีที่แล้ว

    I can fixed resistance 8k or 10k on the base

    • @diyengineers
      @diyengineers  3 ปีที่แล้ว

      Sorry, not sure if I understood. Let me know if you have any questions! 🙂

  • @tungvan4029
    @tungvan4029 3 ปีที่แล้ว

    Can u explain? Why chose res 4.7k without caculate.

    • @diyengineers
      @diyengineers  3 ปีที่แล้ว +2

      Hi! Thank you for your question. On the video at 5:25 I go over the minimum voltage between the base and the emitter, and we write the equation to relate the base current to the voltage drops and resistance (in this case selected to be 4.7k). Selection of 4.7k allowed the base current (ib) to be 0.94mA, which is below the maximum allowable at the Arduino IO Pins. As a result, 4.7k is an acceptable resistance. You could use a different resistance, but you need to ensure all equations work. For example, if you picked a larger resistance, the value of ib would drop, which is okay when comparing against the maximum allowable current, but this could cause issues on the equation I show later (ic = hfe_min * ib), because if Ib is too low, then Ic might be low too, resulting in the LED not turning on.
      Hope this helps!

    • @tungvan4029
      @tungvan4029 3 ปีที่แล้ว +1

      Thanks. At 10:01 Ic =18.54mA and Ib=0.9mA so hfe=19, but in datasheet hfe_min=35, why? AND 0:43, the current operating LED is 15mA, 18.54>15, i meant what is the current rate of LED?

    • @diyengineers
      @diyengineers  3 ปีที่แล้ว +3

      The current gain (Ic/Ib) can vary due to manufacturing tolerances of the transistors, temperature, current, etc. What you want to do is design your circuit such that it can operate at the minimum specified current gain, in this case at 35. You can see this at 6:19 where I calculate Ic using the minimum specified current gain, I get 32.8mA, which is above what the LED should be pulling, so we know the transistor will be able to provide the required current (which is expected to be less than the 32.8mA that were calculated). We are not pushing the current, the LED is pulling the current based on what it needs (based on its specifications), so we are simply doing the calculation to ensure the transistor can supply what the LED demands.
      When I made this video, the LED specification indicated an operating current of 15mA. Normally we would use this as guidance for the design of the circuit. I went back to the website (link below) and saw that they actually updated the specification to show a current of 20mA. So our measurement is now below the rating they have on the website. Slight variations versus listed values are expected.
      www.superbrightleds.com/moreinfo/led-wired-bolts/little-dot-smd-led-accent-light/639/#tab/specifications

    • @tungvan4029
      @tungvan4029 3 ปีที่แล้ว +1

      @@diyengineers yup. thanks a lot!

    • @diyengineers
      @diyengineers  3 ปีที่แล้ว +1

      Glad I could help! 😀👍

  • @robinsonsoto8471
    @robinsonsoto8471 3 ปีที่แล้ว

    You are applying 12 v to a led without current limiting resistor. This does not make sense.

    • @diyengineers
      @diyengineers  3 ปีที่แล้ว +1

      Hi Robinson. It is an LED flash that was designed to light up when 9V-14.8V are applied. It's a ready to use component (not a standard LED). That's why a resistor is not needed (it's already built-in).
      See this link for more details: www.superbrightleds.com/moreinfo/led-wired-bolts/little-dot-smd-led-accent-light/639/

  • @harpuli
    @harpuli 2 ปีที่แล้ว +1

    that led is dead without a resistor at 12v

    • @diyengineers
      @diyengineers  2 ปีที่แล้ว

      I think I made mention of it being a special LED with a built-in resistor. But, yes, if it was a standard LED, it would be an issue. Thanks for watching!

    • @RexxSchneider
      @RexxSchneider 2 ปีที่แล้ว

      Because LEDs are now so commonly used in automotive applications, it makes sense for a manufacturer to build the part with a built-in resistor (or some other means of current limiting) so that the voltages encountered in use with a car having a 12V battery can be connected directly to the part. You can see that the datasheet specifies a voltage between 9V and 14.8V, and car alternators are specified to output a maximum of 14.8V. When you see a part specified with a maximum voltage of 14.8V, it is a sure indicator that it's designed for automotive use and will tolerate 12V directly.

  • @user-pm8je4fo7e
    @user-pm8je4fo7e ปีที่แล้ว

    The fact that this particular part has "built-in resistance" does not matter since your circuit diagram clearly states "regular LED". Also you code doesnt add up with actual results obtained, which kinda undermines the whole experiment. And the worst thing is that you math isnt actually explaining anything. It doesnt explain how to find values we are looking for, how to find proper resistance and transistor, it just shows how your already-in-place results are correct (and they are not, since your shematics doesnt reflect actual parts being used). This material is useless AND confusing for anyone who knows less than you do (or claim to).

    • @diyengineers
      @diyengineers  ปีที่แล้ว

      Thanks for watching!

    • @user-pm8je4fo7e
      @user-pm8je4fo7e ปีที่แล้ว

      @@diyengineers yeah, you tricked me into that. Hope you proud.