Questions tagged [i2c]

I²C (Inter-Integrated Circuit) is a multi-drop serial bus invented by Philips (now NXP) for communication between ICs on the same PCB, i.e. short distance.

I2C uses a 2-wire bus to communicate between a bus master and 1 or more slave devices. The master is a microcontroller, the slave devices are often microcontroller peripherals, but can also be an other microcontroller. Typical slave devices include:

  • EEPROMs
  • RTCs (Real Time Clocks)
  • Temperature sensors
  • GPIO expanders

Protocol
Communication goes over two wires: SDA (data) and SCL (clock). The lines are pull-up to high level by resistors, and the devices can make them low level with an open-drain NFET. Communication is initiated by a master which sends a start condition:

enter image description here

Start and stop conditions are easily detectable; they're the only situations where SDA can change while SCL is high. All other SDA changes should take place while SCL is low.

The master then sends the slave's 7-bit address followed by a R/W bit. After 8 bits have been received the slave replies with an ACK by pulling SDA low, which is read by the master at the 9th clock pulse. Successive bytes may be written by the master or the slave as the master clock SCL. Each byte is to be acknowledged by the slave. The communication ends with the stop condition: SDA going high while SCL is high.

enter image description here

Addressing
An I2C address is 7 bits long. Many devices have the highest bits of their address fixed, while the lowest bits can be selected using 1 or more inputs on the IC. The LM75B for instance has the address range 1001xxx2, where the 3 least significant bits can be set via 3 inputs on the device. This allows for up to 8 LM75Bs sharing the same I2C bus.

Further reading

I2C Specification at NXP
I2C Manual at NXP
I2C Interface Solutions, NXP presentation
I2C info at www.i2c-bus.org

I2C pull-up resistors

EDN article Design calculations for robust I2C communications
Is there a correct resistance value for I2C pull-up resistors?
TI application note I2C Bus Pullup Resistor Calculation

1818 questions
121
votes
9 answers

Tradeoffs when considering SPI or I2C?

What tradeoffs should I consider when deciding to use an SPI or I2C interface? This accelerometer/gyro breakout board is available in two models, one for each interface. Would either one be easier to integrate into an Arduino…
Mark Harrison
  • 10,419
  • 27
  • 68
  • 95
94
votes
11 answers

Is there a correct resistance value for I2C pull-up resistors?

The datasheet of the 24LC256 EEPROM states that: The SDA bus requires a pull-up resistor to VCC (typical 10 kΩ for 100 kHz, 2 kΩ for 400 kHz and 1 MHz). I thought that any resistor with a kΩ value would do the job (and it seems that my EEPROM…
JonathanD
  • 1,422
  • 1
  • 11
  • 12
55
votes
6 answers

Maximum I2C Bus Length?

What is the maximum length of cable that could be used to connect two I2C devices (I2C master-> I2C slave)? Yes, I know that I2C is really designed for intra-board communication. I have been tasked with a "design goal" of using a common I2C bus for…
Nate
  • 868
  • 1
  • 10
  • 13
45
votes
8 answers

How to resolve I2C address clashes?

I want to connect multiple I2C slave devices to a micro controller all on the same set of pins but the I2C devices all share the same address. The addresses are fixed in the hardware. Is there any way to connect multiple devices with the same…
Simon P Stevens
  • 636
  • 1
  • 5
  • 10
43
votes
6 answers

How to connect multiple i2c-interface devices into a single pin A4 (SDA) and A5 (SCL) on Arduino?

I want to add a real time clock module into my little project. I want to display both time and date on my existing i2c 2x16 LCD module. Both i2c-interface real time clock module and 2x16 LCD module use the same pin A4 (SDA) and A5 (SCL) on Arduino…
banksia
  • 431
  • 1
  • 4
  • 3
41
votes
10 answers

SPI or I2C: which to use for a longish bus

I'm contemplating a project that would require several AVRs talking to each other over a bus. They'd be separated by as much as 6 feet. It seems like both I2C and SPI can let a series of micros communicate over a bus, but I haven't seen anything…
edebill
  • 2,509
  • 2
  • 20
  • 26
41
votes
7 answers

What happens if I omit the pullup resistors on I2C lines?

Just now I realized that the I2C data and clock lines (SDA and SCL) must have pullup resistors. Well, I've built a couple of clocks using the DS1307 RTC (see datasheet) according to the schematic below. Notice that I have omitted both pullup…
Ricardo
  • 6,134
  • 19
  • 52
  • 85
37
votes
4 answers

I2C 3.3 to 5.0 V conversion

I'm trying to interface a 3.3V I2C device to a 5V Arduino. I don't have any problem with SCL line. I can use a simple voltage divider but I have an issue with the SDA line as this is bi-directional. I'm not at all sure how to handle this. I have…
JohnC
  • 4,779
  • 3
  • 25
  • 20
37
votes
3 answers

What is the difference between I2C and TWI?

What is the difference between the I2C and TWI interfaces? Wikipedia says that TWI is variant of I2C. Some other sources says that only name is different.
user54579
35
votes
1 answer

Is MCP23017's I2C address matching broken?

I'm struggling to get an I2C master to communicate reliably with a slave, because there's also an MCP23017 port-expander on the same bus. The MCP23017 seems to detect - and ACK! - its address, even when that bit pattern appears coincidentally in the…
SusanW
  • 987
  • 7
  • 26
29
votes
8 answers

Can I use I2C over 2m cables?

In my current project, I need to communicate between a microcontroller and some sensors over I2C. One of them is a temperature sensor, it needs to be placed at approximatively 2 meters far from the microcontroller. I cannot choose another protocol…
JackOfHearts
  • 649
  • 1
  • 6
  • 14
23
votes
7 answers

Why I2C designed to work with pull-up resistors and not pull-down ones?

I understand that in I2C, SCL and SDA lines use pull-up resistors and the pin drivers are open collector NPN devices which can drive pins to ground. This gives I2C an advantage that the same bus now can be shared with multiple slaves, and even if…
Ashutosh
  • 798
  • 1
  • 5
  • 15
20
votes
4 answers

I2C: 3.3 V and 5 V devices without level-shifting on 3.3 V bus?

Do I really need a level-shifter if I use 5 V-powered devices on an I2C-bus that has pull-ups to 3.3 V? In my understanding the devices will only pull the lines (SDA, SCL) low (to ground) and never drive their supply-voltage to the bus. So I don't…
Jannis
  • 303
  • 1
  • 2
  • 5
19
votes
4 answers

What I2C address should I choose?

Now I know in order to have an I2C address reserved for yourself you have to pay the I2C people some money. But I don't want to do this. I'm asking for advice on what address I should choose for my slave and master MCUs on my project. There will…
Thomas O
  • 31,546
  • 57
  • 182
  • 320
19
votes
7 answers

Is there any definitive I2C pin-out guidance out there? Not looking for a "STANDARD"

EDIT: This has been repeated several times, so putting it on top: Yes, it is well known that there is no "standard" for I2C inter-device connectors, but surely this community can formulate a list of "guidance" points for making such interconnects,…
Anindo Ghosh
  • 50,188
  • 8
  • 103
  • 200
1
2 3
99 100