I2C Communications (Wire)

This is an I2C communications library that facilitates two-wire class communications (also called "Wire Library"). The pull-up to communication lines is needed. There is a total of five channels available. You cannot use a slave device. The following information describes channels corresponding to pins, which can be confirmed by referring to the pin map.

To use, specify #include <Wire.h>.

 

   Wire: 19(SCL), 18(SDA) (Software I2C)
   Wire1: 1(SCL), 0(SDA)
   Wire2: 6(SCL), 5(SDA)
   Wire3: 8(SCL), 7(SDA)
   Wire4: 11(SCL), 12(SDA)

begin

Description
Initiates the Wire library.
Syntax
Wire.begin(address)
Parameters
address: Specifies a 7-bit address when operating as a slave. It is not necessary during master operation.
Returns
None

requestFrom

Description
Used by the master to request bytes from a slave device. The bytes may then be retrieved with the available() and read() functions.
Syntax
Wire.requestFrom(address, quantity)
Wire.requestFrom(address, quantity, stop)
Parameters
address: The 7-bit address of the device from which to request bytes.
quantity: The number of bytes to request.
stop: Boolean. True will send a stop message after the request, releasing the bus. False will continually send a restart after the request, keeping the connection active.
Returns
Byte: The number of bytes returned from the slave device.

beginTransmission

Description
Begin a transmission to the I2C slave device with the given address. Subsequently, queue bytes for transmission with the write() function and transmit them by calling endTransmission().
Syntax
Wire.beginTransmission(address)
Parameters
address: The 7-bit address of the device to which to transmit
Returns
None

endTransmission

Description
Ends a transmission to a slave device that was begun by beginTransmission() and transmits the bytes that were queued by write().
Syntax
unsigned char Wire.endTransmission()
Parameters
None
Returns
0: Success
1: Data too long to fit in transmit buffer
2: Received NACK on transmit of address
3: Received NACK on transmit of data
4: Other error

write

Description
Adds a character string or data to the end of the transmit buffer.
Syntax
Wire.write(value)
Wire.write(string)
Wire.write(data, length)
Parameters
value: A value to send as a single byte
string: A string to send as a series of bytes
data: An array of data to send as bytes
length: The number of bytes to transmit
Returns
Byte: Write() will return the number of bytes written, though reading that number is optional.

available

Description
Returns the number of bytes available for retrieval in the receive buffer.
Syntax
Wire.available()
Parameters
None
Returns
The number of bytes available for reading.

read

Description
Reads one byte of data from the receive buffer.
Syntax
Wire.read()
Parameters
None
Returns
The next byte received.

Sample Program

Receive 6 bytes of data from slave device at address 0x40.


#include <Arduino.h>
#include <Wire.h>
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}
 
void loop()
{
  Wire.requestFrom(0x40, 6);    // request 6 bytes from slave device #2
 
  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read();    // receive a byte as character
    Serial.print(c);         // print the character
  }
 
  delay(500);
}