I'm trying to get my Sparkfun "LSM9DS0" to work with my Pro-Mini-328 (3v) . interestingly, when i run the sketch through my UNO (with scl and sda adj to 3.3v), the sketch works fine.
but when i use the Pro-Mini, i get good readings from the Accelerometer, but the Gyro x,y,z are frozen. I've gone back and forth using 2 UNO's and 2 Mini-Pros.
is this a problem with the calcs?
the sketch uses the SFE_LSM9DS0.h library...
// The SFE_LSM9DS0 requires both the SPI and Wire libraries.
// Unfortunately, you'll need to include both in the Arduino
// sketch, before including the SFE_LSM9DS0 library.
#include <SPI.h> // Included for SFE_LSM9DS0 library
#include <Wire.h>
#include <SFE_LSM9DS0.h>
#include <stdlib.h>
#define LSM9DS0_XM 0x1D // Would be 0x1E if SDO_XM is LOW
#define LSM9DS0_G 0x6B // Would be 0x6A if SDO_G is LOW
LSM9DS0 dof(MODE_I2C, LSM9DS0_G, LSM9DS0_XM);
// Do you want to print calculated values or raw ADC ticks read
// from the sensor? Comment out ONE of the two #defines below to pick:
#define PRINT_CALCULATED
//#define PRINT_RAW
#define PRINT_SPEED 5 // 500 ms between prints
String strClearBuff;
char gX[10], gY[10], gZ[10], aX[10], aY[10], aZ[10];
void setup()
{
Serial.begin(9600); // Start serial at 9600 bps
// You can either call it with no parameters (the easy way):
uint16_t status = dof.begin();
// Or call it with declarations for sensor scales and data rates:
//uint16_t status = dof.begin(dof.G_SCALE_2000DPS, dof.A_SCALE_6G, dof.M_SCALE_2GS);
// make sure communication was successful.
Serial.print("LSM9DS0 WHO_AM_I's returned: 0x");
Serial.println(status, HEX);
Serial.println("Should be 0x49D4");
Serial.println();
delay(500);
}
void loop()
{
printGyro(); // Print "G: gx, gy, gz"
printAccel(); // Print "A: ax, ay, az"
printMag(); // Print "M: mx, my, mz"
// Print the heading and orientation for fun!
printHeading((float) dof.mx, (float) dof.my);
// rjh printOrientation(dof.calcAccel(dof.ax), dof.calcAccel(dof.ay),
// rjh dof.calcAccel(dof.az));
Serial.println();
delay(PRINT_SPEED);
}
void printGyro()
{
// To read from the gyroscope, you must first call the
// readGyro() function. When this exits, it'll update the
// gx, gy, and gz variables with the most current data.
dof.readGyro();
// Now we can use the gx, gy, and gz variables as we please.
// Either print them as raw ADC values, or calculated in DPS.
Serial.print("G: ");
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcGyro helper function to convert a raw ADC value to
// DPS. Give the function the value that you want to convert.
// strClearBuff = "";
dtostrf(dof.calcGyro(dof.gx),7,2,gX);
Serial.print("\tgX: "); Serial.print(gX);
dtostrf(dof.calcGyro(dof.gy),7,2,gY);
Serial.print("\tgY: "); Serial.print(gY);
dtostrf(dof.calcGyro(dof.gz),7,2,gZ);
Serial.print("\tgZ: "); Serial.print(gZ);
// Serial.print("\tdof.calcGyro(dof.gx)");
// Serial.print(dof.calcGyro(dof.gx), 2);
// Serial.print("\t, ");
//
// Serial.print(dof.calcGyro(dof.gy), 2);
// Serial.print("\t\t, ");
// Serial.print(dof.calcGyro(dof.gz), 2);
#elif defined PRINT_RAW
Serial.print(dof.gx);
// rjh Serial.print("\t, ");
// rjh Serial.print(dof.gy);
// rjh Serial.print("\t\t, ");
// rjh Serial.print(dof.gz);
#endif
}
void printAccel()
{
// To read from the accelerometer, you must first call the
// readAccel() function. When this exits, it'll update the
// ax, ay, and az variables with the most current data.
dof.readAccel();
// Now we can use the ax, ay, and az variables as we please.
// Either print them as raw ADC values, or calculated in g's.
Serial.print("\tA: ");
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcAccel helper function to convert a raw ADC value to
// g's. Give the function the value that you want to convert.
dtostrf(dof.calcGyro(dof.ax),7,2, aX);
Serial.print("\taX: "); Serial.print(aX);
dtostrf(dof.calcGyro(dof.ay),7,2,aY);
Serial.print("\taY: "); Serial.print(aY);
dtostrf(dof.calcGyro(dof.az),7,2,aZ);
Serial.print("\taZ: "); Serial.print(aZ);
// Serial.print(dof.calcAccel(dof.ax), 2);
// Serial.print("\t, ");
// Serial.print(dof.calcAccel(dof.ay), 2);
// Serial.print("\t\t, ");
// Serial.print(dof.calcAccel(dof.az), 2);
#elif defined PRINT_RAW
// Serial.print(dof.ax);
// Serial.print("\t, ");
// rjh Serial.print(dof.ay);
// rjh Serial.print("\t\t, ");
// rjh Serial.print(dof.az);
#endif
}
void printMag()
{
// To read from the magnetometer, you must first call the
// readMag() function. When this exits, it'll update the
// mx, my, and mz variables with the most current data.
dof.readMag();
// Now we can use the mx, my, and mz variables as we please.
// Either print them as raw ADC values, or calculated in Gauss.
Serial.print("\tM: ");
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcMag helper function to convert a raw ADC value to
// Gauss. Give the function the value that you want to convert.
Serial.print(dof.calcMag(dof.mx), 2);
// rjh Serial.print("\t, ");
// rjh Serial.print(dof.calcMag(dof.my), 2);
// rjh Serial.print("\t\t, ");
// rjh Serial.print(dof.calcMag(dof.mz), 2);
#elif defined PRINT_RAW
Serial.print(dof.mx);
// rjh Serial.print("\t, ");
// rjh Serial.print(dof.my);
// rjh Serial.print("\t\t, ");
// rjh Serial.print(dof.mz);
#endif
}
// Here's a fun function to calculate your heading, using Earth's
// magnetic field.
// It only works if the sensor is flat (z-axis normal to Earth).
// Additionally, you may need to add or subtract a declination
// angle to get the heading normalized to your location.
// See: http://www.ngdc.noaa.gov/geomag/declination.shtml
void printHeading(float hx, float hy)
{
float heading;
if (hy > 0)
{
heading = 90 - (atan(hx / hy) * (180 / PI));
}
else if (hy < 0)
{
heading = - (atan(hx / hy) * (180 / PI));
}
else // hy = 0
{
if (hx < 0) heading = 180;
else heading = 0;
}
Serial.print("\tHeading: ");
Serial.print(heading, 2);
}
// Another fun function that does calculations based on the
// acclerometer data. This function will print your LSM9DS0's
// orientation -- it's roll and pitch angles.
void printOrientation(float x, float y, float z)
{
float pitch, roll;
pitch = atan2(x, sqrt(y * y) + (z * z));
roll = atan2(y, sqrt(x * x) + (z * z));
pitch *= 180.0 / PI;
roll *= 180.0 / PI;
Serial.print("\tPitch, Roll: ");
Serial.print(pitch, 2);
Serial.print(", ");
Serial.println(roll, 2);
}
if you need me to simplify the code, i will - a lot of the code has to do with having the data go to the serial monitor with out shifting columns when the values add or remove a digit.
thanks for your help,
Russ from Coral Springs, Florida - USA