I2C Communications (Wire)
This is an I2C communications library that facilitates two-wire class communications with I2C/TWI devices
(also called "Wire Library"). The pull-up to communication lines are needed. With a total of 8 serial channels available. You cannot use slave device.
The followings describe channels corresponding to pins, also you can confirm that in pin map.
To use, specify #include <wire.h>.
Wire : A5(SCL), A4(SDA)
Wire1: 0(SCL), 1(SDA)
Wire2: 7(SCL), 6(SDA)
Wire3: 26(SCL), 24(SDA)
Wire4: 5(SCL), 3(SDA)
Wire5: 8(SCL), 9(SDA)
Wire6: 11(SCL), 12(SDA)
Wire7: 60(SCL), 58(SDA)
begin
Description | Initiates the Wire library. |
---|---|
Syntax | Wire.begin() |
Parameters | None |
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 to request bytes from
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(unsigned char address) |
Parameters | address: the 7-bit address of the device to transmit to |
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 character string or data to end of 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 recevie 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 a data from slave device #2.
#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(2, 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); }