How to Build a Bidirectional Logic Level Shifter Using MOSFETs

mosfet logic level shifter bidirectional

In modern electronics, we often face a “language barrier” between components. A Raspberry Pi or ESP32 speaks 3.3V logic, while many sensors and older Arduinos speak 5V logic. Connecting them directly can result in a fried 3.3V chip.

To fix this, we use a Logic Level Shifter. While you can buy pre-made modules, building one yourself with a MOSFET is the best way to understand the physics of level translation.

Hardware Required

To build a single-channel shifter, you will need:

  • N-Channel MOSFET: Either a BS170 MOSFET or a 2N7000 MOSFET. (Note: Pay close attention to the reversed pinouts of these two!).
  • Two 10kΩ Resistors: These act as “pull-up” resistors.
  • Power Sources: 3.3V and 5V.

The Schematic & Wiring

The magic of this circuit lies in the MOSFET’s Body Diode. Here is how to wire it:

  1. Gate (G): Connect this to your Low Voltage (3.3V) power rail.
  2. Source (S): Connect this to your 3.3V Device Pin (e.g., ESP32 TX).
  3. Drain (D): Connect this to your 5V Device Pin (e.g., Arduino RX).
  4. Pull-ups: * Connect one 10kΩ resistor from the Source to 3.3V.
    • Connect the other 10kΩ resistor from the Drain to 5V.

How it Works (The Science)

This circuit is bidirectional, meaning data can flow both ways.

Case 1: 3.3V Side is HIGH (3.3V)

The Source and Gate are both at 3.3V. The VGS​ (Gate-Source Voltage) is 0V, so the MOSFET is OFF. The 5V side is pulled up to 5V by its resistor. Both sides stay High.

Case 2: 3.3V Side pulls LOW (0V)

The Gate is at 3.3V and the Source is at 0V. The VGS​ is now 3.3V, which is higher than the threshold of the 2N7000 MOSFET. The MOSFET turns ON, pulling the 5V Drain down to 0V. Now both sides are Low.

Case 3: 5V Side pulls LOW (0V)

The MOSFET is initially off, but the voltage flows through the internal body diode of the MOSFET from Source to Drain. This drops the Source voltage, creating a VGS​ difference that turns the MOSFET fully ON, completing the path.

Which MOSFET is better for this?

For high-speed data like I2C or SPI, the 2N7000 MOSFET is often preferred due to its lower input capacitance. However, if you are simply switching a signal to turn on a 5V relay from a 3.3V pin, the BS170 MOSFET is a rugged choice that handles more current.

Common Mistakes to Avoid

  • Forgetting Common Ground: Always connect the GND of the 3.3V supply and the 5V supply together.
  • Mixing up D and S: If you swap the Drain and Source, the body diode will stay forward-biased, and your 3.3V pin will be constantly “flooded” with 5V, potentially damaging it.

Testing Your Level Shifter

To verify your circuit, upload this simple “Blink” variant. It sends a signal from your 3.3V pin (ESP32/Pi) and expects to see the 5V device (Arduino) respond.

/* * elxhub Logic Level Shifter Test 
 * Use this to verify your 3.3V to 5V MOSFET translation.
 */

const int signalPin = 12; // Connect to the 'Source' of your 2N7000

void setup() {
  pinMode(signalPin, OUTPUT);
  Serial.begin(115200);
  Serial.println("Starting Logic Level Test...");
}

void loop() {
  // Pulling the 3.3V side LOW
  digitalWrite(signalPin, LOW);
  Serial.println("Signal: LOW (0V) -> 5V side should now be 0V");
  delay(2000);

  // Pulling the 3.3V side HIGH
  digitalWrite(signalPin, HIGH);
  Serial.println("Signal: HIGH (3.3V) -> 5V side should now be 5V");
  delay(2000);
}

Next Step: Now that you’ve mastered the signal side, why not learn how to switch high-power loads? Check out our Ultimate MOSFET Selection Guide to find the right part for your next motor project!

Conclusion

Building your own logic level shifter is a rite of passage for electronics hobbyists. It teaches you that a MOSFET is not just a switch, but a voltage-controlled gate that can protect your expensive microcontrollers.

Yoast SEO Settings

  • Focus Keyword: Logic Level Shifting MOSFET
  • SEO Title: How to Build a MOSFET Logic Level Shifter | 3.3V to 5V Tutorial
  • Slug: logic-level-shifter-mosfet-tutorial
  • Meta Description: Learn how to build a bidirectional logic level shifter using N-channel MOSFETs. Safe 3.3V to 5V translation for I2C, SPI, and UART circuits.
Scroll to Top