Sub topic 12
Mixing 3.3 V and 5 V Devices on an I2C Bus
Prerequisites and voltage rules
This chapter assumes that you understand Arduino setup() and loop(), basic resistors, and the I2C roles of controller and target. The controller starts transactions. A target responds at its address.
I2C signals are unusual because SDA and SCL are open-drain lines. Devices pull a line down to 0 V, but they do not actively drive it high. Pull-up resistors create the high level. Therefore, the pull-up voltage determines the bus high voltage.
A 5 V Arduino and a 3.3 V sensor can share an I2C bus only when:
- The sensor inputs tolerate the bus high voltage.
- The Arduino reliably recognizes the sensor's high level.
- The pull-ups are connected to a suitable voltage.
- Both devices use compatible ground references.
A 3.3 V sensor must not receive 5 V on SDA or SCL unless its datasheet explicitly allows it.
Choosing a connection
The safest general solution is a bidirectional I2C level shifter. A common module uses one MOSFET per signal line. The low-voltage side connects to 3.3 V, and the high-voltage side connects to 5 V. Each side has its own pull-ups.
A direct connection can work when the bus pull-ups are connected to 3.3 V and the 5 V controller's input thresholds accept 3.3 V as high. Do not assume this from the nominal voltage alone. Check the controller datasheet. For an ATmega328P running at 5 V, the guaranteed high-input threshold may be higher than 3.3 V, so direct operation is not guaranteed.
Some breakout boards include pull-ups. If several boards are connected, their pull-ups work in parallel. The combined resistance can become too low, causing excessive current when a line is low. Remove redundant pull-ups when the board provides a jumper or resistor option.
>callout warning > Never solve a voltage mismatch by connecting a 5 V pull-up to a 3.3 V-only device. Use a level shifter, or verify that the device has 5 V-tolerant I2C pins.
Pull-ups, speed, and testing
The pull-up resistance affects how quickly a line rises. Lower resistance gives faster rising edges but increases low-level current. Higher resistance reduces current but may make the bus too slow, especially with long wires or many devices.
Start with the pull-ups recommended by the device boards or datasheets. Use 100 kHz while testing. Increase the speed only after the bus works reliably.
The Arduino code does not change just because a level shifter is present:
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
Wire.beginTransmission(0x68);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.println("Device responded");
} else {
Serial.println("Check wiring, address, and voltage levels");
}
delay(1000);
}Check SDA, SCL, and ground continuity. Then confirm that each side of the level shifter has the correct supply voltage. A scanner finding no devices often indicates reversed lines, missing pull-ups, or a voltage problem rather than a software error.
Exercise
A 5 V Arduino is connected to a 3.3 V sensor breakout. The breakout has 4.7 kΩ pull-ups to 3.3 V. Is a level shifter automatically required?
Show answer
No. The sensor is protected from 5 V because the bus rises only to 3.3 V. However, confirm that the Arduino reliably recognizes 3.3 V as high. If it does not, add a bidirectional level shifter.