Trying to get Accelerometer and Gyroscope Data for my gesture recognition project using Arduino Uno and MPU9250

I am trying to built my gesture recognition project using Arduino UNO and MPU9250 sensor. I am trying to acquire the data of Accelerometer and Gyroscope in a format so that I could convert the output data into a .csv file for putting the data into the machine learning model for train the particular TinyML model. But ever after using the adequate library I am not able to Aquire the data from the sensor. I have used the following code and used the MPU9250 library by hideakitai

 #include <Wire.h>

#include "MPU9250.h"
#include <Adafruit_Sensor.h>

/* Private define ------------------------------------------------------------*/
#define NUM_SAMPLES     50
#define NUM_GESTURES    30
#define G               9.80665f
#define ACC_THRESHOLD   (2.5f*G)

#define GESTURE_0       0
#define GESTURE_1       1
#define GESTURE_TARGET  GESTURE_0
//#define GESTURE_TARGET  GESTURE_1

/* Private variables ---------------------------------------------------------*/
int samplesRead   = NUM_SAMPLES;
int gesturesRead  = 0;

MPU9250 IMU(Wire,0x68);
int status;

void setup() {
  // init serial port
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }

  // init IMU sensor
  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while (1) {
      delay(10);
    }
  }
  
  // configure IMU sensor
  IMU.setAccelRange(MPU9250::ACCEL_RANGE_8G);
  IMU.setGyroRange(MPU9250::GYRO_RANGE_500DPS);
  IMU.setDlpfBandwidth(MPU9250::DLPF_BANDWIDTH_20HZ);
  IMU.setSrd(19);

  // print the CSV header (ax0,ay0,az0,...,gx49,gy49,gz49,target)
  for (int i=0; i<NUM_SAMPLES; i++) {
    Serial.print("aX");
    Serial.print(i);
    Serial.print(",aY");
    Serial.print(i);
    Serial.print(",aZ");
    Serial.print(i);
    Serial.print(",gX");
    Serial.print(i);
    Serial.print(",gY");
    Serial.print(i);
    Serial.print(",gZ");
    Serial.print(i);
    Serial.print(",");
  }
  Serial.println("target");
}

void loop() {
  
  while(gesturesRead < NUM_GESTURES) {
    // wait for significant motion
    while (samplesRead == NUM_SAMPLES) {
      // read the acceleration data
      IMU.readSensor();
      
      // sum up the absolutes
      float aSum = fabs(IMU.getAccelX_mss()) + fabs(IMU.getAccelY_mss()) + fabs(IMU.getAccelZ_mss());
      
      // check if it's above the threshold
      if (aSum >= ACC_THRESHOLD) {
        // reset the sample read count
        samplesRead = 0;
        break;
      }
    }
  
    // read samples of the detected motion
    while (samplesRead < NUM_SAMPLES) {
        // read the acceleration and gyroscope data
        IMU.readSensor();
  
        samplesRead++;
  
        // print the sensor data in CSV format
        Serial.print(IMU.getAccelX_mss(), 6);
        Serial.print(',');
        Serial.print(IMU.getAccelY_mss(), 6);
        Serial.print(',');
        Serial.print(IMU.getAccelZ_mss(), 6);
        Serial.print(',');
        Serial.print(IMU.getGyroX_rads(), 6);
        Serial.print(',');
        Serial.print(IMU.getGyroY_rads(), 6);
        Serial.print(',');
        Serial.print(IMU.getGyroZ_rads(), 6);
        Serial.print(',');

        // print target at the end of samples acquisition
        if (samplesRead == NUM_SAMPLES) {
          Serial.println(GESTURE_TARGET);
        }
        
        delay(10);
    }
    gesturesRead++;
  }
}

Please feel to give to suggestion to change the code.

Welcome to the forum, and thanks for posting your code correctly!

Please describe what you expect to happen, and what happens instead.

Have you used the I2C address scanner to verify communications, and correct I2C address?

Does the MPU9250 module have the required bidirectional 5V to 3.3V logic level shifters?

Thank you for your response.

