user10231: Is there any limit on the number of Wire.write() commands consecutively in between the Wire.beginTransmission() command and the Wire.endTransmission() command for the Arduino Wire library?

user5063: A quick look at the source code implies that there is a fixed length buffer of 32 bytes:
From the header file wire.h:
#define BUFFER_LENGTH 32

And from the implementation of the write function:
size_t TwoWire::write(uint8_t data)
{
  if(transmitting){
  // in master transmitter mode
    // don't bother if buffer is full
    if(txBufferLength >= BUFFER_LENGTH){
      setWriteError();
      return 0;
    }
    // put byte in tx buffer
    txBuffer[txBufferIndex] = data;
    ++txBufferIndex;
    // update amount in buffer   
    txBufferLength = txBufferIndex;
  }else{
  // in slave send mode
    // reply to master
    twi_transmit(&data, 1);
  }
  return 1;
}


user10794: There are five buffers used by the TWI and Wire library. They are defined as 32 bytes.
In wire.h:
#define BUFFER_LENGTH 32

In Wire.cpp:
uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
...
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];

In twi.h:
  #define TWI_BUFFER_LENGTH 32

In twi.c:
static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
...
static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
...
static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];

So immediately you are using 5 x 32 bytes (160 bytes).

You could increase that to (say) 64 bytes, bearing in mind your 160 byte overhead is now 320 bytes, and you don't have a lot of RAM.
The comments about endTransmission() are correct. After a Wire.beginTransmission() nothing is actually transmitted until the endTransmission() call. This lets you fill up the buffer at your leisure, and then let the library do the writes in a fairly timing-specific way.

Reference

I2C
Wire Library


10231: <p>Is there any limit on the number of <code>Wire.write()</code> commands consecutively in between the <code>Wire.beginTransmission()</code> command and the <code>Wire.endTransmission()</code> command for the Arduino Wire library?</p>
 5063: <p>A quick look at the source code implies that there is a fixed length buffer of 32 bytes:</p>

<p>From the header file <code>wire.h</code>:</p>

<pre><code>#define BUFFER_LENGTH 32
</code></pre>

<p>And from the implementation of the <code>write</code> function:</p>

<pre><code>size_t TwoWire::write(uint8_t data)
{
  if(transmitting){
  // in master transmitter mode
    // don't bother if buffer is full
    if(txBufferLength &gt;= BUFFER_LENGTH){
      setWriteError();
      return 0;
    }
    // put byte in tx buffer
    txBuffer[txBufferIndex] = data;
    ++txBufferIndex;
    // update amount in buffer   
    txBufferLength = txBufferIndex;
  }else{
  // in slave send mode
    // reply to master
    twi_transmit(&amp;data, 1);
  }
  return 1;
}
</code></pre>
 10794: 

<p>There are <strong>five</strong> buffers used by the TWI and Wire library. They are defined as 32 bytes.</p>

<p>In wire.h:</p>

<pre class="lang-C++ prettyprint-override"><code>#define BUFFER_LENGTH 32
</code></pre>

<p>In Wire.cpp:</p>

<pre class="lang-C++ prettyprint-override"><code>uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
...
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
</code></pre>

<p>In twi.h:</p>

<pre class="lang-C++ prettyprint-override"><code>  #define TWI_BUFFER_LENGTH 32
</code></pre>

<p>In twi.c:</p>

<pre class="lang-C++ prettyprint-override"><code>static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
...
static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
...
static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
</code></pre>

<p>So immediately you are using 5 x 32 bytes (160 bytes).</p>

<hr>

<p>You could increase that to (say) 64 bytes, bearing in mind your 160 byte overhead is now 320 bytes, and you don't have a lot of RAM.</p>

<p>The comments about <code>endTransmission()</code> are correct. After a <code>Wire.beginTransmission()</code> nothing is actually transmitted until the <code>endTransmission()</code> call. This lets you fill up the buffer at your leisure, and then let the library do the writes in a fairly timing-specific way.</p>

<hr>

<h3>Reference</h3>

<ul>
<li><a href="http://www.gammon.com.au/i2c" rel="noreferrer">I2C</a></li>
<li><a href="http://www.arduino.cc/en/Reference/Wire" rel="noreferrer">Wire Library</a></li>
</ul>
