Turn Off 5V Sensors by GPIO - will it work with this Transistor / Resistor?

I just started with ESP Devices recently, and now it looks like i have to do my first time soldering :smiley: I would need just need to turn on/off a +5V Device by a GPIO.
Though i’m a bit frightened, it should be doable, but before ordering all Parts i want to make sure that it basically will work.

I’m starting with that Sketch, but as i need to exchange the Transistor to the availabe mentioned below, can someone with Experience please help me out with his Knowledge if this will work and be safe, and which Resistor/Diode i should go for?
grafik

Given:

  • +5V Powerbus (ESP32 Dev Board)
  • ESP32 GPIO and GND
  • LD2410 Sensor (+5V, 80mA typically, 200mA max)
  • Transistor PN2222ABU (NPN, 40V, 1A, 300MHz, HFE:35, TO-92)
  • Diode 1N4001 (THT, 50V, 1A, DO41)

Wanted:

  • Which Resistor should i go for?
  • Will it work with that Transistor?
  • Will it work with that Diode?

I know, this seems to the most basic Stuff. But i never did it, and seriously, the more i read, the more i’m getting scared :crazy_face:
It would be supercool if someone can just tell me if the exchanged Transistor will work with the Diode, and which Resistor i should go for.

1 Like

It should work with the component values listed on the schematic. The pi and the esp have the same logic level voltages.

1 Like

Of course i would give the schematic a shot. But where i order the other Parts (and much more), there is only that (slightly different) Transistor available.

Good to have it confirmed that the Logic Level Voltages are the Same :smiley: You see, i even didn’t know i would need to check that… :crazy_face:

Current values of the PN2222ABU are slightly higher than the 2N2222 shown in the schematic. The PN2222ABU is a good substitute.

Here are the differences between four variants of the 2N2222 family:

Parameter 2N2222 2N2222A PN2222 PN2222A
Collector-Base Voltage(VCBO) 60 V 75 V 60 V 75 V
Collector-Emitter Voltage(VCEO) 30 V 40 V 30 V 40 V
Emitter-Base Voltage(VEBO) 5 V 6 V 5 V 6 V
Collector Current(IC) 0.8 A 0.6 A 0.6 A 1 A
DC Current Gain(hFE) 300(Max) 300(Max) 300(Max) 300(Max)
Output Capacitance(COBO) 8 pF 8 pF 8 pF 8 pF
Collector Power Dissipation(PC) 1.2W 1.5W 625 mW 625 mW
Storage Temperature(TSTG) -65 to 200°C -55 to 150°C -55 ~ 150 °C -55 ~ 150 °C
Package TO-18 TO-92 TO-92 TO-92
1 Like

Sounds good, jippie, Thank you!
I really can’t interprete the Values of the 2N2222 Family, or better said - if it will work :frowning:
So: (sorry, i really need the ReAssurance, hold my shivering Hand :smiley:)
A Resistor with 1K and that Diode (1A) will safely do the Job? What would be the max mA the Device should draw?

It should be good, except you may need to look at the fan’s rating and make sure that you won’t pull more than the 5v supply can provide. If you do then the ESP may hang/reboot/brownout.

1 Like

Thanks Guys! Really appreciate all of your Help. Especially for helping with maybe stupid beginner Questions, i know that.

I will give it a shot, and you surely gonna hear from me when i burnt down my Flat :smiley:

1 Like

Here is an open-collector calculator 101

To drive a load to ground with an NPN transistor you need to pick the transistor on the following data

The Collector to Emitter voltage must be higher than your max voltage. In your case this is 5 V. So a 20V or 40V is more than fine. Just more than 5 V with some good margin.

The max collector current must be larger than your load current. Again you need a good margin.

The Max Power Dissipation must be larger than the power that will be burned in the transistor. This you need to calculate using the formular Power = Voltage x Current. The difficult part is the voltage. The voltage is the voltage over the collector to emitter. In the ideal world this is 0 assuming that the transistor is saturated. But in practical life expect 0.05 to 1 V. If find one that has max 0.2 V you should be good.

Now I said saturation. What is that? A transistor is saturated when the basis current is larger than the collector current divided by the DC current gain (hFE or beta). Here you need to look at the MINIMUM value for the hFE. For 2N2222A this would be something like 100 at a 200 mA collector current.

The voltage drop on base to emitter is always 0.6 to 0.7 V for silicon.

The port that drives the basis is from an ESP device so this is 3.3V when the port is high.

So you take your load which is 200 mA max. And then you divide the 200 mA by 100 and that is 2 mA.

So we need a resistor that is small enough to limit the current to 2.67 mA. The voltage over the resistor is the 3.3V - 0.7 V = 2.6 V. And then we use ohms law to calculate the resistor. R = V / I. So 2.6V / 2mA = 1.3 kohms.

We have to make sure the transistor is always in saturation. So we should actually make the basis current even larger. The more basis current the lower the saturation voltage. The max output from an ESP is something like 12 mA. But if we keep some margin. Let us say we feed 6 mA. That is 3 times more than we calculated for saturation.

