# Switch power to load using 2 GPIO pins, 5V and 12V

I will like to use 2 GPIOs on a raspberry pi to switch on a device (piezo buzzer), using 5V or 12V at a time.

My idea is to use two MOSFETs and place pullup resistors on the gates of the MOSFETs from the GPIOs to ensure they are on when needed. Then a program ensures the 2 GPIOs can never be on at the same time, just one at a time.

Is this safe/legal? I know that I will need P-channel MOSFETS, here's a schematic of my thought process and I hope you can correct or tender suggestions with better solutions.

simulate this circuit – Schematic created using CircuitLab

• Is the load just an example or R6 is really an external resistor in series with the buzzer, and 1k is really its value? Jun 25, 2022 at 19:54
• @devnull they're both just examples Jun 25, 2022 at 20:02
• Thanks. I wonder (in a XY Problem sense) if the question isn't really: I'd like to control a buzzer with two volume levels using GPIOs. I have 3.3V, 5V and 12V rails available and the MCU is connected to the 3.3V rail. Does this circuit work for this? Jun 25, 2022 at 20:10
• @devnull i appreciate the correction, that is a clearer explanation of what i am trying to do, thanks. Jun 25, 2022 at 20:27
• Which piezo buzzer is it? Jun 26, 2022 at 20:37

One problem with your circuit is that you would not be able to turn off the PMOS given that your GPIOs can drive up to 3.3V only, when high.

When GPIO13 and GPIO18 are driven low, no issues, the PMOS will see high enough voltage differential between gate and source to fully turn on. When those GPIOs are driven high (3.3V), there is still a significant differential between gate and source, especially with the PMOS driven by GPIO18 — 3.3V at the gate minus 12V at the source means -8.7V, more than enough to turn on a PMOS. In the case of the PMOS driven by GPIO13, the differential would be 3.3V-5V = -1.7V, maybe not enough to fully turn it on, but enough to be partially on.

You may want to do something like this:

simulate this circuit – Schematic created using CircuitLab

It flips your logic. Now a high on GPIO13 for example, means M1 will conduct and vice versa. This configuration will ensure the PMOS are truly OFF when they need to be. Notice how the gates of the PMOS are pulled-up to the same voltage as what's at the source terminal, that way, when the low side NMOS are not active, the voltage across gate and source on those PMOS is 0V, keeping them off.

There's a complication though. When the 12V PMOS is turned on, current will still find a path from 12V to 5V through M1's body diode, so you need to protect the 5V rail by adding a diode, for example (See D1). Another possibility is replacing M1 with a PNP transistor, that way you would not need D1 but will need a resistor to the base to limit the current through it.

• thanks for the suggestion. circuit lab simulation seems to show no voltage at the buzzer's node. perhaps, could i be doing something wrong? Jun 25, 2022 at 20:29
• @Big6 Don't the FETs have internal parastic diode in them? If you turn on the 12V to buzzer, current would flow from 12V to buzzer node, and via diode of M1 to 5V, likely destroying something in the process. Jun 25, 2022 at 23:29
• @Justme Thanks for pointing that out!
– Big6
Jun 26, 2022 at 14:46