Go Down

Topic: LSM9DS1: how to change sampling frequency and acceleration ranges (Read 2991 times) previous topic - next topic

riccardo_nardicchia

Hello all,
I have an Arduino Nano 33 BLE Sense and I need to use its accelerometer for a project: default range for acceleration is +/-4g, sampling rate is 119 Hz.

How can I increase the sampling frequency and the acceleration max range of LSM9DS1 IMU?

Looking at the official library "Arduino_LSM9DS1" (LSM9DS1.h) it's not clear how to change this parameters.

I'm not sure if and how I can use other LSM9DS1 libraries...

Thanks for support,

Regards

Riccardo

Biomechanist

Hi Riccardo.

Did you find any information to answer your question? I am wondering the same thing. If you take a look at the Sparkfun library for the LSM9DS1, you'll see that it has support to set sampling rates (and a lot more), however, this is for the older Arduino Nano or Uno boards and won't compile. It appears to be an issue with how the SPI settings need to be handled now.

I have not dug into github to see if there are any requests to update it yet. I'm a little out of my depth here.


riccardo_nardicchia

Hi Biomechanist,

no sorry, I didn't find any solution. I also checked all the other libraries for LSM9DS1 but I'm not able to get them working with the I2C communication of my Nano 33 BLE Sense. I'm quite newbie in this field :-(.

We'll see if someone more expert can support, it's a shame to have such powerful sensor and not be able to use it at full performance.

Biomechanist

Greetings again Riccardo.

I was able to change the settings, though not in a way that's elegant (for now). I will explain how I did it below, but first I'll give a short background to why this is not elegant...

If you locate your LSM9DS1 library (I'm assuming it's Arduino_LSM9DS1, not Sparkfun or some other version), then go into the src directory and open the file LSM9DS1.cpp in a code editor. You can search for the line:

writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL, 0x70); // 119 Hz, 4G


This line (and other similar ones which are all helpfully commented in the code) establishes all of the IMU the settings. I'm very new to everything Arduino, but I find this to be not very user friendly. No offense to the developers of the source files (who without I would be totally lost), but hardcoding settings like this doesn't make a lot of sense. The Sparkfun library is MUCH better developed and allows the user to dynamically adjust settings, which is exactly how things should be... however that library appears to be out of date for the new boards (I think a few small changes will fix it).


HERE's WHAT YOU NEED TO DO:
1. Get yourself a copy of the LSM9DS1 datasheet...
https://cdn.sparkfun.com/assets/learn_tutorials/3/7/3/LSM9DS1_Datasheet.pdf

2. Open the datasheet and go to the Register Mapping section, then scroll down to the register descriptions.

3. The accelerometer register you're looking for is called CTRL_REG6_XL

4. The settings are listed in the datasheet (tables 67, 68). Keep in mind that each register is an 8-bit word, each bit is either 1 or 0. The datasheet explains what each bit means for a given register. The settings hardcoded in the Arduino_LSM9DS1.cpp are: 011 10 0 00

5. If you find a binary to hex converter online, you'll see that value is 0x70 in hex (112 decimal). So, just figure out what the binary value of your settings is, convert to hex, then you can edit Arduino_LSM9DS1.cpp and give it your value instead of what's there.


As a final note, I will say that I'm unsure if the gyro also needs to be updated for this to work as intended. This setting may only work when the gyro is disabled, as you'll notice when you look at the documentation. So, making changes to CTRL_REG1_G may also be necessary. I hope this helps you and please report back with what you learn.

Sacha260194

Hey everybody,

Has anyone found a solution to increase the sample frequency of the accelerometer ?

Thank you,


femmeverbeek

Quote
Hey everybody,

Has anyone found a solution to increase the sample frequency of the accelerometer ?

Thank you,
Like Biomechanist pointed out, you need to edit a line in the .cpp file of the library.
See page 52 in the datasheet
You need to understand how 8 bits make up a byte. The three left most bits (= most significant) control the output data rate.


writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL, .....);
the current value at the  .... is 0x70     : 119 hz
change to
0x30    : 10 hz
0x50    :50 hz
0x90    : 238 hz
0xB0    : 476 hz
0xD0    : 952 hz

The same line can be used to change the full scale setting of the accellerometer.

Quote
As a final note, I will say that I'm unsure if the gyro also needs to be updated for this to work as intended. This setting may only work when the gyro is disabled, as you'll notice when you look at the documentation. So, making changes to CTRL_REG1_G may also be necessary. I hope this helps you and please report back with what you learn.
I made new library functions for changing the full scale setting of the gyrosope in the program. they work well and I was surprised to see that setting 2, which should not be available, nicely produced 1000 dps after all.
If you want I can post the code changes here.

mathlete

For those who don't want to crack open the datasheet or convert hex to binary and binary to hex, here are some example settings that MAX out the data rates on the sensors:

