ENع
←Level shifting
Lesson 12 · Digital pins: inputs and outputs

3.3 V and 5 V: level shifting

12 min10 steps5 questionsBeginner
About this lesson

Many parts now run on 3.3 V, but the Arduino Uno runs on 5 V. Wire an ESP32 to an Uno and you meet two problems: the Uno's 5 V HIGH is more than the ESP32's pins can take (at most 3.6 V), and the ESP32's HIGH is only just enough for the Uno, which wants 3 V. Fix the first with a 1 kΩ and 2 kΩ voltage divider, and the second with a BSS138 MOSFET and two pull-ups, which also works in both directions. The lesson "Digital input and logic levels" introduces thresholds, the Circuits lesson "Voltage and current dividers" derives the divider formula, and the Hardware lesson "Transistors: the switch that changed everything" explains the MOSFET; here we use them to join two boards safely.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 12 · Interactive
01 / 10

Two boards, two voltages

An Arduino Uno runs on 5 volts, so its HIGH is about 5 V. An ESP32 runs on 3.3 volts, so its HIGH is about 3.3 V. We want to wire a pin of one to a pin of the other, both ways.

Uno R3   ATmega328P, supply 5 V   HIGH ≈ 5 V
ESP32    supply 3.3 V              HIGH ≈ 3.3 V
LOW is about 0 V on both

line 1  Uno TX   -> ESP32 RX
line 2  ESP32 TX -> Uno RX
Loading figure…
or ← →
Read the lesson step by step
  1. Two boards, two voltages

    An Arduino Uno runs on 5 volts, so its HIGH is about 5 V. An ESP32 runs on 3.3 volts, so its HIGH is about 3.3 V. We want to wire a pin of one to a pin of the other, both ways.

    Uno R3   ATmega328P, supply 5 V   HIGH ≈ 5 V
    ESP32    supply 3.3 V              HIGH ≈ 3.3 V
    LOW is about 0 V on both
    
    line 1  Uno TX   -> ESP32 RX
    line 2  ESP32 TX -> Uno RX
  2. What counts as HIGH

    An input reads HIGH above one voltage and LOW below another. The Uno needs 3 V for HIGH, 1.5 V or less for LOW. The ESP32 needs 2.48 V and 0.82 V. In between, nothing is promised.

               LOW up to   HIGH from   never above
    Uno  5 V   1.5 V      3 V       5.5 V
    ESP32 3.3  0.82 V     2.48 V      3.6 V
    
    Uno:   0.3 × 5 V and 0.6 × 5 V   (VIL, VIH)
    ESP32: 0.25 × 3.3 V and 0.75 × 3.3 V
    between LOW and HIGH: the pin may read either
  3. Too high for the ESP32

    Wire the Uno's TX straight to the ESP32's RX and a HIGH puts 5 V on it. The ESP32 datasheet allows at most 3.6 V on a pin. That is 1.4 V too much, and it can damage the chip.

    direct wire   Uno TX -> ESP32 RX
    Uno HIGH      ≈ 5 V
    ESP32 limit   VDD + 0.3 = 3.6 V
    5 V > 3.6 V  ->  damage
    
    // Arduino Uno R3 (5 V): TX is pin 1
    void setup() { Serial.begin(9600); }
    void loop() {
      Serial.println("hi");   // 5 V HIGHs on TX
      delay(1000);
    }
  4. Fix 1: a voltage divider

    Put two resistors in the way: 1 kΩ in series, then 2 kΩ down to ground. They share the 5 V in proportion, so the middle sits at 5 × 2 ÷ 3 = 3.33 V.

    Vout = Vin × Rbot / (Rtop + Rbot)
         = 5 × 2000 / (1000 + 2000)
         = 3.33 V
    current while HIGH: 5 V / 3000 Ω = 1.67 mA
    
    # divider: Uno 5 V HIGH -> ESP32 input
    vin, r_top, r_bot = 5.0, 1000, 2000
    vout = vin * r_bot / (r_top + r_bot)
    print(f"{vout:.2f} V")   # 3.33 V
    
    # ESP32 input: HIGH from 0.75*3.3 V, at most 3.3+0.3 V
    print(0.75 * 3.3 <= vout <= 3.3 + 0.3)  # True
  5. 3.33 V: HIGH and safe

    3.33 V is above the ESP32's 2.48 V, so it reads HIGH, and below 3.6 V, so the pin is safe. A LOW of 0 V stays 0 V. A divider only steps down, so line 2 needs something else.

    ESP32 RX sees 3.33 V
    2.48 V <= 3.33 V <= 3.6 V  ->  HIGH, safe
    Uno LOW 0 V  ->  0 V  ->  LOW
    a divider only makes voltages smaller
  6. Too low for the Uno?

    Now the ESP32's TX drives the Uno's RX. 3.3 V cannot harm a 5 V pin. But the Uno needs 3 V for HIGH, and the ESP32 only promises 2.64 V. Often it works; it is not guaranteed.

    direct wire   ESP32 TX -> Uno RX
    ESP32 HIGH    at least 0.8 × 3.3 = 2.64 V (often more)
    Uno HIGH      needs 3 V
    2.64 V  ->  unsure
    3.3 V  ->  HIGH, only 0.3 V to spare
  7. Fix 2: a MOSFET shifter

    Use one transistor, a BSS138 MOSFET. Its gate is tied to 3.3 V. Its source side has a 10 kΩ pull-up to 3.3 V, and its drain side a 10 kΩ pull-up to 5 V. Each side keeps its own voltage.

    MOSFET   BSS138, N-channel
    gate     3.3 V (the lower supply)
    source   ESP32 side, 10 kΩ pull-up to 3.3 V
    drain    Uno side,   10 kΩ pull-up to 5 V
    turns on when gate - source > 0.8..1.5 V
  8. The ESP32 sends HIGH

    The ESP32 lets its side sit at 3.3 V. Gate and source are both at 3.3 V, a difference of 0 V, so the MOSFET stays off. The 10 kΩ pull-up lifts the Uno's side to a full 5 V: a clear HIGH.

    source 3.3 V, gate 3.3 V  ->  VGS = 0 V
    0 V < 0.8 V  ->  MOSFET off
    pull-up  ->  Uno side 5 V  ->  HIGH
  9. The ESP32 sends LOW

    Now the ESP32 pulls its side to 0 V. The gate is still at 3.3 V, so the difference is 3.3 V, well past the 1.5 V the MOSFET needs. It switches on and drags the Uno's side down to 0 V: LOW.

    source 0 V, gate 3.3 V  ->  VGS = 3.3 V
    3.3 V > 1.5 V (max threshold)  ->  MOSFET on
    Uno side  ->  0 V  ->  LOW
    the 5 V pull-up now passes 0.5 mA to ground
  10. It works both ways

    On a two-way bus such as I2C, either end may pull the line low. If the 5 V side pulls low, a diode built into the MOSFET drags the 3.3 V side down until the transistor turns on. One part shifts both ways.

    two-way bus (I2C): the 5 V device pulls low
    5 V side pulled to 0 V
    body diode  ->  source side falls
    VGS passes 1.5 V  ->  MOSFET on  ->  ESP32 side 0 V
    ESP32 side never sees more than 3.3 V
    (our Uno RX only listens; an I2C pin can also pull low)

Quiz Unlocks after the last step.