Arduino nano BLE - Increase IMU speed in serial with Serial.write

float time1,time2,time3,diff_time;
int16_t AX=0;
int16_t AY=0;
int16_t AZ=0;
int16_t GX=0;
int16_t GY=0;
int16_t GZ=0;

float aX_Old=0;
float aY_Old=0;
float aZ_Old=0;
//float test;
float diff_X=0;
float diff_Y=0;
float diff_Z=0;
//#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>


const float accelerationThreshold = 0.4; // threshold of significant in G's
const int numSamples = 119;

int samplesRead = numSamples;


void setup() {
 
  Serial.begin(115200);
  while (!Serial);

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
  // print the header
  Serial.println("aX,aY,aZ,gX,gY,gZ");
}

void loop() {
  float aX, aY, aZ, gX, gY, gZ;

    
  // wait for significant motion
  while (samplesRead == numSamples) {
    if (IMU.accelerationAvailable()) {
      // read the acceleration data
      IMU.readAcceleration(aX, aY, aZ);
      //IMU.readAcceleration(gX, gY, gZ);
 
      diff_X=fabs(aX-aX_Old);
      diff_Y=fabs(aY-aY_Old);
      diff_Z=fabs(aZ-aZ_Old);

      // check if it's above the threshold
      if (diff_X >= accelerationThreshold  &&  diff_Y >= accelerationThreshold && diff_Z >= accelerationThreshold ) {
        // reset the sample read count
        samplesRead = 0;
        break;
      }
    }
  }

  // check if the all the required samples have been read since
  // the last time the significant motion was detected

  while (samplesRead < numSamples) {
    
    // check if both new acceleration and gyroscope data is
    // available && IMU.gyroscopeAvailable()
          time1=millis();

    if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()) {
      // read the acceleration and gyroscope data
      IMU.readAcceleration(aX, aY,aZ);
      IMU.readGyroscope(gX, gY, gZ);
      samplesRead++;


      AX=(int16_t)(aX*100);
      AY=(int16_t)(aY*100);
      AZ=(int16_t)(aZ*100);
      GX=(int16_t)(gX*100);
      GY=(int16_t)(gY*100);
      GZ=(int16_t)(gZ*100);

    Serial.println(aX);(uint8_t*)

      // print the data in CSV format*/

        Serial.write((uint8_t)(AX >> 8)); Serial.write((uint8_t)(AX & 0xFF));
        Serial.write((uint8_t)(AY >> 8)); Serial.write((uint8_t)(AY & 0xFF));
        Serial.write((uint8_t)(AZ >> 8)); Serial.write((uint8_t)(AZ & 0xFF));
        Serial.write((uint8_t)(GX >> 8)); Serial.write((uint8_t)(GX & 0xFF));
        Serial.write((uint8_t)(GY >> 8)); Serial.write((uint8_t)(GY & 0xFF));
        Serial.write((uint8_t)(GZ >> 8)); Serial.write((uint8_t)(GZ & 0xFF));
        

    /*
      Serial.print(aX, 3);
      Serial.print(',');
      Serial.print(aY, 3);
      Serial.print(',');
      Serial.print(aZ, 3);
      Serial.print(',');
      Serial.print(gX, 3);
      Serial.print(',');
      Serial.print(gY, 3);
      Serial.print(',');
      Serial.println(gZ, 3);*/

  
      if (samplesRead == numSamples) {
        // add an empty line if it's the last sample
        Serial.println();
        time2=millis();
        diff_time=time2-time1;
        Serial.println(diff_time);
      }
    }      

  }


   aX_Old=aX;
   aY_Old=aY;
   aZ_Old=aZ;

}

Hi there, I am trying to increase the display of my IMU values through the serial port. The Serial.print function limits me.
I changed the header files for a 952Hz sampling rate of my gyroscope and accelerator.

Now i want to use Serial.write to increase the serial printing, unfortunatly the data are not readable in the serial port. I need to display the data.

Do you know how I could read my data in order to display it “-0.349,-0.461,1.442,9.399,-2.808,-6.226”?

Thank you for your help :slight_smile:

If you're using serial monitor, you're out of luck with the write method.

First to try would be a higher baudrate; I'm not familiar with your board so not sure what is supported. And if you board has native usb (can act like a HID), it does not matter.

Next one is to use a terminal program that can interpret data as an integer; I think realterm can do it, but not sure.

Last option is to write your own pc software that does the above.

//Edit
Just checked, Realterm can interpret two bytes as and int.

Note that I don't have experience with imu. As I understand it you take 952 samples per second and transmit them over the serial port at 115200 baud. Each sample contains 6 values.

When you print the values (comma separated), each sample (based on your example) contains 36+5 bytes. At 115200, it takes 3.6 milliseconds to transfer and hence you can transfer only 1 / 3.6e-3 = 280 samples per second.

With your int approach using write, you're transmitting 12 bytes for a sample. Same calculation brings you to 2.1 milliseconds to transfer the sample and hence you can transfer 1 / 2.1e-3 = 480 samples per second.

Your only way is to increase the baudrate.

Thank you for your reply.
You are right regarding the time and number of sample per seconds i checked that in my code.

I increased the baudrate but it doesn't seems to change the speed of my acquisition.

I beleive indeed having a terminal that can read and interprate the data i send in binary is a good option.

I found a solution :

I do a buffer on each value that i fill in a loop. Then when the loop is over i just print it in my serial with Serial.print. It works.

Thanks for your help i will try your terminal option and let you know ;).

But for now my Arduino BLE sense is dead .... (i implement FFT and now it is not recognized on serial port anymore ...)