Andrea Ciufo
Andrea Ciufo

Reputation: 379

MPU 6050 SD Writing Frequency Arduino is not constant , how to have a constant writing frequency?

I am a noob and i am from civil engineer, I apologize in advance.

I have a MPU6050 connected to my Arduino Uno and I write the data on an SD. I can write at a sample rate of 20millis.

Why i have this default frequency?

This is part of the code

// Main:
  void loop()
  {
    // Body of main/loop
    // Check status:
    Time0 = millis();
    Read_Write();
   // LED off and continue:
      digitalWrite(LEDpin, LOW);
      
       }

  void Read_Write()
  // function that reads the MPU and writes the data to the SD.
  {
   // Local variables:
    int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; // Variables read from MPU6050

    // Read data:
    Wire.beginTransmission(MPU);
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
    AcX = Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
    AcY = Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
    AcZ = Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
    
    GyX = Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
    GyY = Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
    GyZ = Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
    
    Serial.print(AcX);
    Serial.print(',');
    Serial.print(AcY);
    Serial.print(',');
    Serial.print(AcZ);
    Serial.print(',');
    Serial.print(GyX);
    Serial.print(',');
    Serial.print(GyY);
    Serial.print(',');
    Serial.print(GyZ);
    Serial.print(',');
    
    Serial.println();
    
    
    // Data preparation for file saving:
    String dataString = ""; // string for assembling the data to log:

    // Add time tag:
    dataString += String(Time0); dataString += ",";

    // Append the MPU6050 data to the string:
    dataString += String(AcX); dataString += ",";
    dataString += String(AcY); dataString += ",";
    dataString += String(AcZ); dataString += ",";
    dataString += String(GyX); dataString += ",";
    dataString += String(GyY); dataString += ",";
    dataString += String(GyZ);
    
    // Open the file in append mode:
    File dataFile = SD.open("datalog.txt", FILE_WRITE);

    // If the file is available, write to it:
    if (dataFile) 
    {
    dataFile.println(dataString);
    dataFile.close();
    if (Serial_plus_SD)
      Serial.println(dataString);
    }
    // if the file does not open, pop up an error:
    else 
    errorFW();
    
    return;
  }

Why is not constant the logging time but sometimes is 40millisecond (see the image)? enter image description here

For me is important because my goal is to have a costant frequency to apply an FFT to study the noise and identify the best low pass filter.

How can I modify the frequency? I though to put a delay() on my code.

 void loop()
      {
        // Body of main/loop
        // Check status:
        Time0 = millis();
        Read_Write();
       // LED off and continue:
          digitalWrite(LEDpin, LOW);
          delay(80)
           }

Upvotes: 1

Views: 1045

Answers (0)

Related Questions