How to change sample rate for a sensor (LSM6DSOX) with Arduino Due code

Hello Forum

I have bought a couple of Adafruit LSM6DSOX sensors, code and data taken from here: Arduino | LSM6DSOX, ISM330DHC, & LSM6DSO32 6 DoF IMUs | Adafruit Learning System

From the code, I am not able to see how I can change the sample rate. It samples by default with 104 Hz, and I want it to sample with either 3.3 kHz or 6.6 kHz, which should be possible.

I'm quite new, but my guess is that the choosing part belongs to this part of the code:

// sox.setAccelDataRate(LSM6DS_RATE_12_5_HZ);
  Serial.print("Accelerometer data rate set to: ");
  switch (sox.getAccelDataRate()) {
    case LSM6DS_RATE_SHUTDOWN:
      Serial.println("0 Hz");
      break;
    case LSM6DS_RATE_12_5_HZ:
      Serial.println("12.5 Hz");
      break;
    case LSM6DS_RATE_26_HZ:
      Serial.println("26 Hz");
      break;
    case LSM6DS_RATE_52_HZ:
      Serial.println("52 Hz");
      break;
    case LSM6DS_RATE_104_HZ:
      Serial.println("104 Hz");
      break;
    case LSM6DS_RATE_208_HZ:
      Serial.println("208 Hz");
      break;
    case LSM6DS_RATE_416_HZ:
      Serial.println("416 Hz");
      break;
    case LSM6DS_RATE_833_HZ:
      Serial.println("833 Hz");
      break;
    case LSM6DS_RATE_1_66K_HZ:
      Serial.println("1.66 KHz");
      break;
    case LSM6DS_RATE_3_33K_HZ:
      Serial.println("3.33 KHz");
      break;
    case LSM6DS_RATE_6_66K_HZ:
      Serial.println("6.66 KHz");
      break;
  }

My Arduino code is this:

// Basic demo for accelerometer & gyro readings from Adafruit
// LSM6DSOX sensor

#include <Adafruit_LSM6DSOX.h>

// For SPI mode, we need a CS pin
#define LSM_CS 10
// For software-SPI mode we need SCK/MOSI/MISO pins
#define LSM_SCK 13
#define LSM_MISO 12
#define LSM_MOSI 11

Adafruit_LSM6DSOX sox;
void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit LSM6DSOX test!");

  if (!sox.begin_I2C()) {
    // if (!sox.begin_SPI(LSM_CS)) {
    // if (!sox.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) {
    // Serial.println("Failed to find LSM6DSOX chip");
    while (1) {
      delay(10);
    }
  }

  Serial.println("LSM6DSOX Found!");

  // sox.setAccelRange(LSM6DS_ACCEL_RANGE_2_G);
  Serial.print("Accelerometer range set to: ");
  switch (sox.getAccelRange()) {
    case LSM6DS_ACCEL_RANGE_2_G:
      Serial.println("+-2G");
      break;
    case LSM6DS_ACCEL_RANGE_4_G:
      Serial.println("+-4G");
      break;
    case LSM6DS_ACCEL_RANGE_8_G:
      Serial.println("+-8G");
      break;
    case LSM6DS_ACCEL_RANGE_16_G:
      Serial.println("+-16G");
      break;
  }

  // sox.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS );
  Serial.print("Gyro range set to: ");
  switch (sox.getGyroRange()) {
    case LSM6DS_GYRO_RANGE_125_DPS:
      Serial.println("125 degrees/s");
      break;
    case LSM6DS_GYRO_RANGE_250_DPS:
      Serial.println("250 degrees/s");
      break;
    case LSM6DS_GYRO_RANGE_500_DPS:
      Serial.println("500 degrees/s");
      break;
    case LSM6DS_GYRO_RANGE_1000_DPS:
      Serial.println("1000 degrees/s");
      break;
    case LSM6DS_GYRO_RANGE_2000_DPS:
      Serial.println("2000 degrees/s");
      break;
    case ISM330DHCX_GYRO_RANGE_4000_DPS:
      break; // unsupported range for the DSOX
  }

  // sox.setAccelDataRate(LSM6DS_RATE_12_5_HZ);
  Serial.print("Accelerometer data rate set to: ");
  switch (sox.getAccelDataRate()) {
    case LSM6DS_RATE_SHUTDOWN:
      Serial.println("0 Hz");
      break;
    case LSM6DS_RATE_12_5_HZ:
      Serial.println("12.5 Hz");
      break;
    case LSM6DS_RATE_26_HZ:
      Serial.println("26 Hz");
      break;
    case LSM6DS_RATE_52_HZ:
      Serial.println("52 Hz");
      break;
    case LSM6DS_RATE_104_HZ:
      Serial.println("104 Hz");
      break;
    case LSM6DS_RATE_208_HZ:
      Serial.println("208 Hz");
      break;
    case LSM6DS_RATE_416_HZ:
      Serial.println("416 Hz");
      break;
    case LSM6DS_RATE_833_HZ:
      Serial.println("833 Hz");
      break;
    case LSM6DS_RATE_1_66K_HZ:
      Serial.println("1.66 KHz");
      break;
    case LSM6DS_RATE_3_33K_HZ:
      Serial.println("3.33 KHz");
      break;
    case LSM6DS_RATE_6_66K_HZ:
      Serial.println("6.66 KHz");
      break;
  }

  // sox.setGyroDataRate(LSM6DS_RATE_12_5_HZ);
  Serial.print("Gyro data rate set to: ");
  switch (sox.getGyroDataRate()) {
    case LSM6DS_RATE_SHUTDOWN:
      Serial.println("0 Hz");
      break;
    case LSM6DS_RATE_12_5_HZ:
      Serial.println("12.5 Hz");
      break;
    case LSM6DS_RATE_26_HZ:
      Serial.println("26 Hz");
      break;
    case LSM6DS_RATE_52_HZ:
      Serial.println("52 Hz");
      break;
    case LSM6DS_RATE_104_HZ:
      Serial.println("104 Hz");
      break;
    case LSM6DS_RATE_208_HZ:
      Serial.println("208 Hz");
      break;
    case LSM6DS_RATE_416_HZ:
      Serial.println("416 Hz");
      break;
    case LSM6DS_RATE_833_HZ:
      Serial.println("833 Hz");
      break;
    case LSM6DS_RATE_1_66K_HZ:
      Serial.println("1.66 KHz");
      break;
    case LSM6DS_RATE_3_33K_HZ:
      Serial.println("3.33 KHz");
      break;
    case LSM6DS_RATE_6_66K_HZ:
      Serial.println("6.66 KHz");
      break;
  }
}

