Given the Pi’s 3.3V logic and risk of “frying it” with higher voltage applied at GPIO pin, I am using a voltage divider with MOSFET as a electronically-controlled switch to supply GPIO input. The circuit is as shown in C below.

voltage divider with MOSFET

Fig. A is the basic voltage divider with a switch. In B, I have replaced the switch with a MOSFET (n channel, P30N06LE) which is operated by Vi at gate from a sensor and feeds Vo to GPIO. (The light sensor is a photoresistor in voltage divider configuration and not shown. Sufficient to know that it switches MOSFET on and off correctly per lighting condition.) The Vo is still 5V for logic high in B, so I have inserted a diode with voltage drop of 2V in C to reduce voltage to 3V suitable for Pi GPIO. The diode I have used is a LED, which has the additional benefit of informing status (on/off).

hardware ldr

This is the implementation I have tested on breadboard with a bench power supply. It operates correctly, so the LED comes on in the dark and Vo is near 3V in ambient light and 0V in dark or dim light.

So am I good to wire up Vo to Pi without frying it?


1 Answer 1


The circuits shown in A, B, and C are not safe for the Raspberry Pi.

In your schematic diagram figures, when the transistor is OFF (when the switch is open) the voltage at the transistor's drain is +5 VDC, which will damage the PI's input pin.

The circuit you want looks more like the circuit shown in Fig. 1:

enter image description here

Figure 1.

