Level shifters and the 74AHCT125

The ESP32 uses 3.3 V logic. Many LED strips, servos, sensors and older Arduino boards use 5 V logic. A level shifter changes a signal from one voltage to the other. This page tells you when you need one, how to connect the 74AHCT125, and which other types to use for I²C and for 5 V signals into the ESP32.

When you need a level shifter

Up: 3.3 V out of the ESP32 into a 5 V device

A 5 V device reads a signal as HIGH only above its input-high threshold (VIH). For many 5 V CMOS chips, VIH is 0.7 × 5 V = 3.5 V. The ESP32 gives only 3.3 V, so the signal is less than the specification.

It frequently works on the bench, but it can fail with long wires, electrical noise or a warm chip. Typical examples: WS2812B LED strips, some servos, and 5 V shift registers and displays.

Down: 5 V out of a device into the ESP32

ESP32 pins are not 5 V tolerant. The maximum input voltage is approximately 3.6 V. A 5 V signal can damage the pin or the chip, immediately or after some time.

Typical examples: the HC-SR04 echo pin, the TX pin of a 5 V Arduino, and 5 V sensor modules with a digital output.

Check the module first. Many breakout boards have a regulator and level shifters on the board. If a module has a VIN pin and accepts 3.3 V to 5 V, supply it with 3.3 V. Its signals are then 3.3 V and you need no level shifter.

The 74AHCT125 quad buffer

The 74AHCT125 has four one-way buffers. Supply it with 5 V. Each buffer reads a 3.3 V signal on its input (A) and gives a strong 5 V signal on its output (Y). It is fast, low cost and easy to use, so it is the best choice for LED strips and other fast signals.

74AHCT125 11OE̅ 21A (in) 31Y (out) 42OE̅ 52A (in) 62Y (out) 7GND 14VCC (5 V) 134OE̅ 124A (in) 114Y (out) 103OE̅ 93A (in) 83Y (out)
Pinout of the 14-pin DIP package, seen from the top. The notch shows the pin 1 end. OE̅ (output enable) is active LOW: connect it to GND to turn the buffer on.

Key specifications (5 V supply)

Supply voltage (VCC)4.5 V to 5.5 V
Input HIGH (VIH)2.0 V minimum, so 3.3 V is a clear HIGH
Input LOW (VIL)0.8 V maximum
Output HIGHAlmost 5 V
Output current±8 mA per output
DelayApproximately 5 ns, so it can send signals of many MHz
DirectionOne way only: A to Y

The T in AHCT means TTL-compatible inputs. These inputs read 2.0 V and more as HIGH. A 74AHC125 (no T) with a 5 V supply needs 3.85 V for HIGH, so it does not work with 3.3 V input signals. The 74HCT125 also works, but it is slower.

Connect the 74AHCT125

Connections for one channel

PinConnect to
14 VCC5 V. Put a 100 nF capacitor between pin 14 and GND, near the chip
7 GNDGND of the ESP32 and GND of the 5 V device (common ground)
1 1OE̅GND (buffer on)
2 1AESP32 GPIO pin (3.3 V signal)
3 1Y5 V device input. For an LED strip, add a 330 Ω resistor in series
Unused channelsConnect each unused A input and OE̅ pin to GND. Do not leave inputs open

Rules

  • Supply the chip with 5 V only. With a 3.3 V supply, the outputs give 3.3 V and the chip is outside its specification.
  • Do not connect a Y output to an ESP32 pin. The outputs are 5 V. The 74AHCT125 changes signals up, not down.
  • Connect all grounds together. A signal voltage has a meaning only when both sides use the same ground.
  • Put the chip near the ESP32. Then the long wire carries the strong 5 V signal.
  • Use the OE̅ pins to switch a channel off. With OE̅ HIGH, the output is disconnected (high impedance). You can use this to share one line between two devices.

Typical uses on this site: addressable LEDs (WS2812B data and APA102 clock and data), servos that do not respond reliably to 3.3 V, and 5 V shift registers.

Other ways to shift levels

MethodDirectionSpeedUse it forDo not use it for
74AHCT125 or 74HCT245 (8 channels) with a 5 V supply3.3 V to 5 VVery fastLED strips, servos, SPI to 5 V devices, any push-pull outputSignals into the ESP32, I²C
Voltage divider (for example 1 kΩ and 2 kΩ)5 V to 3.3 VMedium. Slow with large resistorsHC-SR04 echo, 5 V serial TX into the ESP32 RX, 5 V digital sensor outputsSignals out of the ESP32, I²C
74LVC125A with a 3.3 V supply5 V to 3.3 VVery fastFast 5 V signals into the ESP32. Its inputs accept up to 5.5 VSignals out of the ESP32
BSS138 MOSFET module (pins LV, HV, GND)Both directionsSlow (approximately 400 kHz maximum)I²C between 3.3 V and 5 V devices, slow open-drain signalsLED strips and other fast signals
TXS0108E module (8 channels)Both directions, automaticFast, but weak outputFast bidirectional push-pull signals with light loadsLines with pull-up resistors or heavy loads. It can oscillate

Calculate a voltage divider

Vout = Vin × R2 / (R1 + R2). Connect R1 from the 5 V signal to the ESP32 pin, and R2 from the ESP32 pin to GND.

With R1 = 1 kΩ and R2 = 2 kΩ: 5 V × 2 / 3 = 3.33 V. With R1 = 10 kΩ and R2 = 20 kΩ, the divider uses less current but is slower.

Refer to the ultrasonic distance page for an example with the HC-SR04 echo pin.

I²C between 3.3 V and 5 V

I²C uses open-drain lines with pull-up resistors, and both devices send data on SDA. A one-way buffer such as the 74AHCT125 cannot do this. Use a BSS138 module or a PCA9306 chip. Connect LV to 3.3 V, HV to 5 V and GND to GND.

Before you add a shifter, look at the module. Many 5 V I²C modules have their pull-up resistors to the VIN pin. Supply them with 3.3 V if they accept it.

Resources