void loop() {

  //  /* Get a new normalized sensor event */
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  sox.getEvent(&accel, &gyro, &temp);

 // Serial.print("\t\tTemperature ");
 // Serial.print(temp.temperature);
 // Serial.println(" deg C");

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("\t\tAccel X: ");
  Serial.print(accel.acceleration.x);
  Serial.print(" \tY: ");
  Serial.print(accel.acceleration.y);
  Serial.print(" \tZ: ");
  Serial.print(accel.acceleration.z);
  Serial.println(" m/s^2 ");

  /* Display the results (rotation is measured in rad/s) */
 // Serial.print("\t\tGyro X: ");
  //Serial.print(gyro.gyro.x);
 // Serial.print(" \tY: ");
 // Serial.print(gyro.gyro.y);
 // Serial.print(" \tZ: ");
//  Serial.print(gyro.gyro.z);
 // Serial.println(" radians/s ");
 // Serial.println();

  delay(100);

  //  // serial plotter friendly format

  //  Serial.print(temp.temperature);
  //  Serial.print(",");

  // Serial.print(accel.acceleration.x);
  //  Serial.print(","); Serial.print(accel.acceleration.y);
  //  Serial.print(","); Serial.print(accel.acceleration.z);
  //  Serial.print(",");

  // Serial.print(gyro.gyro.x);
  // Serial.print(","); Serial.print(gyro.gyro.y);
  // Serial.print(","); Serial.print(gyro.gyro.z);
  // Serial.println();
  //  delayMicroseconds(10000);
}

Hopefully this is a easy fix.

serialmonitor.PNG

serialmonitor.PNG

Firstly, you should look over your sensor datasheet (Adafruit LSM6DSOX) and see what's the maximum permitted sample rate.

Next, take into account that any Serial.println() in loop() takes several tens of us. If you really need to see your sensor outputs on Serial monitor, you had better log into a buffer a serie of results obtained througout a precise sample frequency, AND after e.g. n results, output the buffer on Serial monitor.

To sample at a precise frequency, you might use an interrupt triggered by a Timer Counter (because 1/ 6.6 KHz ~151.5 us) rather than micros().
Search for antodom DUE Timer Counter library on github.

Hello Forum

