N-channel MOSFET as LED switch - Page 1
Forum section: electronics / beginners

author: VEGETA
author_rank: Super Contributor
timestamp: May 13, 2025, 08:50:32 pm
content: Hello,I have a project which uses ESP32 output pin as open collector, it is either outputting ground or nothing (hi-z) to control an LED which I made into RGB LED.Here is the original implementation guide:I decided to use an RGB LED instead, and found another way to implement the functionality by using an N-channel MOSFET as seen in attachments. this is because I don't want to tax ESP32 IO with more current than it can handle + the RGB LED chosen is common anode as seen in attachment. Firmware will make the LED (RGB LED) blink, pulse or solid depending on the action but overall same circuit.Is my implementation correct?I mean when ESP32 IO pin outputs 0v, the mosfet will be off and the rgb led will be off. When the IO pin outputs nothing (or high z), the mosfet will be kept on and thus rgb led is on.nevermind resistor values as I will change them according to the specific led ratings (I guess they are correct, but will check)please check and advise.

author: thm_w
author_rank: Super Contributor
timestamp: May 14, 2025, 12:00:25 am
content: No you don't want to do this. Look at the Vgs on the FET.Use the ESP 32 to drive the gate of the FET, and use it as a low side switch. Pullup needed if output is open collector only, default LED state will be on in that case. But why does output have to be open collector?Also RGB, but you have them all on at the same time, so you'll roughly get a white color. Not clear whats going on there.

author: sleemanj
author_rank: Super Contributor
timestamp: May 14, 2025, 01:19:52 am
content: No, N-Ch should be low side, see attached.Quote from: thm_w on May 14, 2025, 12:00:25 amThey have pots being used as variable resistors to adjust each die in order that they can adjust the combined colour.

author: VEGETA
author_rank: Super Contributor
timestamp: May 14, 2025, 06:34:37 am
content: QuoteI am using an already existing esp32 project, but implement it in my own. firmware and esp32-related hardware are pre-determined and I need to work with them. the output from IO17 is open collector. the software will modify the led status, and as you said, the led will be on by default unless software pulls it down to ground to shut it which makes it blink or so.I have fixed the circuit, please check attachment.Quoteas mentioned by the post by sleemanj, I have individual pots for each color to adjust, so the user can play with them until he gets the color that he wants.____QuoteI didn't see the image until I made my own. they look identical. I think mosfet orientation is correct too.

author: PGPG
author_rank: Frequent Contributor
timestamp: May 14, 2025, 05:52:10 pm
content: In my opinion original conception you found in this circuit is stupid.If LED has voltage drop of 2V then LED current is (3.3-2)/64.9=20mA.To switch LED off ESP output has to drive 3.3/64.9=51mA.So to drive LED needing 20mA someone load the output pin with 51mA.

author: VEGETA
author_rank: Super Contributor
timestamp: May 14, 2025, 06:33:20 pm
content: Quote from: PGPG on May 14, 2025, 05:52:10 pmsome people in their implementation use a series limiting resistor, but since i am using an RGB LED i wanted to do it properly by using the ESP as it should: giving command signals to a proper power switch.Hereis the project i am talking about. it adds bluetooth controller to retro gaming systems. the project is open and generalized so that you adapt it to your specific console.I wonder if i can just replace the n-mosfet with an npn transistor. I am fine with the nmosfet and will use it, just wondering.chosen mosfet is BSS138, i guess it works fine for the job despite R_on not so small. I may modify the circuit to work with 5v instead of 3.3v since theRGB LEDhas forward voltage of about 3.4 to 2 of its LEDs when peak current and brightness.So now ESP32 will send 3.3v activation signal to mosfet gate, and it will be fully on since Vgs = 3.3v... even when using 5v power source right? the currently chosen resistors suit the 5v to make RGB LEDs forward currents = 20mA. And with the 100 ohms series POT it can be dimmed to nothing or fully on... which is exactly what i want.

author: Terry Bites
author_rank: Super Contributor
timestamp: May 15, 2025, 03:22:25 pm
content: I imagine the micro will have an open drain rather than an open collector output.Will your LEDs get enough current on a 3V3 supply? Measure Vf @ the current you intend to use. Your MOSFET Vds@ If?A small bipolar such as a 2N3904 will be far easier to switch reliably than a mosfet on a 3V3 supply.

author: wraper
author_rank: Unknown
timestamp: May 15, 2025, 03:35:40 pm
content: Quote from: VEGETA on May 14, 2025, 06:33:20 pmGPIO is a proper switch as long as you do not exceed it's max current rating. Adding external switch just for the sake of adding more parts is stupid. Unless you need more than 40mA per LED or more than 120mA through all GPIO combined, you do not improve anything. And original example is especially stupid as it likely exceeds max pin current rating (unless MCU internal switch resistance is high enough to limit the current to acceptable level) and wastes a lot of power for nothing.

