What is I2C? Connecting multiple devices with just 2 wires
Atlasbot · 16/8/2026 · 11 min read
UART can only talk between exactly two devices. But a real robot usually has a lot of sensors — IMUs, proximity sensors, OLED displays — and the MCU only has a limited number of GPIO pins. I2C solves this problem correctly: many devices share the same 2-wire bus, separated by address.
Key concepts
I2C (Inter-Integrated Circuit) uses exactly 2 signal wires for the entire bus, regardless of how many devices are connected:
- SDA (Serial Data) — data line, common for both sending and receiving
- SCL (Serial Clock) — clock wire, generated by device master (usually MCU)
Unlike UART (asynchronous, no clock), I2C is a synchronous communication — with its own clock wire, so the two sides do not need to agree on the speed in advance, avoiding the error of baud rate drift.
Address — key for multiple devices to share 2 wires
Each device on the I2C bus has a fixed 7-bit address (some chips allow a few low bits to be swapped with hardware pins to avoid duplicate addresses). When the MCU (master) wants to talk to a specific sensor, it sends that correct address onto the bus first — only the device with the matching address "replies" (ACK), other devices silently ignore it.
In short: I2C trades speed (slower than SPI) for the ability to multiple devices over the same 2 wires — ideal when you need to read a lot of low-cost sensors but don't want to waste a lot of GPIO pins.
How it works
Master (MCU)
SDA ──┬─────────┬─────────┬───── (needs pull-up resistor)
SCL ──┼─────────┼─────────┼───── (needs pull-up resistor)
│ │ │
Sensor Sensor Screen
IMU is about OLED
(0x68) way (0x3C)
(0x29)
A typical I2C transaction: master broadcasts START condition, sends 7-bit address + 1 read/write bit, device matches response address ACK, then data is transmitted byte by byte (each byte also has its own ACK), ending with STOP condition.
Example of reading data from an IMU using HAL on STM32:
uint8_t data[6];
HAL_I2C_Mem_Read(&hi2c1, IMU_ADDR << 1, REG_ACCEL_X, 1, data, 6, HAL_MAX_DELAY);
Since SDA/SCL is open-drain (the device can only pull the wire low, not push it high itself), the I2C bus always needs external pull-up resistor (usually 4.7kΩ) to get the wire back high when no device is holding it low — missing pull-up resistor is the most common cause of failure. I2C "doesn't work" even though the code is completely correct.
Technical details
Formulas
7-bit I2C addresses allow a maximum of 2⁷ = 128 addresses, but the I2C standard reserves some addresses for special purposes (broadcast, reserved):
Number of available addresses = 2^7 − number of reserved addresses
= 128 − 16 (start/end reserved space according to I2C specification) ≈ 112 usable physical addresses
→ Still enough for most boards with a few to a few dozen I2C sensors
Parameters
The I2C bus speed is divided into standardized "modes", and the pull-up resistor value needs to be chosen to match the speed:
| Mode | Maximum speed | Reference pull-up resistor |
|---|---|---|
| Standard mode | 100 kHz | 4.7 – 10kΩ |
| Fast mode | 400 kHz | 2.2 – 4.7kΩ |
| Fast mode+ | 1 MHz | 1 – 2.2kΩ |
Default I2C address of some popular sensors: IMU MPU6050 — 0x68; pressure sensor BMP280 — 0x76 or 0x77; OLED display SSD1306 — 0x3C.
Register / Bit / Byte
The first byte sent to the bus immediately after the START condition is not just an address — it consists of 7 address bits + 1 read/write bit: bits 7 to 1 are the device address, bit 0 (lowest) is 0 if the master wants to write to the device, 1 otherwise want to read from the device. This is the reason why in HAL code it is common to see the address shifted left by 1 bit (IMU_ADDR << 1) — the library itself adds the read/write bit to bit position 0 after shifting.
Protocol
I2C has a stricter variant called SMBus (System Management Bus) — which is basically compatible with I2C but adds timeout rules (the bus resets if it hangs too long) and more standard voltages — many power/battery management chips use SMBus instead of pure I2C, although still communicating via the same I2C hardware of the MCU.
Practical applications
When you need to install 2 identical sensors with the same default address on the same board (eg 2 ToF sensors measuring distance on both sides of the robot, both have default address 0x29), it is not possible to connect them both directly to the same I2C bus because of address conflicts — a common solution is to use IC multiplexer I2C (Example: TCA9548A) acts as a "channel selector switch", allowing the MCU to talk to each sensor in turn via its own channel even though both retain the same default address.
Practical example
// Arduino — scans all I2C addresses that the device is responding to
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("Dang quet I2C...");
for (uint8_t addr = 1; addr < 127; addr++) {
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
Serial.print("Tim thay thiet bi tai dia chi: 0x");
Serial.println(addr, HEX);
}
}
}
void loop() {}
Output:
Dang quet I2C...
Tim thay thiet bi tai dia chi: 0x3C
Tim thay thiet bi tai dia chi: 0x68
This code ("I2C scanner") is the first debugging tool that should be run when an I2C sensor is "unrecognized" — if no address appears in the output, it is almost certainly a missing pull-up resistor or wrong SDA/SCL wire, not an error in the code reading the sensor data.
Common mistakes
- Missing pull-up resistor on SDA/SCL — as mentioned in the Principle of Operation section, this is the most common cause of the I2C bus becoming completely unresponsive.
- Address conflict between 2 identical devices — as mentioned in the Practical Application section, need to use multiplexer or choose a sensor with hardware address change pin if available.
- Confusing SDA and SCL when wiring — both wires "look the same", reverse wiring causes the bus not to work even with a full pull-up — always compare the correct symbols on the module.
- Clock stretching is not properly supported by the MCU — some sensors "stretch" the clock when more internal processing time is needed (clock stretching) — if the MCU-side I2C library does not handle this mechanism properly, communication with that sensor may freeze or fail intermittently even though other sensors on the same bus still operate normally.
Conclusion
I2C correctly solves the problem of "many sensors, few GPIO pins" thanks to the addressing mechanism on the same 2 wires - in exchange for slower speed than SPI and requiring a clear understanding of pull-ups, addresses, and ACKs to effectively debug when there is a problem. The next article in this section is about SPI — a much faster communication standard, at the expense of needing an additional device select (CS) pin for each device on the bus.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.