(NB: I apologize for the lousy graphic. For some reason the CircuitLab schematic editor wouldn't insert a schematic diagram into my answer.)

When transistor Q1 is OFF, the voltage divider R1 and R2 outputs a logic HIGH signal to the Raspberry Pi's (RPI) GPIO input pin. When transistor Q1 is fully ON (operating in ohmic mode), the voltage at Q1's drain is approximately ground and therefore a logic LOW signal is applied to the RPI's GPIO input pin.

Note that the CONTROL signal has negative logic:

LOW       OFF    HIGH
HIGH      ON     LOW

If the CONTROL signal is not present (neither logic LOW nor HIGH), transistor Q1's ON|OFF state cannot be guaranteed. For example, a microcontroller's I/O pins are default configured as inputs when power is first applied to the microcontroller, and logic input pins "float"; they do not drive logic LOW or logic HIGH output signals. Therefore, you should connect to Q1's gate either a pull-up resistor (to +5 V) or a pull-down resistor (to ground) to ensure Q1's default state is either ON or OFF, respectively. A resistance of \$10\,\mathrm{k\Omega}\$ should work fine for this resistor.

If the +5 V supply receives power from a battery, eliminate the LED because it wastes power.

If you want the LED annunciator, I recommend you use a P-type enhancement mode MOSFET (PMOS) as a high side switch as shown in Fig. 2.


simulate this circuit – Schematic created using CircuitLab

Figure 2.

When the CONTROL signal is approximately zero volts (logic LOW), PMOS transistor M1 turns ON and supplies +5 VDC power to the light-emitting diode (LED) D1 and the current limiting resistor R1. The voltage that's applied to the Raspberry Pi's GPIO input is approximately

$$ V_{RPI\_GPIO\_IN(MAX)} \approx 5\,\mathrm{V} - V_{D1} \quad \quad (1) $$

where \$V_{D1}\$ is the forward voltage drop across diode the LED D1. This assumes \$V_{CC} = 5\,\mathrm{V}\$ and \$V_{D1} \ge 2\,\mathrm{V}\$, which ensures \$V_{RPI\_GPIO\_IN(MAX)} \le 3\,\mathrm{V}\$.

Turning OFF transistor M1 removes +5 VDC power from diode D1 and resistor R1, and the voltage that's applied to the Raspberry Pi's GPIO input is approximately zero volts (logic LOW). To turn OFF M1, the CONTROL signal's voltage must be within the range

$$ \left ( V_{CC} + V_{TH} \right ) \le V_{CONTROL} \le V_{CC} \quad \quad (2) $$

where \$V_{CC} = 5\,\mathrm{VDC}\$ in this example, and \$V_{TH}\$ is M1's minimum gate-source threshold voltage as specified in M1's datasheet. (NB: Recall that a PMOS transistor's gate-source voltage has negative polarity—i.e., the gate voltage must be negative with respect to the source voltage.) For example, assume M1's datasheet states the part's minimum gate-source threshold voltage is \$V_{TH} = -1\,\mathrm{V}\$. To turn M1 OFF, the CONTROL voltage must be between

$$ \left ( V_{CC} + V_{TH} \right ) \le V_{CONTROL} \le V_{CC}\\ \left ( 5\,\mathrm{VDC} + (-1\,\mathrm{V}) \right ) \le V_{CONTROL} \le 5\,\mathrm{VDC}\\ \rightarrow 4\,\mathrm{VDC} \le V_{CONTROL} \le 5\,\mathrm{VDC} $$

For the circuit shown in Fig. 2, the CONTROL signal has negative logic:

LOW       ON     HIGH
HIGH      OFF    LOW

In some designs the CONTROL signal voltage cannot achieve the voltage range specified by relation (2), and therfore the CONTROL signal cannot turn OFF transistor M1 (M1 is always ON). For example, if the power source is +24 VDC rather than +5 VDC, and the CONTROL signal is a 5 V logic signal, a logic HIGH signal (~5 V) on the CONTROL line obviously does not achieve a voltage ≥ 23 V which is required to turn off M1 (assuming \$V_{TH}=-1\,\mathrm{V}\$ for M1). In such cases the circuit designer adds a second transistor—usually an N-type enhancement mode MOSFET (NMOS) —that performs a voltage translation function (5 V to VCC) as shown in Fig. 3.


simulate this circuit

Figure 3.

When the CONTROL signal is approximately zero (logic LOW), NMOS transistor M2 is OFF, the voltage at M2's drain and M1's gate is approximately VCC, PMOS transistor M1's gate-source voltage is approximately zero volts, transistor M1 turns OFF, and VCC power is disconnected from R2, R3, and D1. In this case, resistor R3 functions as a pull-down resistor that places a logic LOW voltage on the Raspberry Pi's GPIO input pin (\$V_{RPI\_GPIO\_IN} \approx 0\,\mathrm{V}\$).

$$ V_{GS1} = V_{G1} - V_{S1} = (\sim V_{CC}) - V_{CC} \approx 0\,\mathrm{V} \quad \quad (3)\\ V_{GS2} = V_{G2} - V_{S2} = (\sim 0\,\mathrm{V}) - 0\,\mathrm{V} \approx 0\,\mathrm{V} \quad \quad (4)\\ $$

When the CONTROL signal voltage is sufficiently HIGH (positive) to fully turn ON NMOS transistor M2 (i.e., M2 is operating in ohmic mode), the voltage at M2's drain and M1's gate is approximately zero volts, PMOS transistor M1's gate-source voltage is approximately -VCC volts, transistor M1 turns ON, and VCC power is applied to R2, R3, and D1. (NB: Transistor M2 is chosen so that a logic HIGH signal on the CONTROL line fully turns ON transistor M2.) When NMOS transistor M2 is ON, resistor R1 limits to a safe level the flow of current through M2's drain-source path. The resistance values for R2 and R3 are chosen so that when M1 is ON (a) the flow of current through LED D1 is limited to a safe level, and (b) the voltage drop across R3 is about 3 V, which is a safe and valid logic HIGH voltage for a digital input pin on a Raspberry Pi.

$$ V_{GS1} = V_{G1} - V_{S1} = (\sim 0\,\mathrm{V}) - V_{CC} \approx -V_{CC} \quad \quad (5)\\ V_{GS2} = V_{G2} - V_{S2} = (\sim 5\,\mathrm{V}) - 0\,\mathrm{V} \approx 5\,\mathrm{V} \quad \quad (6)\\ $$

(NB: Equation 6 assumes the CONTROL signal is a 5 V logic HIGH signal.)

For the circuit shown in Fig. 3, the CONTROL signal has positive logic:

LOW       OFF    OFF    LOW
HIGH      ON     ON     HIGH

One final comment (nag, actually!). The transistor symbol you've used in your schematic drawings is for a depletion mode MOSFET, but the transistor you are using is an enhancement mode MOSFET. Using the correct transistor symbol is important because the answer to your question depends entirely on the type of MOSFET you are using. The answer for a depletion mode MOSFET is completely different from the answer you'd get for an enhancement mode MOSFET.

  • \$\begingroup\$ Jim, thank you so much. I found your illustrations very helpful and your comments are spot on. Thank you so much for your help. You rock, dude! \$\endgroup\$
    – Sun Bee
    Mar 24, 2020 at 16:15
  • 1
    \$\begingroup\$ @SunBee, I just realized the circuit I provided in Fig. 2 will work, but it is probably not practical. Resistor R1's value will be quite small, which means there will be substantial current through R1 and Q1 when Q1 turns on, and therefore R1 must dissipate more power than you'd like it to. I'll post a revised / improved schematic soon. \$\endgroup\$ Mar 24, 2020 at 17:07
  • 1
    \$\begingroup\$ Hello Jim, I implemented the circuit in fig. 1 with R1=R2=10k. With control LOW, Q1 is off and RPI_GPIO_IN is a nice and stable 2.5V. With control HIGH, Q2 is on and RPI_GPIO_IN is nearly zero. The circuit is drawing very little current from my bench power supply at 5V setting. Just wanted to let you know and I look forward to your fig. 2. Thank you again. \$\endgroup\$
    – Sun Bee
    Mar 24, 2020 at 19:27
  • \$\begingroup\$ Your detailed answer has clarified many concepts about use of MOSFETs in switching. Thank you, Jim. \$\endgroup\$
    – Sun Bee
    Mar 25, 2020 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.