Code: [Select]
//gyroscope settings
 writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G, 0xD8); // 952 Hz, 2000 dps range, 16 Hz BW

 //accelerometer settings
 writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL, 0xD0); // 952 Hz, 4G range

 //magnetometer settings
 //writeRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG1_M, 0xB4); // Temperature compensation enable, medium performance, 20 Hz
 //writeRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG1_M, 0xBC); // Temperature compensation enable, medium performance, 80 Hz
 writeRegister(LSM9DS1_ADDRESS_M, LSM9DS1_CTRL_REG1_M, 0xFC); // Temperature compensation enable, ultrahigh performance, 80 Hz

mathlete


femmeverbeek

I wrote a (rather large) update on the LSM9DS1 library.

https://github.com/FemmeVerbeek/Arduino_LSM9DS1

To change and read the sample rate  (ODR= output data rate)

int setAccelODR(uint8_t range); // Sample Rate 0:off, 1:10Hz, 2:50Hz, 3:119Hz, 4:238Hz, 5:476Hz, 6:952Hz  Automatic BW setting
int setGyroODR(uint8_t range); //Sampling Rate 0:off, 1:10Hz, 2:50Hz, 3:119Hz, 4:238Hz, 5:476Hz, 6:952Hz
int   setMagnetODR(uint8_t range); // Sampling rate (0..7) corresponds to {0.625,1.25,2.5,5.0,10.0,20.0,40.0,80.0}Hz


float getAccelODR(); // Measured Sample Rate of the sensor.
float getGyroODR(); // Measured Sample rate of the sensor.
float getMagnetODR(); // Sampling rate of the sensor in Hz.

Accel and Gyro share their ODR, so changing one also changes the other.
setGyroODR(0); switches off the Gyro. The Accel stays on, but it's ODR value may change significantly.
setAccelODR(0); switches off both saving power.
There's no Gyro only mode.

Since the ODR is (or should be) in the orientation tracking calculation of the gyroscope, its absolute value is important.
The get... procedures of accel and gyro return a value that was measured during the set.... procedure.
It's a bit disappointing that the 952Hz setting does not work.


rssr25

https://github.com/kriswiner/LSM9DS1/blob/master/LSM9DS1_MS5611_BasicAHRS_t3.ino A more thorough example of manipulating the registers on LSM9DS1 IMU as found on the Nano 33 BLE Sense.
Like Biomechanist pointed out, you need to edit a line in the .cpp file of the library.
See page 52 in the datasheet
You need to understand how 8 bits make up a byte. The three left most bits (= most significant) control the output data rate.


writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL, .....);
the current value at the  .... is 0x70     : 119 hz
change to
0x30    : 10 hz
0x50    :50 hz
0x90    : 238 hz
0xB0    : 476 hz
0xD0    : 952 hz

The same line can be used to change the full scale setting of the accellerometer.

I made new library functions for changing the full scale setting of the gyrosope in the program. they work well and I was surprised to see that setting 2, which should not be available, nicely produced 1000 dps after all.
If you want I can post the code changes here.

When I wanted to change the Accelerometer sample rate on my Nano 33 BLE Sense from 119Hz to 50Hz, I had to also manipulate the gyroscope.

I have put: 0x50 in writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL, 0x50);
I have put: 0x10 (power off) in writeRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG1_G, 0x10);

The serial output showed accelerometer sample rate to be 50Hz.

Also Installing the LSM9DS1 from Arduino Libraries showed that the return on float LSM9DS1Class::accelerationSampleRate() function is hardcoded to 119Hz. Use
return readRegister(LSM9DS1_ADDRESS, LSM9DS1_CTRL_REG6_XL); to return the register value in decimal.

Then accelerationSampleRate call will show the required 50Hz sample rate in decimal which should be 80.

femmeverbeek

I advice you to use the update for the LSM9dDS1 library I wrote.

https://github.com/FemmeVerbeek/Arduino_LSM9DS1

Use the functions 

IMU.setGyroODR(0);  // switch off gyro
IMU.setAccelODR(2);  // 50 Hz ODR


The functions getGyroODR and getAccelODR return the actual measured value of the ODR.

It took me quite some time to find out all about the registers and how they really work. setting 6= 952 Hz turns out not to be working

There are three operational modes
0 = gyro and accelerometer off
1 = accelerometer only :
2 = accelerometer and gyro both working and sharing their ODR

There is no mode with gyro only

In mode 1 the setting has to be written in CTRL_REG6_XL,  the setting in CTRL_REG6_XL = 0
In mode 2 The gyro register CTRL_REG1_G controlls the shared ODR for both gyro and accelerometer


clrw97

I know it is a bit late to reply, but FYI:
Arduino_LSM9DS1@Github

As you can see in line 94 LSM9DS1.h (line 241 LSM9DS1.cpp respectively), there is a function to set the g value. I have not tested it, but I will soon. Also, there are some other functions that seem to help if you want to change the parameters of the IMU.

Go Up