I2C/The bus/Why a bus

Why a bus

A microcontroller runs out of pins long before you run out of modules to plug into it. I2C connects every one of them with the same two wires, and one more module costs no pins at all.

The simple way to connect a module is a GPIO for every signal it has. A 1602 LCD wired directly takes six: RS, E and four data lines. A 4×4 keypad takes eight, one for each row and each column. Four seven-segment digits driven directly take twelve.

An ESP32 has 34 GPIOs, and fewer than half of them are free without a catch. Six are wired to the flash chip, six can only read, five decide how the chip boots, and two are the serial port you watch in the monitor. That leaves about fifteen. The display and the keypad take fourteen, and the digits do not fit.

A bus

A bus is wires that every module shares, instead of wires each module owns. I2C is a bus with two:

  • SCL, the clock.
  • SDA, the data.

The ESP32 gives one GPIO to each, and every module connects to the same two. Six modules or twenty, it is still two GPIOs. That is why each of the modules above is also sold as an I2C module: the 1602 LCD's version is the same display with a small board soldered to its back, which turns its sixteen pins into four.

Below, the ESP32 is a station and the bus is a road with a lane for each wire. The cars go by once a second. A real bus at 100 kHz sends 100,000 bits a second, so the road is 100,000 times slower than the wire.

Four pins, two wires

An I2C module has four pins, not two. The other two are power, VCC and GND, which every module needs however it is wired. Only SCL and SDA are the bus.

Where it came from

Two engineers at Philips in Eindhoven, Ad Moelands and Herman Schutte, filed the patent in October 1980: a two-wire bus, one clock wire and one data wire, "for interconnecting a number of stations". Its first job was inside a television, where one processor had to reach every other chip in the set. Philips called it the Inter-IC bus, the bus between the chips.

What sharing costs

Every module on the bus sees everything sent along it. So the ESP32 has to say which module it means, and it does that with a number every I2C device has: its address. That is the next lesson.

Edit this page — content/fundamentals/i2c/why-a-bus.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Fundamentals on the forum →