3.3 V and 5 V: level shifting
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.
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
Read the lesson step by step
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 RXWhat 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 eitherToo 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); }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) # True3.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 smallerToo 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 spareFix 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 VThe 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 -> HIGHThe 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 groundIt 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)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01An ESP32 runs on 3.3 V. Its datasheet says HIGH from 0.75 × VDD and at most VDD + 0.3 V. Which input voltage is both safe and read as HIGH?
0.75 × 3.3 = 2.48 V (2.475 V exactly) and 3.3 + 0.3 = 3.6 V. Only 3.0 V lies between them. 1.5 V is in the unsure zone, and 4.0 V and 5.0 V are above the maximum.
02A divider has 1 kΩ on top and 2 kΩ to ground, with 5 V at its input. What is the voltage at the middle?
Vout = Vin × Rbot / (Rtop + Rbot) = 5 × 2000 / 3000 = 3.33 V, above the ESP32's 2.48 V HIGH threshold and below its 3.6 V limit.
03Why can't the same voltage divider carry a signal from the ESP32 up to the Uno?
A divider shares its input voltage between two resistors, so its output is always a fraction of the input. To raise 3.3 V to 5 V you need a part powered from 5 V, such as the 5 V pull-up of a MOSFET shifter.
04In the MOSFET shifter the gate sits at 3.3 V. The ESP32 drives its side to 3.3 V. What happens?
Gate and source are both at 3.3 V, so VGS is 0 V, below the BSS138's 0.8 V minimum threshold. The MOSFET is off, which leaves the Uno side to its 10 kΩ pull-up: 5 V, a clear HIGH.
05Why do I2C level-shifter boards use the MOSFET circuit rather than a divider?
On I2C both ends pull the line low at different times. When either side pulls low, the MOSFET (helped by its built-in diode when the 5 V side pulls) turns on and both sides go low together, so one part handles both directions. NXP's application note AN10441 describes the three cases.
Glossary
- Logic level
- The voltage that stands for 1 (HIGH) or 0 (LOW) on a pin. It depends on the chip's supply voltage: about 5 V or 3.3 V for HIGH, about 0 V for LOW.
- VIH and VIL
- From a datasheet: the lowest input voltage guaranteed to read HIGH (VIH) and the highest guaranteed to read LOW (VIL). Between them the reading is not promised.
- Absolute maximum
- The voltage a pin must never see. Above it the chip can be damaged, even if it seems to work.
- Level shifter
- A small circuit that lets two chips with different logic voltages talk, by turning one side's HIGH into the other side's HIGH.
- Voltage divider
- Two resistors in series across a voltage. The point between them sits at a fixed fraction of it: Vin × Rbot / (Rtop + Rbot).
- Pull-up resistor
- A resistor from a line to a supply voltage, so the line reads HIGH unless something pulls it LOW.
- MOSFET
- A transistor switched by the voltage between its gate and source (VGS). An N-channel one turns on when VGS rises above its threshold.
- 5 V tolerant
- A 3.3 V pin that the datasheet says can safely take 5 V. ESP32 pins are not: their limit is 3.6 V.
Notes and sources
- Thresholds are datasheet guarantees, not exact switch points. ATmega328P (Table 30-1): LOW up to 0.3 × VCC, HIGH from 0.6 × VCC, for VCC from 2.4 V to 5.5 V. ESP32 (Table 5-3, at 3.3 V and 25 °C): LOW up to 0.25 × VDD, HIGH from 0.75 × VDD, and at most VDD + 0.3 V. The datasheets give 2.48 V as 2.475 V and 0.82 V as 0.825 V; the figure rounds them to the safe side.
- The divider holds even for the Uno's worst-case HIGH: the datasheet promises at least 4.2 V (at 20 mA), and 4.2 × 2 ÷ 3 = 2.8 V, still above 2.48 V. It costs 1.67 mA while the line is HIGH. The Circuits lesson "Voltage and current dividers" derives the formula.
- The BSS138's gate threshold is 0.8 V to 1.5 V (typically 1.3 V), so with its gate on 3.3 V it is surely off at 0 V and surely on at 3.3 V. The three cases are NXP's application note AN10441 on I2C level shifting. Ready-made boards put four of these, each with two 10 kΩ pull-ups, on one breakout.
- Pull-ups make rising edges slower than falling ones, so a MOSFET shifter suits slow and medium buses such as UART, I2C and slow SPI. AN10441 describes it for I2C Standard and Fast mode (up to 400 kbit/s).
- Microchip ATmega328P datasheet (DS40002061B), Table 30-1 Common DC characteristics
- Arduino UNO R3 product datasheet (A000066): IOREF connected to 5 V
- Espressif ESP32 Series datasheet, Tables 5-1 and 5-3 (absolute maximum ratings, DC characteristics)
- NXP AN10441, Level shifting techniques in I2C-bus design (Rev. 01, 2007)
- onsemi BSS138 N-channel MOSFET datasheet
- Adafruit 4-channel I2C-safe bi-directional logic level converter (BSS138, 10K pull-ups)
- SparkFun, Logic Levels (VIH, VIL and a 1 kΩ / 2 kΩ divider)
- SparkFun, Bi-Directional Logic Level Converter Hookup Guide
Quiz Unlocks after the last step.