Hello Friends!
Please give me a hint what is the best way to switch between two mosfets with GPIO5 and control it with GPIO4? The relay is just to show what kind of logic I want. I believe it is possible to realize without relay, by using transistor or some logic components.
Thanks!
You want to control 2 MOSFETs with 2 GPIO pins? Where is the problem? What is the need for a relay or other components?
If I turn both mosfets at the same time, I believe the mosfets will burn out. So I want to add some of protection. I thought when I choose with GPIO5 what I want to have on the output VCC or GND is a safe way. And the GPIO4 will allow me to control the output, for example as PWM.
Google for "mosfet half bridge" (image search) to see some possibly useful examples.
Protection from what? You are correct, if both MOSFETs were turned on together, a short circuit would be made which would burn them out, if the power supply is capable of that.
If you are concerned about a code error, you can write your code in a way which will avoid that danger.
What voltage is VCC?
It could be no higher than 3.3V, otherwise @mastino2's circuit could not work.
12 or 24V
Then your circuit cannot work.
The ESP will not be capable of switching off the MOSFET that you incorrectly labelled "PNP".
this circuit is just for logic explanation
the main idea I want to choose with one GPIO output what will be my screw terminal vcc or gnd. And with second GPIO I want to control it.
Ok, then let us assume Vcc is 3.3V for convenience.
Maybe what you mean to say is that you want to connect your terminal to
- Vcc
- Ground
- Neither
I want to make universal board for my projects in garage, it will have 12/24v power input which will drive the mosfets. It will have 8 screw terminals, which I will be able to set as vcc or as gnd. So when I will connect a LED strip which must be controlled with GND, I will set this screw terminal to GND and control it with another pin for dimming. Instead if I want to connect a fan which is controlled with vcc. I will set this screw terminal to vcc, and control it with another pin also as PWM.
What type of LED strip is this? An RGB strip with common anode?
Why cannot this fan also be controlled by ground?
My reason for these questions is that I believe it is possible that your original question is what is called an "X-Y problem".
For just logic explanation you can use a 1:2 mux or a SPDT analog switch instead of the relay.
Exactly! Thank you very much!
If it is quite general purpose, based on your latest specification, you could probably do something like this (from mosfet - H-bridge driver - Electrical Engineering Stack Exchange)
But bear in mind:
- shoot through
- mosfet characteristics with low (say 3.3v) gate voltage and absolute maximum ratings.
- inductive loads may need a flyback diode.
- higher PWM frequencies may require stronger pullup/down resistors or special gate driver.
I don't expect to dissuade you, and I am only an amateur, but this sounds like a Really Bad Design Idea.
It would be up there with deciding to use black wire for Vcc and red for ground.
Just sayin'.
a7
How much CURRENT will you be switching?
Hi, @mastino2
What do you want the two outputs to do?
Motor direction and speed?
Where would the leads from the motor connect?
Thanks Tom..
![]()

