Problem with range writing MPU6050 data to a SD card

Hi everyone,

I am new in the forum so excuse me if I don t folow some pre-defined methodology. I’m not at all an expert and my questions may be basic. I’m trying to learn thougth.

I’m trying to make a car datalogger and I’m having problems writing the correct data from an MPU6050 accelerometer to a SD card. I’m using the library: Version: 1.0.3 (c) 2014-2015 Korneliusz Jarzebski

The problem is: If I connect the arduino via Serial, to the PC, and SPI, to the SD card, I can change the range (2,4,8 or 16g) and it writes correctly to serial and sometimes to the sd card.
The values written in the SD card are exactly the same floats as the ones written to the serial, but they sometimes come different.

However if I plug it in the USB with the arduino software closed in the PC (or a different external power source), the range that is used and written to the SD card is 2g no matter what.

I dont know if I made myself clear. If I dont please warn me so that I can try and formulate the question better. I’ve already lost lots of time reading the library code and googling, and I can’t find a reason for this.

Thank you in advance.

Code used: (The code is a bit changed as I attempted some useless things, but the base is there)

// Bibliotecas
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <MPU6050.h>

// Definição de variaveis
MPU6050 mpu;
unsigned long time;

// codigo do setup
//****************************** Setup **************************************//
void setup()
{
Serial.begin(115200);
pinMode(9, OUTPUT);

// Definição de velocidade de comunicação de I2C
Wire.setClock(400000);

// SPI SD card
SPI.setClockDivider(SPI_CLOCK_DIV2);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

/***************** MPU6050 *****************/
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_16G))
{
delay(5000);
}

/******************** SD *******************/
//Check if card is ready
if (!SD.begin(10))
{
digitalWrite(9 , LOW);
return;
}
digitalWrite(9, HIGH);

//Escrever cabeçalho Log.csv
File datafile = SD.open(“DataLog.csv”, FILE_WRITE);
if (datafile)
{
datafile.println(" ");
datafile.close();
}
else
{
return;
}

}

//****************************** Ciclo **************************************//

void loop()
{
time = millis();

/***************** MPU6050 *****************/
// Obtenção de valores
Vector normAccel = mpu.readNormalizeAccel();

float xMPU = normAccel.XAxis/9.8;
float yMPU = normAccel.YAxis/9.8;
float zMPU = normAccel.ZAxis/9.8;
float temp = mpu.readTemperature();

// Escrever para o SD
String datastring = String(temp) + “,” + String(time);

//Abre ficheiro, escreve e fecha
File datafile = SD.open(“DataLog.csv”, FILE_WRITE);
if (datafile)
{
datafile.print(xMPU);
datafile.print(",");
datafile.print(yMPU);
datafile.print(",");
datafile.print(zMPU);
datafile.print(",");
datafile.println(datastring);
datafile.close();

Serial.print(xMPU);
Serial.print(" “);
Serial.print(yMPU);
Serial.print(” ");
Serial.println(zMPU);
}
}