Then the resistor then becomes (3.3-0.7)/0.006 = 433 ohms. So maybe 470 ohms would be a good value.

Then we can calculate forward and check everything is OK. At 470 ohms the basis current = output from ESP = 5.5 mA. The transistor collector current can then driver 5.5mA x 100 = 550 mA which is double the load so the transistor should saturate.

But what about the power in the transistor. If the saturation voltage is 1 V and the load is 200 mA, the power is 0.2 W. That little transistor will get warm. And the load will not get the full 5 V.

So not a good choice of device to begin with.

You need a device with a larger minium hFE and lower saturation voltage.

OR

Don’t use a bipolar transisistor in the first place.

A popular one is IRLZ44N. It turns on at a gate voltage around 2V so good for 3.3V logic. Its on resistance is 0.002 ohms so it will be stone cold at a 200 mA load. The ESP 8266 does not need to drive any current so it will be less warm.
A smaller one would be BS170 good for a few hundred mAs. Or BSS138.
The different types mainly differ on their speed but for DC you do not need to care about anything else than the turn on gate voltage and the current rating.

Logic level N channel transistors is really what you want to buy a little bag of. They are much easier to deal with. You do not need a resistor between the ESP output and the gate but it is normal to put one anyway to limit the damage if something fails. So 1 kohms is a good value. And to make sure the FET is off when the ESP is booting you also put 10 kohms from gate to ground.

1 Like

Wow.
Don’t know what to say. Thank you so much for that really in-depth-Explanation. Really appreciate that you took your Time, Thanks! First, i really need Time to understand everything, make clear i got it. There are already some follow-up questions in my Head, but i don’t want to ask yet, as i want to make sure i understood everything as max as possible :wink:

Big But: My Parcel with all Parts just arrived today :weary: :confounded: You said:

Wait. Okay, one Question i need to ask right now :smiley: Why 1V/200mA? Where do those Values come from?

But the big Question for me right now is:
What would you do in my Place, given you had those Devices?
I would love to start right away, and not collect for another Parcel. Aside that those FET are just pricier.
Would you say it could be risky starting with those PN2222ABU, or would you say “not optimal, but give it a shot”?

And if so, what Resistor would you take? I just didnt get it - would it be 1.3kohms, or 433ohms?

Looks like. But by now i have a little Bag of PN2222ABU and wondering if (and how) starting with those would be safe enough to try :woozy_face:

Again - thank you so much for taking your time, i really appreciate your effort, this awesome!

Thanks for the warm words. I was my hope with the long answer that many DIY people tinkering with ESP and Arduino could use the guidance.

Let us look at your transistor (I used numbers from the older 2N2222A earlier)

These are the important data for a transistor as a switch that changes state rarely (ie DC).

image

There is a note 4 saying that the data are for a pulse situation so ignore those. We are integrated in steady state.

You gave the max current of 200 mA in your question so that is what we are designing for.

We have values for the saturation voltage for 150 mA and 500 mA. Note that in the test condition the base current is 10% of the collector current. A 10:1 ratio is normal for measuring saturation. But your ESP device cannot source more than 12 mA.

150 mA is pretty close to 200 and for this the Vce(sat) is 0.3V. This is a maximum value. Your devices are probably better than that. But your current is a little higher than 150 mA and your base current cannot be higher than 12 mA. And you do not want to max out the ESP.

You probably end around 0.2 - 0.5 V voltage over your transistor at normal room temperatures and at 200 mA load and say 6 mA base current. The data for the hFE is not defined for your exact use. And this data sheet has no graphs. But looking at the different numbers I would say the hFE in your situation is probably at least 50. And your device may be much better. We require an hFE of 33 for a base current of 6 mA to give 200 mA. We should be OK.

And then the resistor value becomes (3.3-0.7)/0.006 = 433 ohms. Nearest standard values are 330, 390, 470 ohms. You started out with 1 komhs. That gives a base current of 2.6 mA. It may work. It will work when your load is at average. But if it is a sensor that peaks to 200 mA when it measure, it may go wrong when it starts measuring and the voltage drops from say 4.5 to 4.0 V.

You can put two 1k in parallel. That gives 500 ohms and that is probably OK.

Build it up and measure the voltage over the sensor

You show a schematic with a fan. A fan is a motor. Any inductive load like a motor, or a relay, will create some very high voltages when you turn the load off. The diode shown kills this spike and limit them so the transistor is not destroyed. If your sensor is just a non inductive load you do not need the diode

Just to complete the calculations. We now know the current in the resistor and the saturation voltage on the collector. We need to check power.

P = U x I. Transistor power worst case would be 0.5V x 0.2 A = 0.1W. This is 20% of the max for the transistor so we are good.

Resistor : P = U x I. Let us pick the 470 ohms. (3.3-0.7) * (3.3-0.7)/(470) = 14 mW. Any small leaded resistor is good. If it was SMD even a 0402 size would be OK. So no concerns about power.

