Sub topic 12
Mixing 3.3 V and 5 V Devices
This chapter assumes you know how open-drain lines and pull-up resistors work on an I2C bus (SDA, SCL), and that you can use the Wire library to scan a bus. Here you will learn why mixed voltages cause trouble, when you can ignore the problem, and how to fix it when you can't.
Why voltage levels matter on I2C
Many Arduino boards run at 5 V (Uno, Nano, Mega). Many sensors run at 3.3 V, and many modern boards do too (Due, Zero, ESP32, Nano 33 IoT, Raspberry Pi Pico). Modules from hobby suppliers often mix the two on one breadboard.
An I2C line is open-drain. No device drives the line high. Each device only pulls it low, and the pull-up resistor takes it high. This gives a useful property: the high voltage on the bus is set by whatever the pull-up resistors are connected to, not by the devices.
That leads to two separate questions:
- What voltage are the pull-ups tied to?
- Can every device tolerate that voltage on its pins, and does it read it correctly as logic high or low?
Both must have a good answer for the bus to work.
Input thresholds and absolute maximum ratings
Each chip datasheet gives two sets of numbers you need.
- VIH and VIL are the input thresholds. A voltage above VIH counts as high. A voltage below VIL counts as low. For I2C, the specification sets VIH at 0.7 × VDD and VIL at 0.3 × VDD, where VDD is the supply of that chip.
- Absolute maximum ratings give the highest voltage a pin can see without damage. A common rule is VDD + 0.3 V. A 3.3 V chip with this rating is damaged by anything over about 3.6 V on its pins.
Take a 3.3 V sensor on a bus pulled up to 5 V. Its V_IH is 0.7 × 3.3 V = 2.31 V, so the 5 V high level is read correctly. But 5 V on the pins exceeds the 3.6 V limit, so the sensor can be damaged immediately or fail slowly.
Now take the reverse: a 5 V Arduino on a bus pulled up to 3.3 V. The pins are safe. An ATmega328P at 5 V needs about 0.7 × 5 V = 3.5 V for a reliable high, and the bus only reaches 3.3 V. The Arduino may read highs unreliably. This failure is intermittent and harder to spot.
Which setups need a level shifter
The following table covers common cases.
| Setup | Safe without a shifter? | Reason |
|---|---|---|
| All devices at 3.3 V, pull-ups to 3.3 V | Yes | Everything matches |
| All devices at 5 V, pull-ups to 5 V | Yes | Everything matches |
| 3.3 V controller, devices rated for 5 V, pull-ups to 3.3 V | Usually | 3.3 V is above 0.7 × 5 V = 3.5 V only marginally, so check each datasheet |
| 5 V controller, 3.3 V device, pull-ups to 5 V | No | Device pins exceed their maximum rating |
| 5 V controller, 3.3 V device that is 5 V tolerant, pull-ups to 5 V | Yes | Datasheet permits 5 V on I/O pins |
| 5 V controller, 3.3 V device, mixed pull-ups | No | Needs a level shifter |
Check the datasheet for "5 V tolerant" wording. Some chips are 5 V tolerant on the I2C pins even when powered at 3.3 V. Others are not. Never assume.
How a bidirectional level shifter works
I2C data moves in both directions on the same wire, so a normal one-way shifter will not work. You need a bidirectional shifter. The usual design uses one N-channel MOSFET per line, such as the BSS138, with a pull-up resistor on each side.
The MOSFET gate connects to the low-voltage supply (3.3 V). The source connects to the low-voltage line and the drain to the high-voltage line. Three cases cover its behavior:
- Both sides idle high. The gate and source are both at 3.3 V, so the MOSFET is off. Each side sits at its own pull-up voltage.
- The 3.3 V side pulls low. The source drops, the gate-to-source voltage rises, and the MOSFET turns on. It drags the 5 V side low too.
- The 5 V side pulls low. Current flows through the MOSFET's body diode, which pulls the source low. The MOSFET then turns on and holds the low side low.
Because this works in both directions, one circuit handles SDA and a second identical one handles SCL. Ready-made four-channel BSS138 breakout boards are cheap and are the usual choice.
Wire the shifter like this:
- Connect the LV pin to the 3.3 V supply and the HV pin to the 5 V supply.
- Connect the grounds of both sides together. The shifter needs a common ground.
- Connect SDA and SCL from the 5 V controller to the HV side.
- Connect the 3.3 V devices to the LV side.
Checking that the shifter works
Run the bus scanner on the 5 V side. A working shifter shows every 3.3 V device at its expected address. This sketch is the same scanner from earlier, and it works unchanged through a shifter:
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("Device found at 0x");
if (address < 16) Serial.print('0');
Serial.println(address, HEX);
}
}
Serial.println("Scan done");
delay(5000);
}If the scan finds nothing, test each side separately before suspecting the sketch. Use a multimeter to confirm that the idle voltage on SDA and SCL is 3.3 V on the low side and 5 V on the high side.
Alternatives to a MOSFET shifter
The BSS138 design is simple and works well at 100 kHz and 400 kHz. It has limits, so you may want something else in some cases.
- Dedicated I2C level translators. Chips such as the PCA9306 or TXS0102 do the same job in one package, with better-defined timing. The TXS0108-type auto-direction chips are meant for push-pull signals and often misbehave on open-drain I2C, so do not use them here.
- Powering the whole bus at one voltage. Often the simplest fix is to buy or select a 3.3 V version of the 5 V device, or the reverse, so that no shifting is needed.
- I2C buffers and isolators. Chips such as the PCA9517 translate levels and also buffer the bus capacitance, which helps on longer wires.
- Using a 3.3 V controller. If most of your devices are 3.3 V, a 3.3 V board avoids the problem completely.
Common mistakes
- Pull-ups to the wrong rail. A 5 V pull-up on a 3.3 V device damages it. Look at where the pull-up resistors on every breakout board connect.
- No common ground. Without a shared ground, the voltages mean nothing and the bus behaves randomly.
- Powering the low side but not the high side. The shifter needs both supplies. If one is missing, that side floats and the scanner finds nothing.
- Using a one-way shifter or a resistor divider on SDA. A divider can lower a signal going one way but cannot raise a signal coming back, so the line breaks in one direction.
- Assuming a "5 V tolerant" chip is tolerant when unpowered. Some chips lose that protection when their own supply is off. Check the datasheet if the bus is live while a device is powered down.
Exercise
A 5 V Arduino Uno shares an I2C bus with a 3.3 V sensor that is not 5 V tolerant. The sensor breakout has 10 kΩ pull-ups tied to its 3.3 V supply. You add a BSS138 shifter. Where do you connect the Uno's SDA, the sensor's SDA, the shifter's LV pin, and the shifter's HV pin? Why must the grounds be joined?
Show answer
Connect the Uno's SDA to the shifter's HV-side SDA channel and the sensor's SDA to the LV-side SDA channel. Connect the LV pin to the 3.3 V supply and the HV pin to the 5 V supply. The grounds must be joined because every voltage on the bus is measured relative to ground. Without a common ground, the devices have no shared reference and cannot agree on what is high or low. The sensor's own 3.3 V pull-ups are fine on the low side, and the 5 V side needs its own pull-ups, which most shifter boards provide.
Exercise
A 3.3 V Raspberry Pi Pico reads an I2C sensor powered at 5 V that is pulled up to 3.3 V. The scanner finds the device sometimes but not always. Give one likely cause based on input thresholds.
Show answer
The 5 V sensor expects a high of at least 0.7 × 5 V = 3.5 V. The bus only reaches 3.3 V, which is below that threshold, so the sensor reads highs unreliably. A level shifter or a sensor version that runs at 3.3 V fixes this.