I have bought a couple of Adafruit LSM6DSOX sensors, code and data taken from here: Arduino | LSM6DSOX, ISM330DHC, & LSM6DSO32 6 DoF IMUs | Adafruit Learning System

From the code, I am not able to see how I can change the sample rate seeing in serial monitor or putty to an exact value of 3.3kHz or 6.6kHz. The sensor samples by default with 104 Hz, and I want it to sample with either 3.3 kHz or 6.6 kHz, which I thought as an easy fix by using the sentence:

sox.setAccelDataRate(LSM6DS_RATE_3_33K_HZ);
sox.setGyroDataRate(LSM6DS_RATE_3_33K_HZ)

Super, know with that done I saw the code changing to say it samples with 3.3kHz, but the serial monitor sampled with approximately 9 hz. Hmm then I found a delay at the end which coursed this, and I then removed it. When now running the code I can’t imagine what is causing the lower sampling rate. When I take a section of samples from the serial monitor, copying it into excel, I can see that it samples with approximately 315hz, which is less lower than 3.3kHz.

Im using arduino due and the programming port. I have tried using the native port, but can’t get that up running. Is that the only way to fix this, and how can I use that in this case?
Code: [Select]

My Arduino code is this:
Code: [Select]

// Basic demo for accelerometer & gyro readings from Adafruit
// LSM6DSOX sensor

#include <Adafruit_LSM6DSOX.h>

// For SPI mode, we need a CS pin
#define LSM_CS 10
// For software-SPI mode we need SCK/MOSI/MISO pins
#define LSM_SCK 13
#define LSM_MISO 12
#define LSM_MOSI 11

Adafruit_LSM6DSOX sox;
void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit LSM6DSOX test!");

  if (!sox.begin_I2C()) {
    // if (!sox.begin_SPI(LSM_CS)) {
    // if (!sox.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) {
    // Serial.println("Failed to find LSM6DSOX chip");
    while (1) {
      //delay(10);
    }
  }

  Serial.println("LSM6DSOX Found!");

  // sox.setAccelRange(LSM6DS_ACCEL_RANGE_2_G);
  Serial.print("Accelerometer range set to: ");
  switch (sox.getAccelRange()) {
  case LSM6DS_ACCEL_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case LSM6DS_ACCEL_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case LSM6DS_ACCEL_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case LSM6DS_ACCEL_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }

  // sox.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS );
  Serial.print("Gyro range set to: ");
  switch (sox.getGyroRange()) {
  case LSM6DS_GYRO_RANGE_125_DPS:
    Serial.println("125 degrees/s");
    break;
  case LSM6DS_GYRO_RANGE_250_DPS:
    Serial.println("250 degrees/s");
    break;
  case LSM6DS_GYRO_RANGE_500_DPS:
    Serial.println("500 degrees/s");
    break;
  case LSM6DS_GYRO_RANGE_1000_DPS:
    Serial.println("1000 degrees/s");
    break;
  case LSM6DS_GYRO_RANGE_2000_DPS:
    Serial.println("2000 degrees/s");
    break;
  case ISM330DHCX_GYRO_RANGE_4000_DPS:
    break; // unsupported range for the DSOX
  }

  sox.setAccelDataRate(LSM6DS_RATE_6_66K_HZ);
  Serial.print("Accelerometer data rate set to: ");
  switch (sox.getAccelDataRate()) {
  case LSM6DS_RATE_SHUTDOWN:
    Serial.println("0 Hz");
    break;
  case LSM6DS_RATE_12_5_HZ:
    Serial.println("12.5 Hz");
    break;
  case LSM6DS_RATE_26_HZ:
    Serial.println("26 Hz");
    break;
  case LSM6DS_RATE_52_HZ:
    Serial.println("52 Hz");
    break;
  case LSM6DS_RATE_104_HZ:
    Serial.println("104 Hz");
    break;
  case LSM6DS_RATE_208_HZ:
    Serial.println("208 Hz");
    break;
  case LSM6DS_RATE_416_HZ:
    Serial.println("416 Hz");
    break;
  case LSM6DS_RATE_833_HZ:
    Serial.println("833 Hz");
    break;
  case LSM6DS_RATE_1_66K_HZ:
    Serial.println("1.66 KHz");
    break;
  case LSM6DS_RATE_3_33K_HZ:
    Serial.println("3.33 KHz");
    break;
  case LSM6DS_RATE_6_66K_HZ:
    Serial.println("6.66 KHz");
    break;
  }

  // sox.setGyroDataRate(LSM6DS_RATE_12_5_HZ);
  Serial.print("Gyro data rate set to: ");
  switch (sox.getGyroDataRate()) {
  case LSM6DS_RATE_SHUTDOWN:
    Serial.println("0 Hz");
    break;
  case LSM6DS_RATE_12_5_HZ:
    Serial.println("12.5 Hz");
    break;
  case LSM6DS_RATE_26_HZ:
    Serial.println("26 Hz");
    break;
  case LSM6DS_RATE_52_HZ:
    Serial.println("52 Hz");
    break;
  case LSM6DS_RATE_104_HZ:
    Serial.println("104 Hz");
    break;
  case LSM6DS_RATE_208_HZ:
    Serial.println("208 Hz");
    break;
  case LSM6DS_RATE_416_HZ:
    Serial.println("416 Hz");
    break;
  case LSM6DS_RATE_833_HZ:
    Serial.println("833 Hz");
    break;
  case LSM6DS_RATE_1_66K_HZ:
    Serial.println("1.66 KHz");
    break;
  case LSM6DS_RATE_3_33K_HZ:
    Serial.println("3.33 KHz");
    break;
  case LSM6DS_RATE_6_66K_HZ:
    Serial.println("6.66 KHz");
    break;
  }
}

