1

I'm checking an example for the MPU6050 and got to this part

Wire.beginTransmission(0x68);           //begin, Send the slave adress (in this case 68) 
Wire.write(0x1B);                       //We want to write to the GYRO_CONFIG register (1B hex)
Wire.write(0x10);                       //Set the register bits as 00010000 (1000dps full scale)
Wire.endTransmission(true);             //End the transmission with the gyro

Serial.begin(9600);                     //Remember to set this same baud rate to the serial monitor  
time = millis();                        //Start counting time in milliseconds



/*Here we calculate the gyro data error before we start the loop
 * I make the mean of 200 values, that should be enough*/
if(gyro_error==0)
{
  for(int i=0; i<200; i++)
  {
    Wire.beginTransmission(0x68);            //begin, Send the slave adress (in this case 68) 
    Wire.write(0x43);                        //First adress of the Gyro data
    Wire.endTransmission(false);
    Wire.requestFrom(0x68,4,true);           //We ask for just 4 registers 

Why is there two consecutive wire.write() in the begining and only one later? Is it different somehow?

  • 1
    it is write(byte). to write two bytes to TX buffer it must be called twice – Juraj Oct 3 '19 at 18:13
1

The truth is, the Arduino Wire library sends the data only if endTransmission is called. Until then it collects the data in TX buffer. The version of the write function used in your snippet adds a byte to the TX buffer.

With I2C usually the first byte written is the address of the register to write. Then the data follow. In case of the first part in your snippet the data are only one byte long.

In the second part, first the register address is sent with write and then the data from this register and 3 following registers are requested.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.