2

I have a project using an ESP8266 to control a lamp socket. In very simple terms the sketch switches the lamp socket on or off based on some conditions. The sketch works fine.

What I would like to be able to do is put into the lamp socket either a 3 V 120 mA incandescent bulb or a 3 V 1 mA LED bulb.

The ESP8266 can easily drive the LED directly but of course can't drive the incandescent bulb, so I added a 2N4401 transistor into the circuit with the ESP8266 controlling the base of the transistor.

The problem is that the base resistors required vary dramatically. My calculations indicate the incandescent requires 383 Ω while the LED requires 4.6K Ω. In practical testing, the incandescent requires 1 kΩ to completely shut it off (much less than that and the incandescent will slightly glow when the pin connected to the base is low--i.e., off) yet fully saturate the transistor--i.e., fully on.

Strangely, the LED will not shut off fully even with a 20 kΩ resistor while at this level of resistance on the base the incandescent barely lights.

Using a standard BJT NPN transistor is not going to do the trick. I was thinking maybe a Darlington transistor or MOSFET might be a better solution, but I have never worked with either (although I have done a bit of reading on both.)

enter image description here

  • 2
    Can you add a schematic of you setup? – HandyHowie Feb 21 '22 at 14:45
  • I don't think you're going to be able to do this with a dumb circuit that does not know what type of bulb is connected, Base resistor value should be non-critical unless your schematic, which you have not provided, is unusual. You *are* using the BJT as a switch, right? – DKNguyen Feb 21 '22 at 14:45
  • 1
    Overly complex but possible would be to feed it 1 mA and check what voltage you get. Too low = switch to incandescent mode and bypass the current limiter. High enough = LED installed and everything is good. – winny Feb 21 '22 at 14:45
  • 4
    What I'm getting from this is that you're trying to limit current by playing with the transistor's base current. That's not how it's done, transistor Hfe differs greatly between parts, temperature, etc... – Unimportant Feb 21 '22 at 14:49
  • 1
    If both lamps use 3 volts then apply 3 volts to the socket. – Andy aka Feb 21 '22 at 14:53
  • You are not using the BJT as a switch. You are using it as an amplifier attempting to throttle the current. This will fry it. – DKNguyen Feb 21 '22 at 14:54
  • You are already including an entire microcontroller, why not use it? Sense the resistance of the load and then switch out between the incandescent and LED drivers. – user1850479 Feb 21 '22 at 14:55
  • Just to clarify. I am not trying to use the transistor as an amplifier...in fact quite the opposite. I want to use it as a switch. The use case for this project requires that a user can substitute the 3V incandescent bulb with a 3V LED bulb into the lamp socket. This may not be possible but that's why I am asking. – Dean Arnold Feb 21 '22 at 14:57
  • 3
    Currently you are trying to feed the base of the BJT with *just* enough current to switch the thing you want to switch. You are free to supply the BJT with more base current than it strictly needs to let the collector-emitter current flow more freely. This avoids unnecessarily limitations. Just let the BJT switch fully and limit the current with an additional series resistor in case of the led. PS: they sell leds with built-in resistors to make it easier for the user that needs to swap these things out! – Thijs Feb 21 '22 at 14:58
  • 2
    @DeanArnold You think you're using it as a switch, but you're not. See https://electronics.stackexchange.com/questions/402210/calculating-collector-current-of-npn-bjt-with-led-resistor That series resistor is what limits the lamp current so then the BJT base can be driven so it can be completely conducting without throttle. Then it's a switch. – DKNguyen Feb 21 '22 at 14:59
  • OT but your ESP32 is missing decoupling capacitor(s). – winny Feb 21 '22 at 15:01
  • user1850479: thanks for the comment. I thought about this but I don't have enough available pins on the ESP8266. – Dean Arnold Feb 21 '22 at 15:02
  • Thijs: you hit the nail on the head...this is exactly the type of LEDs I want the user to be able to install in the lamp sockets (as a substitute for the incandescent bulbs). However, the draw a very different level of current then the incandescent bulbs. So, when you install them in the lamp sockets with base resistor at 1KΩ they glow even when the pin is driven low on the ESP8266--i.e., they do not fully switch off. – Dean Arnold Feb 21 '22 at 15:06
  • Are you getting solid 0v and 3.3v on your GPIO outputs? You haven't got anything crazy set up like open collector outputs have you? – HandyHowie Feb 21 '22 at 15:07
  • The base resistor should be chosen so that it can properly turn on the transistor (I would also recommend using a MOSFET and not a BJT then you don't need a series resistor at all). If the transistor can then turn on the bulb, it will equally turn on the LED. Ditto for turning off. – Andy aka Feb 21 '22 at 15:07
  • HandyHowie: yes 0v and 3.3v on GPIOs (e.g., pinMode(LED1, OUTPUT); digitalWrite(LED1,1); etc.. The sketch works fine if I just use the bulb LEDs directly to the GPIOs, or if I use the incandescent bulbs with a transistor thru the GPIOs. The question I have is how can I use both...or is it possible to use either in the lamp socket. – Dean Arnold Feb 21 '22 at 15:13
  • Andy aka: the 2N4401 with a 1kΩ resistor connected to its base which is being controlled by a GPIO works fine with the 3V 120mA bulb. However, when you then place a 3V 1mA LED bulb into the same E10 socket...it glows even when the GPIO pin is driven low. When it is driven high, the LED glows brighter. – Dean Arnold Feb 21 '22 at 15:17
  • @DeanArnold You are going to need two transistors that each use the correct circuit for each lamp, and a way for the MCU to know which to use. You need to abandon this variable base resistor to modify the collector-emitter current approach that uses the BJT as an amplifier. Any method requires more pins. Probably not possible to do with your constraints. – DKNguyen Feb 21 '22 at 15:23
  • DKNguyen: thanks, that was what I was afraid might be the case and why I came to the forum to see if I was missing something. I recognize that the transistor is acting more like an amplifier then a switch (when I switch between bulbs) so was hoping maybe a Darlington or MOSFET might work. – Dean Arnold Feb 21 '22 at 15:23
  • @DeanArnold Darlingtons just let you have less base current for more collector current while MOSFETs just let voltage do the control rather than current. It is still a wash since you're stuck needing a way to swap two base resistors. – DKNguyen Feb 21 '22 at 15:25
  • DKNguyen: Yes agreed. In doing some calculations with the high hFe of the Darlington's in theory it looks like it would switch on and off both. However, I might still get a small amount of current flow when the LED is installed. I may order a couple and see what happens in practice. – Dean Arnold Feb 21 '22 at 15:29
  • `However, when you then place a 3V 1mA LED bulb into the same E10 socket...it glows even when the GPIO pin is driven low. ` @DeanArnold - this sounds problematic. I wouldn't expect any BJT to produce more than 1 uA collector current when the base is being driven to 0 volts. It looks like the 2N4401 is the bad choice here. Or maybe you have it wired incorrectly. – Andy aka Feb 21 '22 at 15:33
  • If the "3 V" LED bulb is actually supposed to be driven with 3 V CV and does internal current limiting, you don't need to do anything. Just use a MOSFET as a low side switch and feed 3 V regardless of what's installed. If it's a bare LED marketed by the Chinese as "3 V LED", where do you need to apply current limiting, you need to do something more elaborate. Please clarify what you have. – winny Feb 21 '22 at 16:12
  • winny: thanks for the email. I saw your previous comment about including decoupling capacitors...thanks for that...I had completely forgot to include them on the diagram. Regarding the LEDs, here is link to the ones I would like to provide as an alternative: [link]https://www.amazon.com/dp/B01CITEJWA?psc=1&ref=ppx_yo2_dt_b_product_details[/link] – Dean Arnold Feb 21 '22 at 17:19
  • @DeanArnold I agree with Andyaka that in a normal situation a led should not be glowing due to the leaking of a transistor switch. In 2N4401 datasheets I'm reading something like 100nA of leakage when the BJT is forced off. So 0V between base and emitter should really result in no light (bulb or led). When you set the ESP pin to 'off', can you check the B-E voltage with a multimeter? Perhaps a miswiring, bad parts or something similar? If you share a picture we can check your wiring. – Thijs Feb 22 '22 at 09:53

3 Answers3

0

TL;DR

BJT Rule #1: Use 5 to 10% of max hFE for a BJT switch. i.e. Ic/Ib = 20 to 10 : 1

Incandescent Rule #1: The lamp cold resistance is 5 to 10% of hot.

Thus 3 V / 120 mA = 25 ohms hot or 1.25 to 2.5 ohms cold. Thus R base needs to be 12.5 to 50 Ohms range.

Rule of CMOS Driver Impedance #1: Ro= Vol/Iol is in the above range so choose a compromise that matches that value of 12 Ohms and pull down with 1K to 0V to turn off an LED.

schematic

simulate this circuit – Schematic created using CircuitLab

Consider the 2N4401 like a 3 ohm switch and the PN2222A like a 1 ohm switch. Which one is better??

2N4401 Collector−Emitter Saturation Voltage (IC = 150 mAdc, IB = 15 mAdc) (IC = 500 mAdc, IB = 50 mAdc)
VCE(sat) = 0.4V max / 150 mA = Ro= 2.6 ohms with Ic/Ib=10

You can do the math for the '2222A which is better.

Tony Stewart EE75
  • 1
  • 3
  • 54
  • 182
0

It is difficult to understand how you managed to calculate those values of base resistor, particularly 4.6 ohms which is way out.

The reason is that a transistor is a current amplifier; you put a current in the base, and get a larger current out of the collector. The ratio of these currents is the gain, but that is difficult to predict since it is dependant on various factors, including the temperature of the device.

Furthermore, the transistor clamps the maximum voltage between base and emitter at around 0.6V; force more in, and it will be destroyed. So putting 3.3V in, via a 4.6 ohm resistor, is asking for trouble; if you're lucky, the ESP8266 (like most logic ICs) will have a current-limit on its outputs, so the transistor and the ESP might have survived - but don't count on it.

So how to fix your circuit? Firstly, when you say 'LED bulb', I'm assuming this is a device with a built-in current-limiting resistor (or a similar electronic circuit). If this isn't true, you need to add an external resistor, for similar reasons to the transistor base; a LED is a constant-voltage device (around 2.1V for a red LED) and will be destroyed if you force a higher voltage in, so must be fed via a current-limiting device, normally a resistor.

You say the LED current is supposed to be 1 mA at 3V, but this does seem quite low for a bulb indicator (as opposed to a LED component); I would have expected 5 mA or more, unless this is a small point-source.

To drive the LED or incandescent bulb, you just need to saturate the transistor (turn it hard on) by feeding sufficient current into the base, such that you can guarantee it will be on, despite the variability of its gain. The main limit to this current is the ESP8266 I/O pins; you should really look this up, but I'd guess that a base resistor around 1K would do.

With regard to the strange results you are seeing when changing the base resistor, I do wonder whether you are actually dealing with a melted lump of silicon, rather than a real transistor, due to your over-enthusiasm in driving the base.

jayben
  • 1,247
  • 2
  • 5
  • My mistake...I meant to type 4.6KΩ resistor...I have corrected it. – Dean Arnold Feb 21 '22 at 17:25
  • The LEDs bulbs I plan to use have a built-in current limiting resistor [link]https://www.amazon.com/dp/B01CITEJWA?psc=1&ref=ppx_yo2_dt_b_product_details. As mentioned in the comments, the 1K resistor works for the incandescent bulbs but does not fully turn-off the LED bulbs. Thank you for your concern but no I am not dealing with melted lumps of silicon. – Dean Arnold Feb 21 '22 at 17:35
0

Assuming your LED bulb is actually a bare LED which needs current limiting, here is one proposal for you.

This turned out really ugly, but here is a starting idea you can think about. If you don't have any more MCU pins, you can do a fully analog circuit which bypasses the current limiting resistor in case a bulb is installed instead of a LED by sensing the current, and triggering a latch circuit which in turn turns on a PFET in order to bypass the current limiting resistor.

PLENTY OF SIMPLIFICATIONS AND OPTIMIZATIONS IS POSSIBLE! V2 is your MCU output.

First off, LED installed. Current is too low to trigger. LED

A bulb installed instead. High current is sensed and latches on M2. Bulb

winny
  • 13,064
  • 6
  • 46
  • 63
  • Thanks very much for taking the time to come up with a possible solution! Very interesting idea. LED Bulbs are not bare, rather they have a built-in current limiting resistor. – Dean Arnold Feb 21 '22 at 17:39
  • Gosh darn it! Then all you need is a MOSFET. All that simulation in vain. – winny Feb 21 '22 at 17:44
  • I thought it might be worth a try. I just haven't really used/spec'd them before. I was looking at ordering some TN2106 Logic Level MOSFETs. Any other suggestions? – Dean Arnold Feb 21 '22 at 18:15
  • Should be as good as the next MOSFET… – winny Feb 21 '22 at 18:22