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.
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.
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 HIGH | Almost 5 V |
| Output current | ±8 mA per output |
| Delay | Approximately 5 ns, so it can send signals of many MHz |
| Direction | One 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
| Pin | Connect to |
|---|---|
| 14 VCC | 5 V. Put a 100 nF capacitor between pin 14 and GND, near the chip |
| 7 GND | GND of the ESP32 and GND of the 5 V device (common ground) |
| 1 1OE̅ | GND (buffer on) |
| 2 1A | ESP32 GPIO pin (3.3 V signal) |
| 3 1Y | 5 V device input. For an LED strip, add a 330 Ω resistor in series |
| Unused channels | Connect 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
| Method | Direction | Speed | Use it for | Do not use it for |
|---|---|---|---|---|
| 74AHCT125 or 74HCT245 (8 channels) with a 5 V supply | 3.3 V to 5 V | Very fast | LED strips, servos, SPI to 5 V devices, any push-pull output | Signals into the ESP32, I²C |
| Voltage divider (for example 1 kΩ and 2 kΩ) | 5 V to 3.3 V | Medium. Slow with large resistors | HC-SR04 echo, 5 V serial TX into the ESP32 RX, 5 V digital sensor outputs | Signals out of the ESP32, I²C |
| 74LVC125A with a 3.3 V supply | 5 V to 3.3 V | Very fast | Fast 5 V signals into the ESP32. Its inputs accept up to 5.5 V | Signals out of the ESP32 |
| BSS138 MOSFET module (pins LV, HV, GND) | Both directions | Slow (approximately 400 kHz maximum) | I²C between 3.3 V and 5 V devices, slow open-drain signals | LED strips and other fast signals |
| TXS0108E module (8 channels) | Both directions, automatic | Fast, but weak output | Fast bidirectional push-pull signals with light loads | Lines 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
- Texas Instruments: SN74AHCT125 datasheetPinout, thresholds and timing.
- Adafruit: 74AHCT125 quad level shifterThe DIP chip that many NeoPixel guides use.
- SparkFun: bi-directional logic level converterHow the BSS138 converter operates, with I²C and serial examples.
- TI: A guide to voltage translation with TXS-type translatorsWhy the TXS0108E has problems with pull-up resistors and heavy loads.
- ESP32 GPIO · Addressable LEDs · Other microcontrollersRelated pages on this site.