// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include<Wire.h>
const int MPU=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
unsigned long startmilli,endmilli,dur;
void setup(){
Wire.begin();
//Wire.setClock(400000L)
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
startmilli=millis();
}
void loop(){
Wire.beginTransmission(MPU);
Wire.write(0x47); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,2,true); // request a total of 14 registers
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
endmilli=millis();
dur=(endmilli-startmilli);
startmilli=endmilli;
Serial.println(dur);
}
I am using this to read z- gyroscope register of MPU6050. This takes 3 or 4 milliseconds to execute 1 loop. So depending on that sampling frequency changes from 333.33Hz to 250 Hz. But I want A constant sampling frequency.
Is there any timer function so that I will call the function to read gyro readings after every 5 milliseconds?
Is my way of calculating sampling frequency correct? (I know this is not the true sampling frequency, But
this is the rate I am getting sensor output.)