help on wire.requestFrom() function

Hi,

i could not understand that how can i give instructioın my master to read an exact byte in the slave.

It is seen that Wire.requestFrom(adress,quantity) has variables aof adress (of slave) and quantity of data that will be received.

But there is several bytes in slave. how can i give address of the right byte?

But there is several bytes in slave. how can i give address of the right byte?

Usually, you use Wire.beginTransmission(), Wire.write(), and Wire.endTransmission() to tell the slave what to do, and then use Wire.requestFrom() to tell the slave to cough up the required data.

Here is a snippet from the SFRRanger_reader example that shows how to select the register on the slave that you want to read from and then request the data. Change to the address and register that you want to read from.

Wire.beginTransmission(112); // transmit to device #112
  Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting

  // step 4: request reading from sensor
  Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112 register (0x02)

in short: first you write the register address to the slave, then you request data

Hello there!

Below is a snippet of a code that I have been working with for the past several months. It correctly gets data from a specified register in the slave, which for me is an IR sensor.

static int i2c_smbus_read_byte_data(int reg)
{
  int value = -1;

  Wire.beginTransmission(APDS9960_I2C_SLAVE_ADDRESS);
  Wire.write(reg);
  Wire.endTransmission();

  Wire.requestFrom(APDS9960_I2C_SLAVE_ADDRESS, 1);

  while (Wire.available())
  {
    value = Wire.read();
  }

  return value;
}

This is a generic function that can be used to read a byte from any register in the IR sensor slave. You can call this function with the specific byte address of what register you want.

thanks for clear answers.
i got it

best regards,