I²C/wire.h Buffer way smaller than 32 bytes?

Hey there,
im having Problems with communicating with a BMS over I²C. I boiled down the problem to the size of the Buffer of the wire.h library. In the documentation it is stated, that the buffer should be 32 bytes long. So the following lines of code should not be a problem:

Wire.beginTransmission(I2C_ADDR>>1); 
  Wire.write(0x01); 
  Wire.write(0x02);  
  Wire.write(0x03);
  Wire.write(0x04);       
  Wire.write(0x05);
  Wire.write(0x06);
  byte txResult = Wire.endTransmission();

However on the scope i can see that only the first three bytes get transmitted. Apart from a missing ACK the endTransmission() command does not report any problems. This looks like the buffer is only three bytes long without the endTransmission command reporting this as a problem.

Has anybody had a similar problem? And maybe a solution to send more than 3 bytes?

Looks like it is defined as 32:

Please post the entire code that has the problem, and the type arduino you are using.

The Problem occurs both on MEGA 2560 and on the Micro

#include <Wire.h>
#define I2C_ADDR            0x10
void setup() {
  Serial.begin(9600);
  Wire.begin(); 
}
void loop() {
  Wire.beginTransmission(I2C_ADDR>>1); 
  Wire.write(0x01); 
  Wire.write(0x02);  
  Wire.write(0x03);
  Wire.write(0x04);       
  Wire.write(0x05);
  Wire.write(0x06);
  byte txResult = Wire.endTransmission();
  Serial.print("Tx: ");
  Serial.println(txResult);
  
  delay(500);
}

Pleae, re-run the following sketch (yours' one of post #4 with slight modification) and report what you are seeing on the Serial Monitor.

#include <Wire.h>
#define I2C_ADDR            0x10

void setup() 
{
  Serial.begin(9600);
  Wire.begin(); 
}

void loop() 
{
  Wire.beginTransmission(I2C_ADDR>>1); 
  //Wire.write(0x01); 
 // Wire.write(0x02);  
  //Wire.write(0x03);
  //Wire.write(0x04);       
  //Wire.write(0x05);
  //Wire.write(0x06);
  byte txResult = Wire.endTransmission();
  if(txResult !=0)
  {
      Serial.print("Communication error!")
      while(true);  //wait for ever
  }  
  Serial.println("Good I2C Bus communicaton.");
  delay(1000);
}

I get "Good I2C Bus communicaton." on the bus. Nothing suprising since other (shorter) commands already worked. The I²C Slave is a BQ769402 BMS.

Your sketch of post #4 is not a diagnostic sketch as it does not contain any code that prints diagonostic message.

I think I found a problem. I didn't now that a I²C Master aborts the communication as soon as a Slave reports a NACK. I thougt the NACK could be ignored. Since the Slave didn't understand the 01 02 03 04 commands on the bus it NACKed and Arduino aborted the bus after three bytes.

The original Problem occured with a more complex command that i was testing after several "easy" commands were successfully implemented. So there must be a problem with the order of individual bytes in the complex command.

I thought that the Arduino itself aborted any comms longer than x bytes but it was the Slave that was confused with the commands, NACKed and thus stopped the Arduino communicating.

I didn't know NACKs where a serious problem, because i get them frequently when reading data from the slave. (The Arduino doesn't ACK when reading the bytes.)

Thanks everyone!

Which Arduino you are using (UNO/NANO/MEGA)?

What have you seen on the Serial Monitor -- 0 or non-zero?

As stated above the problem occurred on MEGA and Micro
I got 3 so NACK on data. That was the reason the Arduino aborted further I²C commands.

But, in post #6, you have said the following; whereas, 3 indicates an error (Fig-1).

I2CerrorCodes
Figure-1:

You are not supposed to connecrt 5V MEGA with your sensor which is a 3.3V (data sheet says: 3.6V) device. Hope that your sensor is not fried!

Which page of the data sheet does show the device address 0x10?

Shorter, successfully implemented commands didn't result in the BMS NACKing so there wasn't a problem. I got NACKs when reading data from the BMS so i thought some NACKs wouldn't be a problem.

Both MEGA and Micro a 5V Controllers. But since the Pullups to 5V are quite weak (10K on MEGA, None/only internal on Micro) the BMS is perfectly fine. It too has 10K pullups to its own 3,3V supply. I used the Mega for some weeks now. The final MCU will be 3,3V though.

In the meantime, you thaink you can work with 5V Arduino being connected with 3.3V sensor?

The BMS is supplied by the cells itself. With 4 cells that's up to 16,8V. It has internal LDOs to supply itself with 3,3V. The PullUps on the MEGA from SDA/SCL to 5V have so far not been a problem. According to the Datasheet the BMS can tolerate up 5,5V on SDA/SCL without any problems.
The I²C Adress is stated on page 53 of the datasheet.
The Recommended operating Voltage Min/Max of SDA/SCL is stated on page 9 of the datasheet.

1 Like

Usually, the data sheets mention the 7-bit slave/device address. This data sheets have given the 8-bit I2C Bus address which you have shifted 1-bit right to get the 7-bit slave/device address.

Yes this is correct.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.