The output of the code is expected to be like the aX,aY,aZ,gX,gY,gZ will be there in the rows for 50 samples and below that well get the Accelerometer and Gyro data in the columns. 1st we'll enable the Gesture_Target Gesture_0 for taking the first 30 data set and then well do the same for the Gesture_Target Gesture_1 by disabling the first gesture.

Yes I have verified the I2C scanner and the communication is well established and the addresses are also correct because I can extract the data of both the Gyro and Accelerometer data by using the example code of the MPU9250 Library.

Yes, It have the required bidirectional bidirectional 5V to 3.3V logic level shifters.

Please feel free to reply and ask anything from my side.

What is the actual output?

Its only coming like this:

aX0,aY0,aZ0,gX0,gY0,gZ0,aX1,aY1,aZ1,gX1,gY1,gZ1,aX2,aY2,aZ2,gX2,gY2,gZ2,aX3,aY3,aZ3,gX3,gY3,gZ3,aX4,aY4,aZ4,gX4,gY4,gZ4,aX5,aY5,aZ5,gX5,gY5,gZ5,aX6,aY6,aZ6,gX6,gY6,gZ6,aX7,aY7,aZ7,gX7,gY7,gZ7,aX8,aY8,aZ8,gX8,gY8,gZ8,aX9,aY9,aZ9,gX9,gY9,gZ9,aX10,aY10,aZ10,gX10,gY10,gZ10,aX11,aY11,aZ11,gX11,gY11,gZ11,aX12,aY12,aZ12,gX12,gY12,gZ12,aX13,aY13,aZ13,gX13,gY13,gZ13,aX14,aY14,aZ14,gX14,gY14,gZ14,aX15,aY15,aZ15,gX15,gY15,gZ15,aX16,aY16,aZ16,gX16,gY16,gZ16,aX17,aY17,aZ17,gX17,gY17,gZ17,aX18,aY18,aZ18,gX18,gY18,gZ18,aX19,aY19,aZ19,gX19,gY19,gZ19,aX20,aY20,aZ20,gX20,gY20,gZ20,aX21,aY21,aZ21,gX21,gY21,gZ21,aX22,aY22,aZ22,gX22,gY22,gZ22,aX23,aY23,aZ23,gX23,gY23,gZ23,aX24,aY24,aZ24,gX24,gY24,gZ24,aX25,aY25,aZ25,gX25,gY25,gZ25,aX26,aY26,aZ26,gX26,gY26,gZ26,aX27,aY27,aZ27,gX27,gY27,gZ27,aX28,aY28,aZ28,gX28,gY28,gZ28,aX29,aY29,aZ29,gX29,gY29,gZ29,aX30,aY30,aZ30,gX30,gY30,gZ30,aX31,aY31,aZ31,gX31,gY31,gZ31,aX32,aY32,aZ32,gX32,gY32,gZ32,aX33,aY33,aZ33,gX33,gY33,gZ33,aX34,aY34,aZ34,gX34,gY34,gZ34,aX35,aY35,aZ35,gX35,gY35,gZ35,aX36,aY36,aZ36,gX36,gY36,gZ36,aX37,aY37,aZ37,gX37,gY37,gZ37,aX38,aY38,aZ38,gX38,gY38,gZ38,aX39,aY39,aZ39,gX39,gY39,gZ39,aX40,aY40,aZ40,gX40,gY40,gZ40,aX41,aY41,aZ41,gX41,gY41,gZ41,aX42,aY42,aZ42,gX42,gY42,gZ42,aX43,aY43,aZ43,gX43,gY43,gZ43,aX44,aY44,aZ44,gX44,gY44,gZ44,aX45,aY45,aZ45,gX45,gY45,gZ45,aX46,aY46,aZ46,gX46,gY46,gZ46,aX47,aY47,aZ47,gX47,gY47,gZ47,aX48,aY48,aZ48,gX48,gY48,gZ48,aX49,aY49,aZ49,gX49,gY49,gZ49,target

There is no Accelerometer and Gyro data coming after that.

My guess is that the code is stuck in this loop. Remove it, for testing purposes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.