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 is needed. The available channels and pin numbers are as follows and can also be confirmed in the pin map. Please note that ESP32 is connected as address 0x28.

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

Wire: 21(SCL), 20(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 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(unsigned char 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 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);
}