void loop() {

  //  /* Get a new normalized sensor event */
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  sox.getEvent(&accel, &gyro, &temp);

 // Serial.print("\t\tTemperature ");
 // Serial.print(temp.temperature);
//  Serial.println(" deg C");

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("\t\tAccel X: ");
  Serial.print(accel.acceleration.x);
  Serial.print(" \tY: ");
  Serial.print(accel.acceleration.y);
  Serial.print(" \tZ: ");
  Serial.print(accel.acceleration.z);
  Serial.println(" m/s^2 ");

  /* Display the results (rotation is measured in rad/s) */
 // Serial.print("\t\tGyro X: ");
 // Serial.print(gyro.gyro.x);
  //Serial.print(" \tY: ");
 // Serial.print(gyro.gyro.y);
 // Serial.print(" \tZ: ");
 // Serial.print(gyro.gyro.z);
 // Serial.println(" radians/s ");
 // Serial.println();

 // delayMicroseconds(4);

  //  // serial plotter friendly format

  //  Serial.print(temp.temperature);
  //  Serial.print(",");

  //  Serial.print(accel.acceleration.x);
  //  Serial.print(","); Serial.print(accel.acceleration.y);
  //  Serial.print(","); Serial.print(accel.acceleration.z);
  //  Serial.print(",");

  // Serial.print(gyro.gyro.x);
  // Serial.print(","); Serial.print(gyro.gyro.y);
  // Serial.print(","); Serial.print(gyro.gyro.z);
  // Serial.println();
  //  delayMicroseconds(10000);
}

Hopefully this is a easy fix.

I don't think your sampling rate is the issue here. I think its your Baudrate. You are using 115,200 bits per seconds. Which is 14,400 Bytes per second.

How many bits or bytes are there in a character? - Stack Overflow.

Is there a good reason why you want this sampling rate?

Also, as a tip, don't print so much useless information, e.g. m/s^2, accel and so on.
Just have it in a simple format and you can print in your setup what they are.
So you will get:

t,ax,ay,az,gx,gy,gz

you might be limited by the ability of Serial to print fast enough. Don't use  Serial.begin(115200);try with  Serial.begin(1000000);and open the console at 1,000,000 bauds

Duplicate topics moved to the same forum section and merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Thanks for the answers, but I have now tried to change to native port and changing baudrate, and the desired sample rate is not obtained.

I have simplified the code and tried with this form the programmable port:

#include <Adafruit_LSM6DSOX.h>

Adafruit_LSM6DSOX sox;
void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    //delay(1); // will pause Zero, Leonardo, etc until Serial console opens

  Serial.println("Adafruit LSM6DSOX test!");

  if (!sox.begin_I2C()) {
 
  }
sox.setAccelDataRate(LSM6DS_RATE_6_66K_HZ);
}

void loop() {

  //  /* Get a new normalized sensor event */
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  sox.getEvent(&accel, &gyro, &temp);


    Serial.print(accel.acceleration.x);
    Serial.print(","); Serial.print(accel.acceleration.y);
    Serial.print(","); Serial.print(accel.acceleration.z);
  //  Serial.print(",");
  
   Serial.println();
   // delayMicroseconds(666);
}

From this, I got a sampling rate of approximately 380hz when I take the numbers copying them into excel and look at how many samples per second.

Then I have tried this code with the Native port:

