1

In this part of code

void readFrom(byte device, byte fromAddress, int num, byte result[]) {
 Wire.beginTransmission(device);
 Wire.write(fromAddress);
 Wire.endTransmission();
 Wire.requestFrom((int)device, num);
 int i = 0;
 while(Wire.available()) {
 result[i] = Wire.read();
 i++;
 }
}

How can Arduino know that Wire.availble() is true or false?

I thought that when the microcontroller "reads" the value from the sensor, in pieces of 8 bits, he reads from 0 (n) to 5 (n+5) at the I2C address of the MPU-6050, and when he increments 6 it finds nothing, but this is my conclusion, i am not sure about it. enter image description here

I am studying how Serial.available works and how the circular buffer works for the data comunication, because available() inherits from the Stream utility class.

The entire code is the following taken from this question

 #include <Wire.h> //The I2C library
int gyroResult[3], accelResult[3];
//Function for writing a byte to an address on an I2C device
void writeTo(byte device, byte toAddress, byte val) {
 Wire.beginTransmission(device); 
 Wire.write(toAddress); 
 Wire.write(val); 
 Wire.endTransmission();
}
//Function for reading num bytes from addresses on an I2C device
void readFrom(byte device, byte fromAddress, int num, byte result[]) {
 Wire.beginTransmission(device);
 Wire.write(fromAddress);
 Wire.endTransmission();
 Wire.requestFrom((int)device, num);
 int i = 0;
 while(Wire.available()) {
 result[i] = Wire.read();
 i++;
 }
}
//Function for reading the gyros.
void getGyroscopeReadings(int gyroResult[]) {
 byte buffer[6];
 readFrom(0x68,0x1D,6,buffer);
 gyroResult[0] = (((int)buffer[0]) << 8 ) | buffer[1]; //Combine two bytes into one int
 gyroResult[1] = (((int)buffer[2]) << 8 ) | buffer[3];
 gyroResult[2] = (((int)buffer[4]) << 8 ) | buffer[5];
} 
//Function for reading the accelerometers
void getAccelerometerReadings(int accelResult[]) {
 byte buffer[6];
 readFrom(0x53,0x32,6,buffer);
 accelResult[0] = (((int)buffer[1]) << 8 ) | buffer[0]; //Yes, byte order different from gyros'
 accelResult[1] = (((int)buffer[3]) << 8 ) | buffer[2];
 accelResult[2] = (((int)buffer[5]) << 8 ) | buffer[4];
}
void setup() {
 Wire.begin(); //Open I2C communications as master
 Serial.begin(9600); //Open serial communications to the PC to see what's happening
 writeTo(0x53,0x31,0x09); //Set accelerometer to 11bit, +/-4g
 writeTo(0x53,0x2D,0x08); //Set accelerometer to measure mode
 writeTo(0x68,0x16,0x1A); //Set gyro to +/-2000deg/sec and 98Hz low pass filter
 writeTo(0x68,0x15,0x09); //Set gyro to 100Hz sample rate
}
void loop() {
 getGyroscopeReadings(gyroResult);
 getAccelerometerReadings(accelResult);
 Serial.print(gyroResult[0]);
 Serial.print("\t");
 Serial.print(gyroResult[1]);
 Serial.print("\t"); 
 Serial.print(gyroResult[2]);
 Serial.print("\t\t");
 Serial.print(accelResult[0]);
 Serial.print("\t");
 Serial.print(accelResult[1]);
 Serial.print("\t");
 Serial.print(accelResult[2]);
 Serial.print("\n");
 delay(50);
}

Minor Question

I am trying to undestand the difference between the register and the device address, but i did find anything on my book. enter image description here

SDsolar
1,1753 gold badges11 silver badges35 bronze badges
asked May 28, 2017 at 20:58
8
  • 3
    You've used lots of words, but I don't actually understand what it is you think you are asking. Commented May 28, 2017 at 21:04
  • Sorry, i think my bad english is a problem to explain my question. The while statement end when Wire.available() is false. This happen when there is nothig to read (?). How can arduino know that there is nothing nothing more to be read? Thank you :) Commented May 28, 2017 at 21:08
  • 1
    Because the Wire library software coupled with the I2C hardware interface has determined that is the case? You should read the datasheet and the Wire library source code. Commented May 28, 2017 at 21:09
  • If you request 5 bytes you have 5 bytes in the buffer, and when you have read all 5 there will be nothing available. Commented May 28, 2017 at 21:10
  • Ok, i think, now is little bit clearer, i will read the Wire library source code . I have read the mpu6050 datasheet yet, your blog post on buffer, but it was still not clear and hard for a self-taught like me :) Commented May 28, 2017 at 21:16

1 Answer 1

1

The simple answer is that the Arduino Wire library has an internal buffer, and it can bring in characters and have them sitting there ready for you to pick them up.

It can also sense a total disconnect because of the way it uses both data pins to establish communications. It is more like a current loop than a simple one-is-transmit-and-the-other-is-receive. If it disconnects the Arduino knows it is not available.

Kudos on going to the documentation, by the way.

Jot
3,2761 gold badge14 silver badges21 bronze badges
answered May 29, 2017 at 3:32
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.