esp8266 + 2n2222 transitor switching 12v LEDs. schematics right?

Long post, I tried to do the research but want to be sure.

So here we go. I have my esp8266 with an 2n2222 transistor and my 12v LEDs.

I would like to use the 2n2222 as a switch. Looking at the datasheet (included in attachment below)of the 2n2222 this 12v is within range, also the 0.4 amps the LEDs are using is in range. [EDIT]: Not 0.4A but 0.04A, see further posts below.[EDIT]

So this is how I hooked everything up.

sketch-1551345633399.png

[Q1] Is this all good?

If it is, all that rest now is to calculate R1

Using this guide here link to guide I think I understand how this goes.

Datasheet 2n2222
sketch-1551348454512.png

My idea of this, looking at the datasheet, saturation at collector current of 150mA / base current at 15mA right?
So, if my collector current will be 400mA (actual mesearments of the LEDs) the base current must be 40mA?
[Q2] Still on the right track?

If so we go on. Looking at the schematics again, at collector current of 500mA the base emitter voltage must be between 0.6 and 2.6. My collector current will be ~400mA. So to be safe let’s say I want a base emitter voltage between 0.6 and 1.2.
[Q3] Still good?

Let’s hope so, I go on with calculating the value of R1. GPIO2 output is 3.3v. I want a voltage drop if at least 2.1v (3.3v minus the maximum base emitter voltage of 1.2v) and a drop of max 2.7v (3.3v minus the minimum base emitter voltage of 0.6v)

So with ohms law we can calculate that. Max output on the esp8266 pin is 12mA.

For 2.1v drop we need 150ohm
For 2.7v drop we need 192ohm
So, R1 needs to be between 150 and 192ohm!
[Q4] Nothing blown up yet?

I hope you guys can help me out here. I broke it all up in the steps I took, trying to explain how I did everything. Hoping that it will be easier to find errors.

1 Like

I'm with you until (including) Q2. Q2a: will the GPIO support 40mA? Ah, you listed max 12mA, so it won't work :frowning:

For the resistor determination take the worst case values, i.e. Ube=2.6V. This leaves 3.3-2.6=0.7V for R1, with 0.7/40[k]=700/40~18 Ohm. But then you risk to blow the BE junction if Ube=0.6V, resulting in (3.3-0.6)/18=150mA. Unfortunately this current will burn the ESP before the transistor.

What you can do in practice: select a specific transistor and measure the really required Ib for Ic=400mA. Make sure that it is within the GPIO drive capabilities. Eventually check further transistors for the least required Ib.

Also measure Ube at that current. Then calculate R1 for safe switching (Ib+x) and verify proper performance in ON state.

After all that you'll be happier and on the safe side with a logic level FET instead of the bipolar transistor. Then R1 only serves the purpose to limit the GPIO current while switching, without further affecting the output current or voltage.

Shoot, good thing i asked here. Not sure how I missed the 12mA max output and the needed 40mA

I do have a couple of transistors laying around. Bought an assortment so will have to go true 7 more datasheet then.

I only have a simple voltmeter hand so I can not do the measuring in practice unfortunately.

I might have a simple relay laying around, can try if that will work with the 3.3v.

Since on the road in a foreign country it's not optional to stop at an electronics shop and ask for what I need. No clue how to find an actual shop with electronics in stock.

Thanks for the reply, appreciate your help

Well, not believing that 3 simple LEDs would pull 400mA I took the volt meter out once again. And there were go, they are not ogling 0.4A but 0.04A

So, this changes a lot, I'll have 40mA collector current.

But to be honest you example of the calculation of R1 for me confused. Sorry I'm all new in this transistors world.

DrDiettrich:
For the resistor determination take the worst case values, i.e. Ube=2.6V. This leaves 3.3-2.6=0.7V for R1, with 0.7/40[k]=700/40~18 Ohm. But then you risk to blow the BE junction if Ube=0.6V, resulting in (3.3-0.6)/18=150mA. Unfortunately this current will burn the ESP before the transistor.

So I am going to try. Still worst case values, that is voltage on base emitter 2.6v. Thus still 0.7v for R1.

Now, 0.7/40[k], just to clarify for myself, R=U/I R=0.7v/0.04A R=17,5 so let's say ~18. That is clear.

But, things have changed, instead of 400mA collector current and 40mA basecurrent we have now 40mA collector current and 4mA base current.

So a redo with these values gives us 0.7/4[k]= 700/4~175 Ohm

If we go for base emitter voltage of 0.6V we get 3.3v-0.6v=2.7v 2.7v/175 Ohm= 0.015A thus 15mA

So, that would be still out op specs of the esp8266? :frowning:

[EDIT]
Still not totally clear what's up with the min en max base emitter saturation voltage....

With ranges of values it can become quite complicated to calculate safe values :frowning:

My experience is about 0.7V for Ube, and never more than 0.8V for low power transistors. I’d chose R1 to not overload the GPIO pin (12mA), with (3.3-0.6)/0.012~220 Ohm. This also should work on worst case conditions.

If you want to extend your experience, measure and draw some characteristic diagrams for several transistors, including current amplification. Get a DMM, it will help you in many situations.

Once again thanks a lot for you help. I do have a simple multimeter helping me out here. A tenma 27 7770.

Allright, so, 220ohm on R1. Not sure what is going on. I have not connected the LEDs and the esp yet. I had the esp on there for a second but unplugged it quickly.

The blue led stays on. So I took my meter and read 3.1v between pin 2 and 4.
Double checked the wiring, all connected according to the image above.....

Any idea what's wrong?

Esp still working I tested it out and it boots fine and connects to the wifi fine.

Sorry, which signal names do you mean?

The output voltage may decrease when loaded with the maximum current, due to the internal resistance of the GPIO.

@OP

Q2. As per data sheet, the saturation point parameters of 2N2222 transistor are: VCE = 1V, IC = 150 mA, hFE = betaDC = 100. These means that you need to inject 150/100 = 15 mA current at the base of the transistor to bring it into saturation zone. If you inject more base current, there will be not increase in the IC (collector current); rather, you will certainly damage the transistor. This is not a good job of killing an innocent object which cannot otherwise defend itself. You want 400 mA current, put 3x2N2222 in parallel or suitable Darlington pair.

Sorry, English is not my native language, I guess we misunderstood each other because I don't know what you want to know about what signal names. I might have formulated my sentences a bit strange?

What I did was solder the 220 ohm resistor in place following the schematics above.

Plug in esp and the red power led goes on but the blue led also stays on. So I removed it.

Removed the 220 ohm resistor p,ug the esp back in. Works good now, connects to wifi, I can reach its server and I can change gpio2 from low to high and vice versa. (Blue LED flashes to indicate the change and I measured it with multimeter, 0v changing into 3v.

Unplugged the esp, soldered the 220 ohm resistor back on and started measuring voltage across pins.

Pin 3 to pin 4 shows 3.1v so gpio2 --> ch_en shows 3.1v. I think this is causing the blue led to stay on? Not sure if this is supposed to happen.

GolamMostafa:
@OP

Q2. As per data sheet, the saturation point parameters of 2N2222 transistor are: VCE = 1V, IC = 150 mA, hFE = betaDC = 100. These means that you need to inject 150/100 = 15 mA current at the base of the transistor to bring it into saturation zone. If you inject more base current, there will be not increase in the IC (collector current); rather, you will certainly damage the transistor. This is not a good job of killing an innocent object which cannot otherwise defend itself. You want 400 mA current, put 3x2N2222 in parallel or suitable Darlington pair.

I want 40mA of current. Figured out later on that the 400mA was a wrong reading on my side. Edited the starting post to make this clear, thanks for answering

@OP

The following circuit will give you max 300 mA current at the collector circuit.
2n2222.png

2n2222.png

Thank you will give that a try.

However I'll only need a out 40 mA to get the LEDs going. Wil this still work?

This still gives me a voltage reading between gpio2 and ch_en of 2.5v keeping the led on the esp on.

Thinking there mus be something wrong with my whole wiring.

Is it needed to have the LEDs connected before testing?

Voltages usually are measured vs. Gnd. I know that GPIO2 is the output pin, but what's ch_en and how is it related to the transistor circuit? The meaning of the LEDs should be described in the board documentation.

The ch_en is enable chip. So if there is no 3.3volt supplied it will not power 9n properly.

Reading voltage between gpio2 and ch_ch that means that the gpio pin acts as a ground pin as soon this whole thing is wired up?

When wired up like this, it reads about 2.5v between those pins.

sketch-1551376131789.png

Confused… :confused: :confused:

This is what i found about the blue led:
The blue LED is connected to the UART TX pin. The red one is power LED

Gnd is - of the battery.

Yes I understand that ground is the battery.

After some more research I found that gpio2 is also connected to the blue led.

However there must be something else wrong since the circuitry shown is ok, the calculations made are ok.

I will take the whole thing apart and try again.

I also have s8050 transistors, wil have a look at their datasheet.

Triple check the pinout of the 2N2222
I used to use them a bit, and found two completely different pinouts ... can’t remember, but search, and verify with your meter. Ideally with the base not connected to the ESP - they’re fairly fragile, and it’s easy to jumper up to 3v3 or ground while testing.

Thanks for thinking along. I checked the pinout a million times. Tried 3 diffrent transistors. All the same marking, 2n2222 a331. That is all info I have, they came in a "all kind of transistors" package

Testing with dmm, from base to emmiter and from base to collector thru both give the same reading. So this means they are ok?

Testing is done without the esp, I made it modulair so it's easier to swap the esp or ro program them.

Maybe ill turn a transistor 180 degrees and see what is does?