author: VEGETA
author_rank: Super Contributor
timestamp: May 15, 2025, 04:16:25 pm
content: Quote from: Terry Bites on May 15, 2025, 03:22:25 pmLED datasheet says 2 of the LEDs is 3.4v and the last one is 2.4v all at 20 mA. mosfet switch resistance is 1.6 ohms at 300 mA or similar, while using 130 ohm and 90.9 ohms for LEDs, i don't think the ~1-2.5 ohms mosfet resistance is gonna matter.therefore, I changed the voltage to 5v since that was the original design I did and the series resistances mentioned are based on 5v.I guess now it should work fine with the mentioned mosfet.Quotewell, the GPIOs are doing other stuff so I don't know if the GPIO pin will be enough, my estimation that it is best to off load it to an external switch rather than take a guess especially that it costs very little and I have used it elsewhere in the board.

author: VEGETA
author_rank: Super Contributor
timestamp: May 19, 2025, 06:34:19 am
content: Hello,I made it 5v as mentioned, and added the mosfet and so on.I want to make one final adjustment which is to add a physical switch to turn off the LED manually if needed.I have attached 3 pics:1A: the thing we did in previous posts, now have another mosfet for the physical switch. I don't link this as i fear that the Q17 mosfet won't work properly since its Vgs not referenced to ground.1B: alternative solution to 1A. now the physical switch in series to completely cut off the circuit to mosfet. when cut off, mosfet gate will be floating, will it work as intended?2: these are 4 other LEDs which are not open collector but rather normal: 3.3v on, 0v off. I added a series resistor to avoid shorting esp32 pin to ground when it is 3.3v.please check and advise, i feel solutions for "1" are not so optimal, but maybe 1B is better.___Another solution I thought of is using a physical slide switch or dip switch in series just before the LEDs, here are some parts I think works:https://lcsc.com/product-detail/DIP-Switches_Shenzhen-Kinghelm-Elec-KH-BM2-54-1P_C2842893.htmlhttps://lcsc.com/product-detail/Slide-Switches_G-Switch-MK-12C03-G015_C2890358.htmlthe slide switch one seems better, rated at higher current and smaller in size. using one per led.do you recommend this over the solutions above?

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 21, 2025, 02:52:36 pm
content: Why using the pin in OC mode? Just set it to push-pull and use a normal npn bjt.

author: VEGETA
author_rank: Super Contributor
timestamp: May 22, 2025, 05:46:36 am
content: Quote from: DavidAlfa on May 21, 2025, 02:52:36 pmI don't know since I am using the firmware as-is, I have to comply with their choices.still waiting for feedback on my last post, if not, then I will simply use the physical slide switch in series as mentioned.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 22, 2025, 12:56:44 pm
content: Then use a 2K2 resistor to VDD and a transistor (2n2222A, BC547C) to drive the leds.It'll consume ~1mA while the leds are off...

author: VEGETA
author_rank: Super Contributor
timestamp: May 22, 2025, 05:09:02 pm
content: Quote from: DavidAlfa on May 22, 2025, 12:56:44 pmyou mean instead of mosfet circuit above?there is another issue i posted which is how to disconnect the entire circuit regardless of esp32 commands, using a physical switch,

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 22, 2025, 05:56:31 pm
content: Even better with a pnp, only consumes power when the leds are on.Add the switch between the esp32 pin and the transistor, the led will stay off by default.You can omit R1 for static switching (Just turning it on or off), but might be required for faster response if using PWM.

author: VEGETA
author_rank: Super Contributor
timestamp: May 22, 2025, 07:10:04 pm
content: Quote from: DavidAlfa on May 22, 2025, 05:56:31 pmthe LED is powered by 5v while esp is by 3.3v. I guess I can use separate supplies with this circuit right? meaning 5v to LED path and 3.3v to R1 to PNP base.the switch can be put between esp32 pin and R3, right? when it is off, the pnp will have "high" on its base which means off, which is the intended function. while if switch is on, then esp32 decides if led is on or off.as for resistor R1 and R3 values, can they all be 10k?

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 22, 2025, 07:52:47 pm
content: No, PNP will conduct if there's more than 0.6V difference between base and emitter.Then go back to NPN.Put the switch between the base and gnd.Or for best efficiency, a pnp+npn...R1 should be high, it's 100K so Q1 gain puts little current to the base of Q2, a way to simplify and reduce the part count.220K might be enough bust must be adjusted based on the transistor gain (hfe).

author: VEGETA
author_rank: Super Contributor
timestamp: May 22, 2025, 08:07:05 pm
content: i have added the switch, please check. I rather prefer 10k over 2.2k to consolidate value if possible.can i do it with n-mosfets? since i already have them in the board. i think it can work.in anyway, i prefer single transistor circuit as i don't care much about efficiency. if still going with npn rather than n-mosfet then will make sure the circuit works with any standard npn without going through specific gain for specific chip.what do you think now?____the potential issue that I have with such circuit is when the switch is connected to ground and esp32 outputs 3.3v, here it would be shorting 3.3v to ground. did you think about this or i noticed it wrongly? adding a series resistor to esp32 pin will create a voltage divider when esp32 does not output 3.3v which i am not sure if it is gonna be ok with n-mosfet.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 22, 2025, 08:38:48 pm
content: The ESP can't output 3.3V as it's open drain, only pull to ground.Sure, if your fets are ok to switch with only 3.3Vgate, they'll work.Most fets will not, which model are you using?

