Wire.read() function is storing all bits as 1,

the datasheet shows an example of a captured waveform of i2c communication is attached below. Now I have the following questions

  1. Can the code attached below can create a waveform as shown in the attachment below?
  2. I doubt --->wire.read()<------- this is writing all bits as 1.. What could be the problem?

Wire.beginTransmission(0x08); // device address
** Wire.write(LOW_BYTE(0x9261));**
** Wire.write(HIGH_BYTE(0x9261));**
** Wire.endTransmission();**

** Wire.write(0x40);**
** Wire.endTransmission();**

** Wire.requestFrom(0x08,1);**
** while(!Wire.available());**
** byte lsb = Wire.read();**
** Serial.print(lsb);**

Comments on attachment

  1. 0x10 ------> write command for the device, which I assume wire.begin transmission does
  2. 0x11------> read command for the device, which I assume wire.requestFrom does
  3. 0x61 , 0x91 ------> 16bit address of register
  4. 0x40 command for resposne
    5. 0x88 should be the output, instead, I am getting 0xFF

Where am I wrong?

Don’t do this: while(!Wire.available());. Remove that line please.

First of all, use a I2C Scanner sketch to find the I2C address of the Slave. Is it really 0x08 ?
Your capture shows 0x10, which converted to the 7-bit shifted I2C address makes 0x08, but I want to be sure.

Please just send the data. When solving a problem, don’t make it more complicated.

// Wire.write(LOW_BYTE(0x9261));
// Wire.write(HIGH_BYTE(0x9261));
Wire.write( 0x61);
Wire.write( 0x92);

Notice that when reading data, there is no STOP condition in the middle. There is a second START.
A START without a previous STOP is called a “repeated start”.

Wire.beginTransmission( 0x08);
Wire.write( 0x40);
Wire.endTransmission( false);   // 'false' for a repeated start
int n = Wire.requestFrom( 0x08, 1);
if( n == 1)
  byte lsb = Wire.read();
  Serial.print( lsb);
  Serial.println( "No data received");

When there was something wrong, the Wire.requestFrom() returns zero or the Wire.available() is zero. Then there is no data, and Wire.read() will return -1. The -1 pushed into a byte will result into 0xFF.

Do you see that small spike in the capture ? You may ignore it. That happens sometimes.

This is not all. There could be a voltage mismatch on the bus, timing issues, weird things that are not according to the I2C standard, and so on.

Can you put code between code-tags ? The </> button is for code tags.
Can you show the whole sketch ?

Yes, a 'scope is better for checking the logic waveform - a logic analyzer may have its threshold
voltage set differently from the microcontroller (in fact this is inevitable really), so if its something
to do with the bus drive voltages / pullup strength etc, the LA may lie to you, whereas a 'scope
will not lie.

Try the following codes (your snippet with correction/adjustment): (Are you sure that your Slave is expecting lower byte first. Please, provide a link to your slave.)

Wire.beginTransmission(0x08);               // device address
  byte busStatus = Wire.endTransmission();
  if(busStatus !=0)
       Serial.print("Transmission Error...!");
       while(1);   //wait for ever

  Wire.write(0x40);                      //pointing t internal register

  byte lsb = Wire.read();
  Serial.print(lsb, HEX);

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