Problem I2c (Wire Library)

Using Arduino UNO
I am trying to send 2 data bytes to a device: its address is 0x4d, first byte 0x18 and the third 0x80
Sketch Code has the usual Wire.begin() in the setup.

Wire.beginTransmission(0x4d);
Wire.write(0x18);
Wire.write(0x80);
Wire.endTransmission(true);

I have another device on the I2C buss monitoring the data on the buss, it reports the following data being send by the UNO SA9aK30NSAP

S== Start A9A == device address (Correct) K == Ack for the address. From here its wrong
30 (should be my second byte 0x18) N == NAC
It then looks like another start being sent to address null and the P is the Stop

After Lots of checks I have found the following: the 0x18 is being shifted left 1 bit to provide the 30

The third byte appears to trigger another start.

Any Help would be gratefully received.
Regard
Ernie

Post your complete code

Using Arduino UNO
I am trying to send 2 data bytes to a device: its address is 0x4d, first byte 0x18 and the third 0x80
Sketch Code
// Wire Master Writer

#include <Wire.h>

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

void loop() {
Wire.beginTransmission(0x4d); // transmit to device
Wire.write(0x18); // register select
Wire.write(0x80); // data
Wire.endTransmission();

delay(2000);
}

I have another device on the I2C buss monitoring the data on the buss, it reports the following data being send by the UNO SA9aK30NSAP

S== Start A9A == device address (Correct) K == Ack for the address. From here its wrong
30 (should be my second byte 0x18) N == NAC
It then looks like another start being sent to address null and the P is the Stop

After Lots of checks I have found the following: the 0x18 is being shifted left 1 bit to provide the 30

The third byte appears to trigger another start.

Any Help would be gratefully received.
Regard
Ernie

What type of monitoring device is that? A logic analyzer? How much do you trust the monitoring output?

Wrong pull-ups for the given bus capacity may show such a behavior. As you failed to provide a wiring diagram we have no clue about your circuitry.

Buss Pull-ups were 4.7K
Monitor unit clock set 150K. It was easily capable of communicating to the Device at this speed with NO problems. So it didnt look as if the buss capacitance/pull-up's causing the problem.

Set the UNO I2C clock (Wire.setClock) to 100K Caused the data shifting problem, as outlined in the post. Reset the Arduino I2C clock to 50K everything is OK, upped speed to 75K problems, dropped to 70K all OK again.

UNO running on 16M/hz crystal perhaps asking for high clock rates on the I2c is a little ambitious. Will keep it running at 50K.

Thanks for your Help.
Ernie

How long are your wires?

I am building a modular "Air Purity System"

I Have 6 similar interface devices (all addressed differently) all looking at a different Gas components in the Supplied Air. The gas sensors are daisy chained on the Buss. Total cable length 70cm.

Buss driven from one end by the UNO and monitored at the other by my Analyser. Seems quite happy running at 50K With a slight 100M/s delay between the blocks of I2C code.

To verify that the I2C Master is sending data correctly to the I2C Slave, please carry out the following 2 Arduino UNO based experiment.

Master Codes:

#include<Wire.h>

void setup()
{
   Serial.begin(9600);
   Wire.begin();
  
  Wire.beginTransmission(0x4d);
  Wire.write(0x18);
  Wire.write(0x80);     
  byte busStatus = Wire.endTransmission(true);
  if(busStatus != 0)
  {
     Serial.print("Slave is not present...!");
     while(1);    //wait for ever
  }
  Serial.print("Slave is present.");
}

void loop()
{

}

Salve Codes:

#include<Wire.h>
volatile bool flag = false;
byte x1, x2;

void setup()
{
   Serial.begin(9600);
   Wire.begin(0x4D);
   Wire.onReceive(receiveEvent);
}

void loop()
{
   if(flag == true)
   {
       int x =  x1<<8|x2;
       Serial.print("Value received is: ");
       Serial.print(x, HEX);
       flag = false;
   }
}

void receiveEvent(int howMany)
{
   x1 = Wire.read();
   x2 = Wire.read();
   flag = true;
}

@Vista-IT, do not cross-post. Threads merged.

Total cable length 70cm.

With full speed it often work up to about 50cm. The longer bus probably explains why it only works at lower speeds. Attach a scope and take a look at the wave form. I'd expect the signals to raise to slow for a 100kHz speed.