Sending byte words trough I2C with the Wire library


I'm seeking a way to send binary words to an I2C line following sensor with my Duemilanove.

With the Wire library I can send 7bit words, with the last bit added by the function called (1/0 for read/write)

I want to send a full 8bit word (by specifying myself the read.write bit)

I tried to dvelve into the Wire library, and :

To send data :


beginTransmission set the address, send fill the transmission buffer, and endTransmission runs some checks then take the address, the buffer and sends them to the advice.

endTransmission() uses the twi.c library by calling the twi_writeTo function

This is where I need your help ! Because I can't understand where is sent the data... It's like it is somwhere else.

For a full comprehension of my problem, i have to send this sequence in my sensor : i2c_start(); i2c_write(currAddress); i2c_write(0x00); i2c_write(0xA0); i2c_stop();

i2c_start(); i2c_write(currAddress); i2c_write(0x00); i2c_write(0xAA); i2c_stop();

i2c_start(); i2c_write(currAddress); i2c_write(0x00); i2c_write(0xA5); i2c_stop();

i2c_start(); i2c_write(currAddress); i2c_write(0x00); i2c_write(newAddress); i2c_stop() And i don't know how to "translate" it into the Arduino's Wire language

The twi library can be found on the libraries\Wire\utility folder

Thanks, LGui

Wire.send() isnt specific enought? It SENDS the data through i2c, then the EndTransmission makes an Ack or Nack dont really remenber and them it stops.

LGui: Hi,


LGui: I'm seeking a way to send binary words....

With the Wire library I can send 7bit words, with the last bit added by the function called (1/0 for read/write)

No, that's for the very first thing sent (the control byte with the device ID) The Read/Write thing is done automatically with the Wire library functions.

You give the I2C device 7-bit address then you send (or receive) 8-bit data bytes.

Here is how I send a single byte to a small (24C02) I2C EEPROM. (A 24C02 has 256 8-bit data words.)

void i2c_eeprom_write_byte(uint8_t dev, uint8_t addr, uint8_t data)
    Wire.beginTransmission(dev);  // 7-bit address is 0x50.  Lower bits indicate connections to the  A2, A1, A0 pins on the 24C02.
    Wire.send(addr); // 8-bit EEPROM address (0 - 255 decimal)
    Wire.send(data); // 8-bit data byte (0 - 255 decimal)
    delay(3); // Wire doesn't do ack polling, so wait for write operation to complete

Here's how I read a single 8-bit byte from a 24C02

int i2c_eeprom_read_byte(uint8_t dev, uint8_t addr)
    int rdata = -1;
    // First set up the address

    // Now, read the data
    Wire.requestFrom(dev, (uint8_t)1);
    if (Wire.available()) {
        rdata = Wire.receive();
    return rdata;

If there is a byte from the EEPROM, its value (0 - 255 decimal) will be returned. If Wire.available() fails, the function returns -1



Footnote: There are separate 32-byte buffers for I2C transmit and receive data.

Here is the sequence of function calls fol writing to my small EEPROM:

The beginTransmission(dev) function initializes the transmit data buffer (sets data buffer length variable to zero and shifts the 7-bit device address one to the left so that it becomes an eight-bit control byte) and stores the control byte in the buffer).

After beginTransmission has set things up, each call to send(xxx) stores an eight-bit byte in the buffer and increments the buffer length variable. Note that it can hold a maximum of 31 bytes after the control byte. For my example, the eight-bit EEPROM address and an eight-bit EEPROM data byte are stored in the buffer.

The call to endTransmission() causes the contents of the buffer to be transmitted by calling the twi_writeTo() function. (I wish the Wire library gurus had used some other name here, but...)

The twi_writeTo() write function copies the Wire buffer to its own buffer (it may seem wasteful, but, there is a reason for doing it this way---I think), and performs functions to become the I2C bus master and send the buffer contents to the device.

Hi, Seno, the Wire.send(data) dont send the data, it just fill the buffer to be send by endTransmission().

Thanks for clarifing thing up Dave, i tought first that only one byte was sent, not two.

I tested my code this way and it works like a charm

Thanks again !