#include <Adafruit_LSM6DSOX.h>

Adafruit_LSM6DSOX sox;
void setup(void) {
  SerialUSB.begin(115200);
  while (!SerialUSB)
    //delay(1); // will pause Zero, Leonardo, etc until SerialUSB console opens

  SerialUSB.println("Adafruit LSM6DSOX test!");

  if (!sox.begin_I2C()) {
 
  }
sox.setAccelDataRate(LSM6DS_RATE_6_66K_HZ);
}

void loop() {

  //  /* Get a new normalized sensor event */
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  sox.getEvent(&accel, &gyro, &temp);


    SerialUSB.print(accel.acceleration.x);
    SerialUSB.print(","); SerialUSB.print(accel.acceleration.y);
    SerialUSB.print(","); SerialUSB.print(accel.acceleration.z);
  //  SerialUSB.print(",");
  
   SerialUSB.println();
   // delayMicroseconds(666);
}

From this, I get a sampling rate of approximately 290hz.

When I’m trying with higher Baudrate for the programming port, it will only give useless data or no data. Does someone have a guess of what ells I can try?

P.S.
UKHeliBob I’m sorry about my duplicate, it was not intentionally. I thought I could delete the other post… But it will not happen again!

Instead of just deciding to give a baud rate of some random number, calculate the minimum required baudrate. Your MCU has limitations, try to keep it as efficient as possible.

dyso:
Well, The maximum baudrate that the serial monitor has is 115200.

this is not a true statement...


You want to print stuff 6600 times per second.

Assuming you have ~60 characters on a line when you print, it means you need to be able to spit out 396 000 characters per second.

A character takes about 10 bits so it means your baud rate needs to be 3 960 000 bauds
You can't really go higher than 2 million bauds with your arduino and you require almost double that ==> won't work. Your Serial.print() will become the limiting factor as it's blocking the thread until it can spit the data out when its small buffer is full.

I would recommend to print way less data to the Serial console, just the values accX, accY and accZ separated with tabs without any extra text. With 2 digits after the decimal point like "0.48\t2.11\t9.57\n" this way

Serial.print(accX, 2);
Serial.write('\t');
Serial.print(accY, 2);
Serial.write('\t');
Serial.print(accZ, 2);
Serial.write('\n');

that will be ~15 bytes per line, and then 1 million bauds could possibly be working out (if not try 2 million bauds)

http://ruemohr.org/~ircjunk/avr/baudcalc/avrbaudcalc-1.0.8.php

if that doesn't work, then buy a different board. The teensy 4.0 or 4.1 have a clock frequency of 600Mhz.

https://www.pjrc.com/store/teensy40.html

J-M-L:
this is not a true statement…

My mistake, sorry. I will edit that statement.

SerialUSB.write() is waayyyyy faster. Try this out eventhough an interrupt triggered by a Timer Counter is certainly the right way to go.

J-M-L, I now tried your proposal to sample fewer data and but I can not figure out how to be able to use the higher baudrates in the serial monitor.
With this code, I can get the sampling rate up to 416 Hz, and that is absolutely better, but not yet good enough.

#include <Adafruit_LSM6DSOX.h>

Adafruit_LSM6DSOX sox;
void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    //delay(1); // will pause Zero, Leonardo, etc until Serial console opens

    Serial.println("Adafruit LSM6DSOX test!");

  if (!sox.begin_I2C()) {

  }
  sox.setAccelDataRate(LSM6DS_RATE_6_66K_HZ);
  sox.setGyroDataRate(LSM6DS_RATE_6_66K_HZ);
}

void loop() {

  //  /* Get a new normalized sensor event */
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  sox.getEvent(&accel, &gyro, &temp);

Serial.print(accel.acceleration.x, 2);
Serial.write('\t');
Serial.print(accel.acceleration.y, 2);
Serial.write('\t');
Serial.print(accel.acceleration.z, 2);
Serial.write('\n');
  
  
}

If I instead use a higher Baudrate as you mention I get data in the format shown in the figure. Here I have been using 500000, but I tried with 1000000 and 2000000 as well, but only it got worse. This is data obtained with the programming port. If I instead use the Native port, I get no data no matter what I do.

ard_newbie, how do you propose to use the SerialUSB.write command? because it cannot be used with just:

Serial.write(accel.acceleration.x, 2);
Serial.write('\t');
Serial.write(accel.acceleration.y, 2);
Serial.write('\t');
Serial.write(accel.acceleration.z, 2);
Serial.write('\n');