author: VEGETA
author_rank: Super Contributor
timestamp: May 22, 2025, 09:03:51 pm
content: Quote from: DavidAlfa on May 22, 2025, 08:38:48 pmright now I have BSS138 mosfet, can change it to anything that works and cheap.largest resistor used in board is 47.5k, can it be used or must be 100k? since it is a pull-up for mosfet gate then i guess lower resistors are ok. correct?all of this to the open collector LED, called "global".while other LEDs are 4 for the ports, they are not open collector but rather normal, 3.3v active and 0v off.for those, i have attached the current circuit. esp32 pin connected to series 10 resistor then to mosfet gate for activation while a physical switch from gate to ground.when switch is off, esp32 can deliver 3.3v to gate and mosfet will be on. while if switch is on, mosfet gate will be ground even if esp32 sends 3.3v since there is a resistor now.what do you think now about both of these situations?

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 22, 2025, 09:31:23 pm
content: 47K is fine. Remember fets work on voltage, not current, so lower resistors will only waste power.If you want to use PWM in the KHz range, then 1K resistor should do fine.

author: VEGETA
author_rank: Super Contributor
timestamp: May 22, 2025, 10:01:39 pm
content: Quote from: DavidAlfa on May 22, 2025, 09:31:23 pmthe firmware can blink the leds or blink slowly, or full solid on.i guess 47.5k is ok for all these uses, if not, then 10k.so you confirm that both circuits work fine? i may start layout tomorrow.is the selected mosfet ok? what feature or spec i need to check for this application?I believe it is the threshold voltage, which is a lot less than 3.3v for this mosfet.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 23, 2025, 07:44:29 am
content: Should do finebut never go with the layout right away, make a small test in a proto board first!

author: VEGETA
author_rank: Super Contributor
timestamp: May 23, 2025, 11:35:58 am
content: Quote from: DavidAlfa on May 23, 2025, 07:44:29 amtesting on proto is for pussies.actually, all my breadboard and components are buried in places now, really hard to dig them all up.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 23, 2025, 11:54:28 am
content: In the worst case you'll have to replace the fet or the resistors, so pretty safe

author: VEGETA
author_rank: Super Contributor
timestamp: May 23, 2025, 03:46:21 pm
content: Quote from: DavidAlfa on May 23, 2025, 11:54:28 amwhat can go wrong with resistor or mosfet? so I can know first hand to pick properly. right now, i guess my choice is good.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 23, 2025, 04:18:55 pm
content: Bad conduction due low VGS, for example. In that case just get a more sensible fet.The BSS138 doesn't perform great at 3.0 V, but 0.5 Ohm RDS is good enough for a low power led.If you wanted to drive something heavier, lets say 1A, it would disipate 0.5W, that small package would get pretty toasty.

author: VEGETA
author_rank: Super Contributor
timestamp: May 23, 2025, 05:04:05 pm
content: Quote from: DavidAlfa on May 23, 2025, 04:18:55 pmcalculated max current is 60 mA, so maybe 70 mA is beyond max, but nothing more than that.if BSS138 is not good enough for 3.3v Vgs, then what spec should I look for?I checkedthis datasheet(there are a lot of datasheets for it) and found:at vgs=2.75v -> Ron = 3 ohms for <= 100mA current. if bumped to 3.3v it could reach something like 2.5 ohms. not the best for sure, but can it really make an issue?i want to get it from LCSC, so if you know a good and cheap part please recommend it or maybe what spec should i look for.i checked for vgs at 3.3v or similar to see the resistance. do i need to look for this or more?other alternatives found that can be better imo:RUM002N05T2L -> figures says it is fully on at 1.2 vgs, its resistance is 2.2 ohm max. small footprint.AO3400A -> small vgs threshold and low resistance (in milli-ohms)BSS806N -> also like all above. at 2.5v it is fully on with 40mOhm resistance. I guess it tolerates 3.3v.all 3 seems good for this, what do you think?:

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 23, 2025, 05:25:14 pm
content: Don't overthink it, will be fine for 200mA or so.What you're searching for is the Gate Threshold Voltage.Also check the column RDS ON @ x voltage.For example, these little SOT23 beasts are less than 2 cents in small quantities, have RDS ON=27mOhm with only 2.5VGS, will happily push 4 amps.https://lcsc.com/product-detail/MOSFETs_AGM-Semi-AGM3400EL_C5447831.html

author: VEGETA
author_rank: Super Contributor
timestamp: May 23, 2025, 06:24:08 pm
content: Looks like what you posted is asian-brand one similar to what i posted above.here is the original one:https://lcsc.com/product-detail/MOSFETs_Alpha-Omega-Semicon-AO3400A_C20917.htmlit is from alpha & omega, there are others from onsemi and diodes as well. kinda the same cheap prices but this one has thousands in stock while the one you posted has only 20.and at <100 mA it will be perfectly working without issues.i have other P-mosfets in the design for something else, will check and pick them to be as good as this one.