GPIO Output - Pulling The Line High or Low With Switch (Overriding The GPIO)

Thread Starter

Mahonroy

Joined Oct 21, 2014
406
Hello,
I have a GPIO of a microcontroller connected up to the gate of a mosfet (BUK9Y107-80E) like this image below:
gpio_switch1.jpg

I want to add a SP3T switch (3 position: ON - OFF - ON function) so that it functions like this: center position and the GPIO works like normal. Push to the right and it overrides the GPIOs output and makes it high, push to the left and it overrides the GPIOs output and makes it low.

Would something like this work? (note the schematic shows a 2 position switch... the 3 position switch would be just like this only there are no connections made in the middle position):
gpio_switch2.jpg

I just kind of threw out those resistor values. Am I on the right track or is there a better way of doing this? I know a DP3T switch can be configured to a true 3 position switch and I could make it work that way, but I am restricted to a single pole switch here. Thanks and any help or advice is greatly appreciated!
 

JohnInTX

Joined Jun 26, 2012
4,787
You only need the 5k resistor. The others do nothing useful (unless the GPIO will ever be tri-state, in which case you need the 20k).
I would recommend the pulldown. After a power on reset, the GPIO will indeed be in tri-state until the program gets around to initializing the ports. Depending on the program, Power On Reset Timer CONFIG setting, internal oscillator startup time, finger on MCLR/ button etc, there is enough time to allow the drivers to false-ON momentarily on power up/reset until the ports finally are initialized.
 

MrAl

Joined Jun 17, 2014
11,496
Hello,
I have a GPIO of a microcontroller connected up to the gate of a mosfet (BUK9Y107-80E) like this image below:
View attachment 129046

I want to add a SP3T switch (3 position: ON - OFF - ON function) so that it functions like this: center position and the GPIO works like normal. Push to the right and it overrides the GPIOs output and makes it high, push to the left and it overrides the GPIOs output and makes it low.

Would something like this work? (note the schematic shows a 2 position switch... the 3 position switch would be just like this only there are no connections made in the middle position):
View attachment 129047

I just kind of threw out those resistor values. Am I on the right track or is there a better way of doing this? I know a DP3T switch can be configured to a true 3 position switch and I could make it work that way, but I am restricted to a single pole switch here. Thanks and any help or advice is greatly appreciated!
Hi,

How much current can your GPIO source and sink?
If you can lower the 5k to 1k that would be better, or else you have to raise the 20k to a higher value because 5k and 20k drop 20 percent of the voltage and so the mosfet gate might only get around 2.6v and that might not be enough (check data sheet).
You can also lower the switch pull up and pull down's to 100 ohms to make sure they can override the GPIO resistor.
.
 

AlbertHall

Joined Jun 4, 2014
12,347
I would recommend the pulldown. After a power on reset, the GPIO will indeed be in tri-state until the program gets around to initializing the ports. Depending on the program, Power On Reset Timer CONFIG setting, internal oscillator startup time, finger on MCLR/ button etc, there is enough time to allow the drivers to false-ON momentarily on power up/reset until the ports finally are initialized.
Yes, good point.
 

Thread Starter

Mahonroy

Joined Oct 21, 2014
406
Hey guys, thanks for the great advice!
The GPIOs can source/sink 25mA @ 3.3v.

So it sounds like I should leave the pull down resistor on the mosfet gate as a precaution.

And it also sounds like I should just remove the 1K resistors completely (for the 3.3v rail and ground), so the switch will just make a direct connection to the rail/ground?

So I really just need to decide what values to use for the mosfet gate pulldown and the GPIO resistor. If I use a 1K resistor for the GPIO (instead of 5k), am I correct that it will only be 3.3mA on the GPIO? Which would be fine? Or should I stick with the 5K and 20K combination?

EDIT:
So I cannot figure out if I should eliminate the 1k resistors completely or not. It seems that maybe I should at the very least have like a 200 ohm resistor there? What are your thoughts?
 
Last edited:

geekoftheweek

Joined Oct 6, 2013
1,222
I'm no expert so take what I have in caution.

MOSFETs don't draw any more current than what it takes to charge the gate, then after that it may or may not leak a small amount so the highest draw on your GPIO will occur when you first send it power. There's formulas to figure it since it does play a part in how much heat is generated while the MOSFET switches on or off.

Personally I've never put a resistor in line with the gate, but it's probably smart in the event something goes wrong and shorts your GPIO output.

As far as pull down you'll want it to be safe and to make sure the MOSFET is turned off... I've melted a few expensive MOSFETs because I didn't take into consideration the tri state of the GPIO would cause it to turn on.

I would do 1k for R10 myself and 100k or more on R4. I usually do 1 Meg for a pull down.

Like I said... I'm not expert and still learning myself, but this is what works for me.
 
Last edited:

Thread Starter

Mahonroy

Joined Oct 21, 2014
406
So one more question if you don't mind?

I decided to switch over to a solid state relay (SSR) instead, and was trying to simplify the switch circuit. Like above, my goal is to have the center position be whatever the GPIO is (on or off), then you can either manually override it to on or off with the switch as well.

Here is what I have so far:
gpio_switch3.jpg

The problem is that if the GPIO is high, switching it to off will cause too much current draw from the GPIO. Likewise, if the GPIO is low, switching it to high will sink too much current.

Is there a good solution here to accomplish this? Thanks again for all of your help.
 

AlbertHall

Joined Jun 4, 2014
12,347
I think you need a transistor. If you use a PNP transistor then the GPIO will work in the same direction as your original circuit.
 

Thread Starter

Mahonroy

Joined Oct 21, 2014
406
When the GPIO is an input then the transistor should be switched off, LED unlit.
Do you want it to be lit in that case?
What you just described is exactly how I want it. Unfortunately this is not the case because of that R5 resistor is pulling it high when the GPIO is an input.

EDIT:
Now that I'm looking at that some more... that R5 shouldn't even be there right? Seems like that R5 should be connected between the switch (pin 1) and 3.3v right?
 
Top