And I would love to use the Timer Counter, but I definitely do not know how.

A tip : Your favorite search engine is your best friend....

Plus search in the DUE sub forum either for SerialUSB.wrtite()/SerialUSB.print() or using a Timer counter with direct register programming or TC library from antodom on github with example sketches to trigger an interrupt.

ard_newbie, how do you propose to use the SerialUSB.write command? because it cannot be used with just:

Serial.write(accel.acceleration.x, 2);

Serial.write('\t');
Serial.write(accel.acceleration.y, 2);
Serial.write('\t');
Serial.write(accel.acceleration.z, 2);
Serial.write('\n');

I did not say to use write for everything.... use write only for the tabs or carriage return and print for the floats => cf the code I had provided.

Hello again

First I have implemented the set clock function to obtain a higher sample rate:

#indclude <Wire.h>

Wire.begin();
  Wire.setClock(400000);              // 400000 Fast Mode.

Then I tried with two different methods and I did obtain 1.7kHz as the highest sample rate.
I gave it a try with the DUETimer lib and tried to use a timer function to gain higher speed, but here I got limited to a Maximum of 640hz…
The code with the timer:

#include <Adafruit_LSM6DSOX.h>
#include <Wire.h>
#include <DueTimer.h>
Adafruit_LSM6DSOX sox;

void firstHandler(){
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  sox.getEvent(&accel, &gyro, &temp);
  Serial.println(accel.acceleration.x, 2);
  //Serial.write('\t');
  //Serial.print(accel.acceleration.y, 2);
  //Serial.write('\t');
  //Serial.print(accel.acceleration.z, 2);
  //Serial.write('\n');
}


void setup(void) {
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000);              // 400000 Fast Mode.
  Timer5.attachInterrupt(firstHandler).setFrequency(500);
    //delay(1); // will pause Zero, Leonardo, etc until Serial console opens

  if (!sox.begin_I2C()) {

  }
  sox.setAccelDataRate(LSM6DS_RATE_3_33K_HZ);
  sox.setGyroDataRate(LSM6DS_RATE_3_33K_HZ);
}

void loop(){
  delay(2000);
  Timer5.start();
}

But then I tried without a timer and gained the 1.7kHz sampling frequency:

#include <Adafruit_LSM6DSOX.h>
#include <Wire.h>
#include <DueTimer.h>

Adafruit_LSM6DSOX sox;
void setup(void) {
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000);              // 400000 Fast Mode.
  while (!Serial)
    //delay(1); // will pause Zero, Leonardo, etc until Serial console opens

   

  if (!sox.begin_I2C()) {

  }
  sox.setAccelDataRate(LSM6DS_RATE_3_33K_HZ);
  sox.setGyroDataRate(LSM6DS_RATE_3_33K_HZ);
}

void loop() {

  //  /* Get a new normalized sensor event */
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  sox.getEvent(&accel, &gyro, &temp);

Serial.println(accel.acceleration.x, 2);
//Serial.write('\n');
//Serial.write('\t');
//Serial.print(accel.acceleration.y, 2);
//Serial.write('\t');
//Serial.print(accel.acceleration.z, 2);
//Serial.write('\n');
  
  
}

Do you have any additions to how I can obtain higher samplings.

1 Like

It's probably a long shot, but you can try a different library?

I’m still curious about what device is listening on the other end of the Serial port.

Could you share?

Dyso, the other lib did not work..

J-M-L, I'm not quite sure about what you are asking for, but I'll then list all the devices I use:

  • ThinkPad P50 Signature Edition (CPU E3-1505M v5, 2,80Ghz) (32GB ram) (64bit) (Windows 10)
  • Arduino Due, http://https://dk.rs-online.com/web/p/arduino/7697412/ (A000062)
  • USB-A to micro USB
  • chip (LSM6DSOX) http://https://learn.adafruit.com/lsm6dsox-and-ism330dhc-6-dof-imu/arduino
  • I2C wiring, using ports (GND, 3,3V, SCL21, SDA20)
#include <Wire.h>
#include <LSM6.h>
LSM6 IMU;

void setup()
{
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000);
IMU.init();
IMU.enableDefault();
}

void loop()
{
IMU.read();

float ax, ay, az;

ax = IMU.a.x * 0.061;  // conversion factor of 0.061 mg/LSB
ay = IMU.a.y * 0.061;
az = IMU.a.z * 0.061;

Serial.println(ax); 
}

Not sure if this works. I haven’t tested it. Just quickly wrote this.