0

With regards to the Adafruit LSM9DS1 breakout: enter link description here

And using the Library: enter link description here

I am trying to increase the sample rate, or speed of this chip. I am using a Sparkfun Pro Micro 5V

With the adafruit library, you need to request accelerometer, gyro, magnetometer and temperature at once, and all I need is the accelerometer values. I am hoping by figuring out how to request only the accel values it will speed up the loop. But also, there must be another setting that could speed up the sample rate?

Any help would be appreciated!

my code currently:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM9DS1.h>
#include <Adafruit_Sensor.h>  // not used in this demo but required!
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();
#define LSM9DS1_SCK A5
#define LSM9DS1_MISO 12
#define LSM9DS1_MOSI A4
#define LSM9DS1_XGCS 6
#define LSM9DS1_MCS 5

void setupSensor()
{
  // 1.) Set the accelerometer range
  lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_2G);
  //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_4G);
  //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_8G);
  //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_16G);

  // 2.) Set the magnetometer sensitivity
  lsm.setupMag(lsm.LSM9DS1_MAGGAIN_4GAUSS);
  //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_8GAUSS);
  //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_12GAUSS);
  //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_16GAUSS);

  // 3.) Setup the gyroscope
  lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_245DPS);
  //lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_500DPS);
  //lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_2000DPS);
}

double accelX = 0;
double accelY = 0;
double accelZ = 0;
double rms = 0;

void setup() 
{
  Serial.begin(115200);
  // Try to initialize and warn if we couldn't detect the chip
  if (!lsm.begin())
  {
    Serial.println("Oops ... unable to initialize the LSM9DS1. Check your 
wiring!");
    while (1);
  }
  Serial.println("Found LSM9DS1 9DOF");
  setupSensor();
  delay(5000);//program stabilization
}

void loop() 
{
  lsm.read();
  sensors_event_t a, m, g, temp;
  lsm.getEvent(&a, &m, &g, &temp); 
  accelX = a.acceleration.x;
  accelY = a.acceleration.y;
  accelZ = a.acceleration.z;
  rms = (accelX*accelX)+(accelY*accelY)+(accelZ*accelZ);
  Serial.println(rms);
}

2 Answers 2

1

Check the header of the library,

you will find readAccel readMag readGyro and readTemp in separate public functions and looking at the implementation of read it just calls those 4 in sequence. readAccel will update the accelData member field.

So you can just call lsm.readAccel() in the loop and then grab lsm.accelData directly and ignore the rest.

void loop() 
{
  lsm.readAccel();
  accelX = lsm.accelData.x;
  accelY = lsm.accelData.y;
  accelZ = lsm.accelData.z;
  rms = (accelX*accelX)+(accelY*accelY)+(accelZ*accelZ);
  Serial.println(rms);
}
4
  • Thanks! I'll try this out, hopefully it will speed up the sampling a bit
    – MattG
    Commented Jul 9, 2019 at 19:52
  • This worked, the sample rate is significantly faster now that I implemented the code above from @ratchet freak
    – MattG
    Commented Jul 10, 2019 at 13:04
  • @MattG having looked at the code in a bit more detail it looked like getEvent also did a read(). So your original code was doing a full read twice and only using the data for half the samples actually read. Commented Jul 10, 2019 at 13:08
  • that makes sense, it is significantly faster now that I implemented your suggestions. Thanks again.
    – MattG
    Commented Jul 11, 2019 at 15:34
1

Here is a link to the LSM9DS1 datasheet. In Chapter 3 it describes how to put the device in Accelerometer-only mode. At a quick scan of it, I believe there was timing information also, but at least knowing how to do it, you could then make your own timing measurements.

3
  • I see you can put the device into accel mode only, but unfortunately I don't understand how to actually implement the instructions. Is this something that would be changed in the library files?
    – MattG
    Commented Jul 9, 2019 at 19:53
  • In the Adafruit library's Adafruit_LSM9DS1.h at line 243 are a handful of functions to read individual sensors (Sensor& getAccel ( void ) { return _accelSensor; }, for instance). To get the timing benefit, you may need to put the device in accel-only mode, which, yes, will need some study of the library .cpp file to figure out how to do it or where to add the capability. I also see that there are some data-rate selections available. But that's about all I know of this device. Any more will need looking at the library code and the datasheet.
    – JRobert
    Commented Jul 10, 2019 at 12:11
  • ok, thanks for the input and help on this @jrobert !
    – MattG
    Commented Jul 11, 2019 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.