Don’t worry about taking time to learn. I am an electrical engineer with 35 years experience. I am happy to teach people these skills. Electronics is a fantastic hobby. And it takes very little knowledge to tinker todays because you get all the difficult stuff as cheap modules from China. All you need to do it learn how to “glue” them together.

Kenneth

1 Like

Again, thank you so much for taking your Time! This is just so great.
I will give it a shot in a Couple of Days (i think i might need a clean working Desk and a couple of Hours to focus only on it, i can’t do this en passant for half an hour :slight_smile:).

I guess you are right, the Learning Curve is steep at the Beginning. Which i still fear a bit, but after it clicked there might be so many possibilities. Lets see where i end up. Thanks for helping me and the whole Community as well!

If your just starting out and you need components, go to Amazon or do a search for “transistor kit” I really likes these because they have a large selection of the more common ones you’ll need and they come in nice storage containers with names and part numbers. There are 10"s of thousands transistors to choose from but, a lot of them can be swapped out with one you do have already and won’t need to place an order for 1 or 2 transistors. If you havnt played around with any of the circuit simulators programs, you really should. Your free to experiment, lean, and you can burn up or destroy asany as it takes you to understand what’s going on. You won’t have a messy work bench to clean up afterwards too!

1 Like

I kmow the goal is to learn but just for the record
That’s a pretty standard circuit and you’ll need that circuit or a higher power one for something like turning on or controlling a 12/24v device from a 3.3v gpio, Dim led’sz change speed of a motor, and so on . They’re so cheap and you can wire up 12v leds to these in no time at all .

1 Like

Hurray! :partying_face:
Of course i was so excited, i couldn’t wait til i had some spare Time, i had delay less important Things :smiley:

It works! I can Turn on/off the Sensor via a Switch in HA (and later in the ESP itself). Though i neither came up with that simple Circuit, nor really understand whats going on, i feel quite proud :smiley:
This is what this Masterpiece of mankind looks like right now:

I went for a 470 ohms Resistor.
Measuring Voltage directly at the Sensor shows 1,3V (off) and 5,2V (on). 5,3V would be directly from the ESP.
Voltage between the switching GPIO and GND is obviously 0V (off) and 3,2V (on).
Unfortunatly my Meter does only AC Ampere.
Anything else i have to look out for to see if everything is cool, or if it will explode soon? :stuck_out_tongue_winking_eye:

@KennethLavrsen: Thank you so much for holding my shivering Hand :smiley: and great Support! :slight_smile:

Edit: If that might be already RTM - should i leave some Space between e.g. Transistors or Restistor (Heating), or doesn’t that matter?

3 Likes

Nice Topic, was looking to turn on/off a Fan and came across this topic. In your drawing you are talking about a fan, but in the text i see you are switching the LD2410.

In my situation i would like to turn off/on a fan in my mod of the Ikea Vindriktning but also adding the LD2410 to the mod. So i got curious why you want to turn off/on the LD2410?

The drawing for the circuit came from a simple rpi fan control :wink:

in the end it is about Energy saving. The Idea is to only let the ld2410 run, if the room is occupied (triggered by a pir sensor).

1 Like

Thanks for your clarification!

I know this is old but for anyone else looking. I highly highly recommend keeping these mosfet modules on hand. They come in single channel or 4 channel and there are so many places you can use these like driving led’s fans, motors, or use them instead of a relay for DC projects obviously. All my 12v outside landscaping lights are controlled by these and it was as simple as using stand-offs to stack 4- 4 channel driver boards.

https://www.amazon.com/ANMBEST-High-Power-Adjustment-Electronic-Brightness/dp/B09KGDDS37/ref=asc_df_B09KGDDS37/?tag=hyprod-20&linkCode=df0&hvadid=563798757335&hvpos=&hvnetw=g&hvrand=12513806678205819388&hvpone=&hvptwo=&hvqmt=&hvdev=m&hvdvcmdl=&hvlocint=&hvlocphy=9016042&hvtargid=pla-1567619045184&psc=1&mcid=27caa8c63e813fc881890fd33a2184df

https://www.amazon.com/ANMBEST-High-Power-Adjustment-Electronic-Brightness/dp/B08SJQTQ4Y/ref=asc_df_B09KGDDS37/?tag=hyprod-20&linkCode=df0&hvadid=563798757335&hvpos=&hvnetw=g&hvrand=12513806678205819388&hvpone=&hvptwo=&hvqmt=&hvdev=m&hvdvcmdl=&hvlocint=&hvlocphy=9016042&hvtargid=pla-1567619045184&psc=1&mcid=27caa8c63e813fc881890fd33a2184df&th=1

Out of interest, i find my LD2410C are unstable. They lockup after a week or so and no longer respond on the serial. Only way to get them going is to power cycle the LD2410C. I am looking to write a lamda function in ESP home to cycle the LD2410C if it becomes unknown. Has alignment with this thread.

Regards
C.