Hello,
i am trying to change the ODR and it seems that the LSM303DLHC doesn't respond the way it should be. The way i change the ODR is by changing the CTRL_REG1_A register. So the datasheet says that for ODR 100Hz i use 0101 for the ODR and for normal mode and all the axis (x,y,z) enabled i get 01010111. I find the Hex of it which is 57 and in the Adafruit_LSM303.cpp i change the code to write8(LSM303_ADDRESS_ACCEL, LSM303_REGISTER_ACCEL_CTRL_REG1_A, 0x57); When i try to take samples the sample rate is much lower (like 40Hz). I figured out that this has to do also with the way i output the data, SD or on the screen, but for smaller ODR like 10hz my sample rate goes to 80 Hz for when i print it on screen, and 40 Hz for the sd card. The baud rate is 9600.
LSM303 ODR
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: LSM303 ODR
Please post the code that you are using.
- adim
- Posts: 12
- Joined: Fri Dec 16, 2016 7:14 am
Re: LSM303 ODR
Code: Select all
#include "Arduino.h"
#include <Adafruit_LSM303.h>
#include <Adafruit_L3GD20.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
Adafruit_LSM303 accelor;
Adafruit_L3GD20 gyro;
#define SENSORS_GRAVITY_EARTH (9.80665F)
float accelx,accely,accelz;
static float _lsm303Accel_MG_LSB = 0.001F;
//SD
File myFile;
int pinCS = 10;
void setup()
{
Serial.begin(9600);
if (!gyro.begin(gyro.L3DS20_RANGE_250DPS)||!accelor.begin())
{
Serial.println("Something is not good..Check your wiring!");
while (1);
}
pinMode(pinCS, OUTPUT);
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed 2");
return;
}
}
void loop()
{
accelor.read();
accelx = (float)accelor.accelData.x* _lsm303Accel_MG_LSB * SENSORS_GRAVITY_EARTH;
accely = (float)accelor.accelData.y* _lsm303Accel_MG_LSB * SENSORS_GRAVITY_EARTH;
accelz = (float)accelor.accelData.z* _lsm303Accel_MG_LSB * SENSORS_GRAVITY_EARTH;
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
myFile.print(accelx);
myFile.print(",");
myFile.print(accely);myFile.print(",");
myFile.println(accelz);
// myFile.print(",");
//
//
myFile.close(); // close the file
}
else {
Serial.println("error opening test.txt");// The Last prints
}
}
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: LSM303 ODR
Your sample rate is going to be gated by the speed that you can write to the SD card. Since you are closing the file after every read, you are forcing it to write the entire block buffer - even if it is only partially filled. If you change your code to only close the file after you are finished with your sample run, the buffer will only need to be written when it is full.
If you print to the Serial Port instead, your sample rate will be limited by your serial communication time. At 9600 baud, it takes about 1 millisecond per character. If you use a baud rate of 115200. your speed should improve.
If you print to the Serial Port instead, your sample rate will be limited by your serial communication time. At 9600 baud, it takes about 1 millisecond per character. If you use a baud rate of 115200. your speed should improve.
- adim
- Posts: 12
- Joined: Fri Dec 16, 2016 7:14 am
Re: LSM303 ODR
Thank you for your response,
as i wrote in the first message the problem is not only in the case that i dont get the right data rate when the ODR is high like 100Hz, it also happens when the ODR is low like 10Hz. When the data rate is low (10Hz) i get 80Hz when i print it in the SD card for example.
as i wrote in the first message the problem is not only in the case that i dont get the right data rate when the ODR is high like 100Hz, it also happens when the ODR is low like 10Hz. When the data rate is low (10Hz) i get 80Hz when i print it in the SD card for example.
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: LSM303 ODR
The data rate and your polling rate are not related. The chip's data rate governs how fast the registers are updated. There is nothing to stop you from polling them multiple times between updates.
- adim
- Posts: 12
- Joined: Fri Dec 16, 2016 7:14 am
Re: LSM303 ODR
Is there a way to match these two or an example that i can understand the way i can control this?
Thank you
Thank you
- adafruit_support_bill
- Posts: 88151
- Joined: Sat Feb 07, 2009 10:11 am
Re: LSM303 ODR
I believe that the I1 pin can be set up to output a Data Ready signal for the accelerometer with the CTRL_REG3_A register. You can monitor that pin to see when a new measurement is available.
See section 7.1.3:
https://cdn-shop.adafruit.com/datasheets/LSM303DLHC.PDF
See section 7.1.3:
https://cdn-shop.adafruit.com/datasheets/LSM303DLHC.PDF
- adim
- Posts: 12
- Joined: Fri Dec 16, 2016 7:14 am
Re: LSM303 ODR
Thank you for your help, i will try to find a way to do it because the Datasheet is not so explanatory.
Please be positive and constructive with your questions and comments.