How Bidirectional MOSFET Level Shifters Work (3.3V to 5V Tutorial)

Many modern microcontrollers like ESP32Raspberry Pi Pico 2STM32 Nucleo-F401RESTM32F103C8T6 ("Blue Pill") and modern Teensy boards operate primarily on 0V to 3.3V logic while standard peripherals—such as 16x2 I2C LCDs and 5V ultrasonic or gas sensors—require 0V to 5V logic. Connecting 3.3V logic devices with 5V logic devices such as for I2C communication creates problem in both the direction. Consider as an example a 3.3V logic ESP32 is connected to a sensor which operates in 5V logic range using I2C communication. The 3.3V logic output from for example ESP32 might not be sufficient to reach the input high threshold level of the sensor. Similarly, the sensor 5V logic output most likely damage the 3.3V tolerance pins of the ESP32. 

Using a MOSFET-based 3.3V to 5V logic level shifter as shown below solves both issues—it safely boosts the 3.3V SDA/SCL lines to 5V to drive the LCD display, while protecting the ESP32's GPIO pins from 5V over-voltage.


In the above circuit diagram, the 3.3V logic port and 5V logic port are connected to the 0 to 3.3V logic devices pin such as ESP32, and the 5V logic port is connected to the 0 to 5V logic device pin such as sensor or microcontroller. The Q1 is N-Channel MOSFET like BSS138, 2N7000 etc. As shown, the gate of the N-MOSFET is connected to 3.3V and the 3.3V logic port is pulled high with resistor R1=10kohm and is connected to the source of the N-MOSFET. On the other side, the 5V logic port is pulled high using 10kohm resistor. When the 3.3V logic port is high at 3.3V, the source is 3.3V and the gate is also 3.3V, and so gate to source voltage (Vgs) is zero and hence the MOSFET is turned off on and the output 5V at the 5V logic port. When the 3.3V logic port is grounded or logic 0, then gate to source voltage (Vgs) is 3.3V-0V = 3.3V and the MOSFET is turned on, and when the MOSFET is turned on, the 5V logic port goes to logic 0 or is grounded. This directional action of the MOSFET level shifter is shown in the video below.


ESP32 Interfacing with I2C LCD using MOSFET Level Shifter

An example of using bidirectional N-MOSFET level shifter circuit to interface two different voltage domain ESP32(0V to 3.3V) and LCD(0V to 5V) using I2C communication is shown in the circuit diagram below.

ESP32 Interfacing with I2C LCD using MOSFET Level Shifter

Here two bidirectional n-channel MOSFET(BSS138) level shifter has been used, each for SDA and SCL pins of the ESP32 devkit v1 and JHD-2X16-I2C LCD. Other N-channel MOSFET that can be used. When replacing the BSS138 in a 3.3V to 5V logic level shifter, you need an N-channel MOSFET with a low Gate Threshold Voltage ($V_{GS(th)}$) so it turns fully ON with just 3.3V at the gate.

Through-Hole (TO-92) Replacements

  • BS170: Very common and easy to breadboard. $V_{GS(th)}$ is around 0.8V to 3V, making it a reliable drop-in choice for low-speed logic level shifting.

  • 2N7000: Essentially the TO-92 version of the 2N7002. Very cheap and widely available. (Note: Check the pinout before inserting, as Source and Drain pins can vary by manufacturer).

  • VN10KM: Classic low-threshold N-channel MOSFET in a TO-92 package that works well with 3.3V logic signals.

Surface-Mount (SOT-23) Replacements

  • 2N7002: The most common general-purpose SMD N-channel MOSFET. Extremely cheap and available everywhere, though its $V_{GS(th)}$ can sit up to 2.5V, so ensure you source logic-level variants for high-speed switching.

  • FDV301N: Specifically designed for low-voltage logic level switching. Features a very low threshold voltage ($V_{GS(th)}$ around 0.75V) and low $R_{DS(on)}$ at 2.7V, making it superior to the BSS138 for 3.3V bus speeds.

  • AO3400A: A higher-current SMD option with an ultra-low threshold ($V_{GS(th)}$ ~0.65V to 1.45V). Ideal if your shifted line needs to drive heavier loads alongside logic signals.

Key Parameter Checklist for Custom Selection

If you pick a different transistor from your parts bin, check these three specs in the datasheet:

  1. $V_{GS(th)}$ (Gate-Source Threshold Voltage): Must be well below 3.3V (ideally $\le 1.5\text{V}$).

  2. $R_{DS(on)}$ tested at $V_{GS} = 2.5\text{V}$ or $3.3\text{V}$: Ensures the channel fully conducts at 3.3V logic levels.

  3. Integrated Body Diode: Essential for bidirectional level shifting to pull down signals via reverse conduction.

Similarly, you can use the same level shifter circuit if you want to interface 3.3V microcontrollers like ESP32-CAM, Raspberry Pi, STM32F103C8T6 ("Blue Pill"), STM32 Nucleo-F401RE, with the following listed 5V sensors:

Environmental & Climate Sensors

  • BME280 / BMP280: Measures temperature, barometric pressure, and relative humidity (BME280) or temperature and pressure (BMP280).

  • AHT20 / DHT20: Digital temperature and relative humidity sensors.

  • SHT31 / SHT40: High-accuracy digital temperature and humidity sensors.

  • MCP9808: High-accuracy digital temperature sensor ($\pm0.25^\circ\text{C}$ typical accuracy).

Motion & Orientation Sensors (IMUs)

  • MPU6050: 6-axis Motion Tracking device combining a 3-axis gyroscope and a 3-axis accelerometer.

  • MPU9250: 9-axis sensor combining 3-axis accelerometer, gyroscope, and magnetometer.

  • LSM6DS3 / LSM6DSOX: 6-DOF IMU chips widely used in modern electronics and wearables.

  • BNO055: 9-axis absolute orientation sensor with built-in sensor fusion processing.

Light, Color & Optical Sensors

  • BH1750: Digital ambient light sensor with direct Lux output.

  • TSL2561 / TSL2591: Digital luminosity and light-to-digital sensors for visible and IR spectrums.

  • TCS34725: RGB color sensor with IR blocking filter and white LED control.

  • VL53L0X / VL53L1X: Time-of-Flight (ToF) laser-ranging distance sensors.

Gas & Air Quality Sensors

  • SGP30 / SGP40: Indoor air quality sensor measuring $eCO_2$ and TVOCs (Total Volatile Organic Compounds).

  • SCD30 / SCD40: True NDIR $CO_2$ sensors for air quality monitoring.

Analog Extensions & Interface ICs

  • ADS1115 / ADS1015: 16-bit / 12-bit precision Analog-to-Digital Converters (ADCs) with programmable gain amplifier.

  • PCF8574: 8-bit I2C I/O expander widely used for driving character LCDs (like 16x2 / 20x4).

  • DS3231: High-precision Real-Time Clock (RTC) module with integrated crystal oscillator.

Post a Comment

Previous Post Next Post