[Solved] Library for I2C communication

Hi there!
As I know, there are multiple libraries in arduino for I2C communication, Including Wire.h, util/twi.h and OneWire. Maybe there are other libraries as well!
Does anybody know what is the difference between those libraries and where to use each?

thanks a lot

There is only one library for I2C, that is the Arduino Wire library: Arduino - Wire.
For less than 1% of the situations there are other compatible libraries, for example a software I2C library.
You should ignore the util/twi.h. That is just a low-level part of the Arduino Wire library. Don't think about it :wink: it is there, that's all.

If you want to write code for an Arduino, then you should use the official Arduino Wire library. When you later change to another Arduino board, then the I2C will still work with the same functions.

The Dallas 1-Wire is something completely different.
The most used Arduino library for the Dallas 1-Wire is called: OneWire.

There are other libraries that have "Wire" in the name.
For example: "VirtualWire". It has nothing to do with I2C and also nothing to do with 1-Wire.

1. For serial data communication using 2-wire (A4 and A5) between UNO and sensor (DS3231 RTC), we include Wire.h Library in the sketch.

2. For serial data communication using 1-wire (any valid DPin) between UNO and sensor (DS18B20), we include OneWire.h Library in the sketch.

3. Check in the following diagram for the devices which uses which Library.
multicom.png

multicom.png

Thank you.
but how can serial communication work with just one wire??

It's a marketing feature. They didn't count the return (GND) when they came up with the name.

Don

What about SCL and SDA wires? At least they should count for them. ::slight_smile:

The I2C bus uses three wires: SCL, SDA and GND.
Since the I2C bus is meant to be used on the same PCB board, the GND is already connected. The I2C bus is not meant to go through a cable.
Both the Master and the Slave can only make the SCL and SDA low. Those signals are pulled high with a resistor. That means both the Master and Slave can send information to each other.
The clock signal (SCL) is always provided by the Master. But even the SCL signal is not sacred. A Slave is allowed to pull the SCL signal low to tell the Master that it needs more time.

Note: For high speed mode, the Master may temporary make the signal high. The Arduino Uno can not do that high speed mode.

For the 1-Wire bus, the signal is also pulled high with a resistor. With a clever protocol that uses specific timing it is possible that information can go both ways. The protocol defines when the microcontroller and when the sensor may use the bus.

Thank you all!