Hi everyone,
With regards to the Adafruit LSM9DS1 breakout:
[adafruit tutorial]Overview | Adafruit LSM9DS1 Accelerometer + Gyro + Magnetometer 9-DOF Breakout | Adafruit Learning System]adafruit tutorial](Overview | Adafruit LSM9DS1 Accelerometer + Gyro + Magnetometer 9-DOF Breakout | Adafruit Learning System)
And using the Library:
[GitHub - adafruit/Adafruit_LSM9DS1: lib for 9dof]GitHub - adafruit/Adafruit_LSM9DS1: lib for 9dof]GitHub - adafruit/Adafruit_LSM9DS1: lib for 9dof](GitHub - adafruit/Adafruit_LSM9DS1: lib for 9dof)
I am trying to increase the sample rate, or speed of this chip.
With the adafruit library, you need to request accelerometer, gyro, magnometer 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:
#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 initialise 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);
}
LSM9DS1, good choice.
I found that the LSM9DS1 Adafruit Library does not work with an ESP32, I wrote my own. You can edit the AdaFruit Library .h and . cpp files to just give you just the data you want to read. If you just want accelerometer data, comment out all the Magnetometer setup and read functions, in the read function change the starting address to just read the accel registers and reduce the array size. Remember, the LSMDS1 always returns a status byte. The Status byte is the first returned byte in the array ( ReturnArray[0]) so you'll want to set your array size to one more then the amount of data you want returned. The Magnetometer is on a different chip that requires more SPI code to process, by commenting out the magnetometer set up and read code you will realize a speed increase.
You can set the LSM9DS1 to send an interrupt when it has new data to be read and trigger a data read. Works quite well.
How fast do you want to read accelerometer data? I got my SPI clock rate set at 5000000Hz. The max SPI clock frequency of the LSM9DS1 is 10MHz.
Thanks for the info!
I basically want it as fast as possible. The only thing this arduino is doing is reading the LSM and printing the values out on the serial port, and I feel as though I am missing some of the peaks of the vibrations due to it not sampling/reading fast enough.
Do you know, is it possible to change the SPI clock rate on a sparkfun Pro-Micro (5v variant)?
Are you asking if you can modify the Pro-Micro board to get a different SPI clock rate? The SPI clock rate to use can be set in software.
You can look at the Adafruit LSM9DS1 library to see what is being used. The LSM9DS1 is not guaranteed to work above 10Mhz.
You will get the fastest read results by using an interrupt, to read the LSM9DS1, that is generated each and every time the LSM9DS1 has new data.
Do you mean the physical interrupt pins?
Yes.
On the LSM9DS1 module are pins INT2, INT1, and INTM; see LSMDS1 register datasheet https://www.st.com/resource/en/datasheet/lsm9ds1.pdf, 3.5 (Accelerometer and gyroscope FIFO ), 7.3, 7.4, 7.5 7.5, 7.6, and a few others for info on programming the LSM9DS1 for interrupt use.
You can, also, define one of the pins on your uController to accept and respond to the interrupt, as well as write an ISR to trigger data reads.
Hello @Idahowalker,
I am interested in knowing what is the max pooling rate you are able to do. Could you get this info from the sensor timestamp?
It would be nice to know how fast your board (which board by the way) can pool the data via SPI.
@mgale31, @Idahowalker
Also I curious about this choice of IMU. What lead you to choose this sensor?
Thanks & Regards
vcmorini:
@mgale31, @Idahowalker
Also I curious about this choice of IMU. What lead you to choose this sensor?
My choice of this module is that the magnetometer uses a SPI buss that can be accessed separately and independently over the SPI buss.
I am using an ESP32 with freeRTOS and the ESP SPI API, whiles a bit more complicated over other libraries, I can take advantage of some of the ESP32 SPI API features using freeRTOS queues to do background sending and receiving (duplex) of the data.
As an example of use, I can send a SPI request for a data read to each device and then run some other function or set of functions and when I am ready I can read the SPI queue(s). The data would be waiting for me instead of me waiting for the data.
I have not benchmarked the LSM9DS1 for speed of operation.
I chose this sensor because of it's on an adafruit breakout, and has libraries already written for it for both arduino and python. I also chose it because of its g sensitivity when compared to other breakouts