MPU9250 datalogger - issues when using a battery

Hello

I am trying to use an MPU9250 to investigate human motion.

I am using a MPU9250, sd card module to log the data and the Arduino Nano development board.

When I connect a 9V battery i am getting inconsistencies with the datalogging, when i power the system using a usb to the board everything runs smoothly.

the inconsistencies i am experiencing are - the last 5 seconds of data are not being logged when power is removed, upon reset there is no data being logged, sometimes it happens sometimes it doesn’t.

Here is my code

#include "SD.h"                                                                              //Header files
#include "SPI.h"
#include "SparkFunMPU9250-DMP.h"

File logfile;
MPU9250_DMP imu;

int status;
char filename[] = "SENSOR00.csv";                                                            //File name
unsigned long m ;

float ax, ay, az;                                                                            //MPU Data
float gx, gy, gz;
float mx, my, mz;

void setup() {
 Serial.begin(115200);
 initSD();                                                                                  //Initialize SD card
 initMPU();                                                                                 //Initialize MPU9250
}

void loop() {
 if ( imu.dataReady() )                                                                      //If MPU Data available
 {
   imu.update(UPDATE_ACCEL | UPDATE_GYRO | UPDATE_COMPASS);
   ax = imu.calcAccel(imu.ax);                                                               //Record MPU Data
   ay = imu.calcAccel(imu.ay);
   az = imu.calcAccel(imu.az);
   gx = imu.calcGyro(imu.gx);
   gy = imu.calcGyro(imu.gy);
   gz = imu.calcGyro(imu.gz);
   mx = imu.calcMag(imu.mx);
   my = imu.calcMag(imu.my);
   mz = imu.calcMag(imu.mz);
   printData();                                                                               //Print data
   logData();                                                                                 //Log data
 }
}

void initSD() {
 Serial.print("Initializing SD card...");
 pinMode(4, OUTPUT);
 if (!SD.begin(4)) {                                                                           // SD card begin
   Serial.println("Card failed, or not present");
 }
 Serial.println("card initialized.");
 for (uint8_t i = 0; i < 100; i++) {                                                           //Check if file exits and if file does not exist create new file
   filename[6] = i / 10 + '0';
   filename[7] = i % 10 + '0';
   if (! SD.exists(filename)) {
     logfile = SD.open(filename, FILE_WRITE);
     break;
   }
 }

 if (! logfile) {                                                                              //If file creation failed
   Serial.println("couldnt create file");
 }

 Serial.print("Logging to: ");
 Serial.println(filename);

 logfile.println("Time,AX,AY,AZ,GX,GY,GZ,MX,MY,MZ");
 Serial.println("AX\tAY\tAZ\tGX\tGY\tGZ\tMX\tMY\tMZ");
 logfile.close();
}

void initMPU() {
 if (imu.begin() != INV_SUCCESS)                                               //If MPU begins not successfully
 {
   while (1)
   {
     Serial.println("Unable to communicate with MPU-9250");
     Serial.println("Check connections, and try again.");
     Serial.println();
   //  delay(5000);
   }
 }

 imu.setSensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);               //Configure MPU
 imu.setGyroFSR(2000); // Set gyro to 2000 dps
 imu.setAccelFSR(2); // Set accel to +/-2g
 imu.setLPF(5);                                                              // Set LPF corner frequency to 188Hz (Max)
 imu.setSampleRate(10000);                                                      // Set sample rate to 1kHz (Max)
 imu.setCompassSampleRate(10);                                                // Set mag rate to 10Hz (Max)
}

void printData() {
 Serial.print("a/g/m:\t");
 Serial.print(ax); Serial.print("\t");
 Serial.print(ay); Serial.print("\t");
 Serial.print(az); Serial.print("\t");
 Serial.print(gx); Serial.print("\t");
 Serial.print(gy); Serial.print("\t");
 Serial.print(gz); Serial.print("\t");
 Serial.print(mx); Serial.print("\t");
 Serial.print(my); Serial.print("\t");
 Serial.println(mz);
}

void logData() {
 logfile = SD.open(filename, FILE_WRITE);
 m = millis();

 if (logfile) {
   logfile.print(m);
   logfile.print(",");
   logfile.print(ax); logfile.print(",");
   logfile.print(ay); logfile.print(",");
   logfile.print(az); logfile.print(",");
   logfile.print(gx); logfile.print(",");
   logfile.print(gy); logfile.print(",");
   logfile.print(gz); logfile.print(",");
   logfile.print(mx); logfile.print(",");
   logfile.print(my); logfile.print(",");
   logfile.println(mz);
   logfile.close();
 }
 else {
   Serial.println("error opening file");
 }
}

if anyone knows where i am going wrong or has any links that can help me, please comment below.

many thanks

Please edit your post to add code tags, as described in the “How to use this forum” post.

9V block batteries are for smoke alarms, and are unsuitable for powering an Arduino for more than a few minutes.

the last 5 seconds of data are not being logged when power is removed

You need to close the file before power down.

Thank you for your response @jremington, ive editted my orginal post. I have already implemented closing the file in my code as shown in the original post.

Also the arduino nano tech specs say the input voltage can range from 7-12v, my assumption was 9v would do the trick. I guess I'll have to try and get a 12V

9V is fine, the problem is the tiny current capacity of a 9V block battery. A fresh one will work, but not for long.

Instead of a Nano, use a 3.3V Pro Mini and 3xAAA batteries, requiring about 1/10 the power of the Nano. For a really tiny and self contained logger, use the SparkFun OpenLog.

With some work, you might even be able to fit the MPU9250 code into the OpenLog.