Skip to content

Commit 97cc721

Browse files
author
Pete Lewis
authored
Merge pull request #25 from jgromes/master
Added option to use non-default I2C interface
2 parents 9891428 + a606ab8 commit 97cc721

File tree

8 files changed

+171
-161
lines changed

8 files changed

+171
-161
lines changed

examples/LSM9DS1_Basic_I2C/LSM9DS1_Basic_I2C.ino

Lines changed: 60 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
11
/*****************************************************************
2-
LSM9DS1_Basic_I2C.ino
3-
SFE_LSM9DS1 Library Simple Example Code - I2C Interface
4-
Jim Lindblom @ SparkFun Electronics
5-
Original Creation Date: April 30, 2015
6-
https://github.com/sparkfun/LSM9DS1_Breakout
2+
LSM9DS1_Basic_I2C.ino
3+
SFE_LSM9DS1 Library Simple Example Code - I2C Interface
4+
Jim Lindblom @ SparkFun Electronics
5+
Original Creation Date: April 30, 2015
6+
https://github.com/sparkfun/LSM9DS1_Breakout
77
8-
The LSM9DS1 is a versatile 9DOF sensor. It has a built-in
9-
accelerometer, gyroscope, and magnetometer. Very cool! Plus it
10-
functions over either SPI or I2C.
8+
The LSM9DS1 is a versatile 9DOF sensor. It has a built-in
9+
accelerometer, gyroscope, and magnetometer. Very cool! Plus it
10+
functions over either SPI or I2C.
1111
12-
This Arduino sketch is a demo of the simple side of the
13-
SFE_LSM9DS1 library. It'll demo the following:
14-
* How to create a LSM9DS1 object, using a constructor (global
12+
This Arduino sketch is a demo of the simple side of the
13+
SFE_LSM9DS1 library. It'll demo the following:
14+
How to create a LSM9DS1 object, using a constructor (global
1515
variables section).
16-
* How to use the begin() function of the LSM9DS1 class.
17-
* How to read the gyroscope, accelerometer, and magnetometer
18-
using the readGryo(), readAccel(), readMag() functions and
16+
How to use the begin() function of the LSM9DS1 class.
17+
How to read the gyroscope, accelerometer, and magnetometer
18+
using the readGryo(), readAccel(), readMag() functions and
1919
the gx, gy, gz, ax, ay, az, mx, my, and mz variables.
20-
* How to calculate actual acceleration, rotation speed,
21-
magnetic field strength using the calcAccel(), calcGyro()
20+
How to calculate actual acceleration, rotation speed,
21+
magnetic field strength using the calcAccel(), calcGyro()
2222
and calcMag() functions.
23-
* How to use the data from the LSM9DS1 to calculate
23+
How to use the data from the LSM9DS1 to calculate
2424
orientation and heading.
2525
26-
Hardware setup: This library supports communicating with the
27-
LSM9DS1 over either I2C or SPI. This example demonstrates how
28-
to use I2C. The pin-out is as follows:
26+
Hardware setup: This library supports communicating with the
27+
LSM9DS1 over either I2C or SPI. This example demonstrates how
28+
to use I2C. The pin-out is as follows:
2929
LSM9DS1 --------- Arduino
3030
SCL ---------- SCL (A5 on older 'Duinos')
3131
SDA ---------- SDA (A4 on older 'Duinos')
3232
VDD ------------- 3.3V
3333
GND ------------- GND
34-
(CSG, CSXM, SDOG, and SDOXM should all be pulled high.
35-
Jumpers on the breakout board will do this for you.)
34+
(CSG, CSXM, SDOG, and SDOXM should all be pulled high.
35+
Jumpers on the breakout board will do this for you.)
3636
37-
The LSM9DS1 has a maximum voltage of 3.6V. Make sure you power it
38-
off the 3.3V rail! I2C pins are open-drain, so you'll be
39-
(mostly) safe connecting the LSM9DS1's SCL and SDA pins
40-
directly to the Arduino.
37+
The LSM9DS1 has a maximum voltage of 3.6V. Make sure you power it
38+
off the 3.3V rail! I2C pins are open-drain, so you'll be
39+
(mostly) safe connecting the LSM9DS1's SCL and SDA pins
40+
directly to the Arduino.
4141
42-
Development environment specifics:
42+
Development environment specifics:
4343
IDE: Arduino 1.6.3
4444
Hardware Platform: SparkFun Redboard
4545
LSM9DS1 Breakout Version: 1.0
4646
47-
This code is beerware. If you see me (or any other SparkFun
48-
employee) at the local, and you've found our code helpful,
49-
please buy us a round!
47+
This code is beerware. If you see me (or any other SparkFun
48+
employee) at the local, and you've found our code helpful,
49+
please buy us a round!
5050
51-
Distributed as-is; no warranty is given.
51+
Distributed as-is; no warranty is given.
5252
*****************************************************************/
5353
// The SFE_LSM9DS1 library requires both Wire and SPI be
5454
// included BEFORE including the 9DS1 library.
@@ -67,8 +67,8 @@ LSM9DS1 imu;
6767
// Example I2C Setup //
6868
///////////////////////
6969
// SDO_XM and SDO_G are both pulled high, so our addresses are:
70-
#define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
71-
#define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW
70+
// #define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
71+
// #define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW
7272

7373
////////////////////////////
7474
// Sketch Output Settings //
@@ -78,42 +78,33 @@ LSM9DS1 imu;
7878
#define PRINT_SPEED 250 // 250 ms between prints
7979
static unsigned long lastPrint = 0; // Keep track of print time
8080

81-
// Earth's magnetic field varies by location. Add or subtract
82-
// a declination to get a more accurate heading. Calculate
81+
// Earth's magnetic field varies by location. Add or subtract
82+
// a declination to get a more accurate heading. Calculate
8383
// your's here:
8484
// http://www.ngdc.noaa.gov/geomag-web/#declination
8585
#define DECLINATION -8.58 // Declination (degrees) in Boulder, CO.
8686

8787
//Function definitions
88-
void printGyro();
89-
void printAccel();
90-
void printMag();
88+
void printGyro();
89+
void printAccel();
90+
void printMag();
9191
void printAttitude(float ax, float ay, float az, float mx, float my, float mz);
9292

93-
void setup()
93+
void setup()
9494
{
95-
9695
Serial.begin(115200);
97-
98-
// Before initializing the IMU, there are a few settings
99-
// we may need to adjust. Use the settings struct to set
100-
// the device's communication mode and addresses:
101-
imu.settings.device.commInterface = IMU_MODE_I2C;
102-
imu.settings.device.mAddress = LSM9DS1_M;
103-
imu.settings.device.agAddress = LSM9DS1_AG;
104-
// The above lines will only take effect AFTER calling
105-
// imu.begin(), which verifies communication with the IMU
106-
// and turns it on.
107-
if (!imu.begin())
96+
97+
Wire.begin();
98+
99+
if (!imu.begin()) // with no arguments, this uses default addresses (AG:0x6B, M:0x1E) and i2c port (Wire).
108100
{
109101
Serial.println("Failed to communicate with LSM9DS1.");
110102
Serial.println("Double-check wiring.");
111103
Serial.println("Default settings in this sketch will " \
112-
"work for an out of the box LSM9DS1 " \
113-
"Breakout, but may need to be modified " \
114-
"if the board jumpers are.");
115-
while (1)
116-
;
104+
"work for an out of the box LSM9DS1 " \
105+
"Breakout, but may need to be modified " \
106+
"if the board jumpers are.");
107+
while (1);
117108
}
118109
}
119110

@@ -141,7 +132,7 @@ void loop()
141132
// mx, my, and mz variables with the most current data.
142133
imu.readMag();
143134
}
144-
135+
145136
if ((lastPrint + PRINT_SPEED) < millis())
146137
{
147138
printGyro(); // Print "G: gx, gy, gz"
@@ -151,10 +142,10 @@ void loop()
151142
// Call print attitude. The LSM9DS1's mag x and y
152143
// axes are opposite to the accelerometer, so my, mx are
153144
// substituted for each other.
154-
printAttitude(imu.ax, imu.ay, imu.az,
155-
-imu.my, -imu.mx, imu.mz);
145+
printAttitude(imu.ax, imu.ay, imu.az,
146+
-imu.my, -imu.mx, imu.mz);
156147
Serial.println();
157-
148+
158149
lastPrint = millis(); // Update lastPrint time
159150
}
160151
}
@@ -184,7 +175,7 @@ void printGyro()
184175
}
185176

186177
void printAccel()
187-
{
178+
{
188179
// Now we can use the ax, ay, and az variables as we please.
189180
// Either print them as raw ADC values, or calculated in g's.
190181
Serial.print("A: ");
@@ -198,7 +189,7 @@ void printAccel()
198189
Serial.print(", ");
199190
Serial.print(imu.calcAccel(imu.az), 2);
200191
Serial.println(" g");
201-
#elif defined PRINT_RAW
192+
#elif defined PRINT_RAW
202193
Serial.print(imu.ax);
203194
Serial.print(", ");
204195
Serial.print(imu.ay);
@@ -209,7 +200,7 @@ void printAccel()
209200
}
210201

211202
void printMag()
212-
{
203+
{
213204
// Now we can use the mx, my, and mz variables as we please.
214205
// Either print them as raw ADC values, or calculated in Gauss.
215206
Serial.print("M: ");
@@ -241,26 +232,26 @@ void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
241232
{
242233
float roll = atan2(ay, az);
243234
float pitch = atan2(-ax, sqrt(ay * ay + az * az));
244-
235+
245236
float heading;
246237
if (my == 0)
247238
heading = (mx < 0) ? PI : 0;
248239
else
249240
heading = atan2(mx, my);
250-
241+
251242
heading -= DECLINATION * PI / 180;
252-
243+
253244
if (heading > PI) heading -= (2 * PI);
254245
else if (heading < -PI) heading += (2 * PI);
255-
246+
256247
// Convert everything from radians to degrees:
257248
heading *= 180.0 / PI;
258249
pitch *= 180.0 / PI;
259250
roll *= 180.0 / PI;
260-
251+
261252
Serial.print("Pitch, Roll: ");
262253
Serial.print(pitch, 2);
263254
Serial.print(", ");
264255
Serial.println(roll, 2);
265256
Serial.print("Heading: "); Serial.println(heading, 2);
266-
}
257+
}

examples/LSM9DS1_Basic_SPI/LSM9DS1_Basic_SPI.ino

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,10 @@ void printAttitude(float ax, float ay, float az, float mx, float my, float mz);
9696
void setup()
9797
{
9898
Serial.begin(115200);
99-
100-
// Before initializing the IMU, there are a few settings
101-
// we may need to adjust. Use the settings struct to set
102-
// the device's communication mode and addresses:
103-
imu.settings.device.commInterface = IMU_MODE_SPI;
104-
imu.settings.device.mAddress = LSM9DS1_M_CS;
105-
imu.settings.device.agAddress = LSM9DS1_AG_CS;
106-
// The above lines will only take effect AFTER calling
107-
// imu.begin(), which verifies communication with the IMU
99+
100+
// imu.beginSPI(), which verifies communication with the IMU
108101
// and turns it on.
109-
if (!imu.begin())
102+
if (!imu.beginSPI(LSM9DS1_AG_CS, LSM9DS1_M_CS)) // note, we need to sent this our CS pins (defined above)
110103
{
111104
Serial.println("Failed to communicate with LSM9DS1.");
112105
Serial.println("Double-check wiring.");
@@ -251,4 +244,4 @@ void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
251244
Serial.print(", ");
252245
Serial.println(roll, 2);
253246
Serial.print("Heading: "); Serial.println(heading, 2);
254-
}
247+
}

examples/LSM9DS1_Interrupts/LSM9DS1_Interrupts.ino

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ to use I2C. The pin-out is as follows:
4040
INT2 ------------- D4
4141
INT1 ------------- D3
4242
INTM ------------- D5
43+
RDY -------------- D6
4344
(CSG, CSXM, SDOG, and SDOXM should all be pulled high.
4445
Jumpers on the breakout board will do this for you.)
4546
@@ -86,11 +87,6 @@ void printStats();
8687
// and sample rates.
8788
uint16_t configureIMU()
8889
{
89-
// Set up Device Mode (I2C) and I2C addresses:
90-
imu.settings.device.commInterface = IMU_MODE_I2C;
91-
imu.settings.device.agAddress = LSM9DS1_AG_ADDR(1);
92-
imu.settings.device.mAddress = LSM9DS1_M_ADDR(1);
93-
9490
// gyro.latchInterrupt controls the latching of the
9591
// gyro and accelerometer interrupts (INT1 and INT2).
9692
// false = no latching
@@ -106,10 +102,10 @@ uint16_t configureIMU()
106102
imu.settings.mag.scale = 4;
107103
// Set magnetometer sample rate to 0.625 Hz
108104
imu.settings.mag.sampleRate = 0;
109-
105+
110106
// Call imu.begin() to initialize the sensor and instill
111107
// it with our new settings.
112-
return imu.begin();
108+
return imu.begin(LSM9DS1_AG_ADDR(1), LSM9DS1_M_ADDR(1), Wire); // set addresses and wire port
113109
}
114110

115111
void configureLSM9DS1Interrupts()
@@ -194,6 +190,8 @@ void setup()
194190
// It is active high and always turned on.
195191
pinMode(RDYM_PIN, INPUT);
196192

193+
Wire.begin();
194+
197195
// Turn on the IMU with configureIMU() (defined above)
198196
// check the return status of imu.begin() to make sure
199197
// it's connected.
@@ -312,4 +310,3 @@ void printStats()
312310
Serial.print(imu.my); Serial.print(", ");
313311
Serial.println(imu.mz);
314312
}
315-

examples/LSM9DS1_Settings/LSM9DS1_Settings.ino

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Distributed as-is; no warranty is given.
4949

5050
LSM9DS1 imu; // Create an LSM9DS1 object
5151

52+
// SDO_XM and SDO_G are both pulled high, so our addresses are:
53+
#define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
54+
#define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW
55+
5256
// Global variables to keep track of update rates
5357
unsigned long startTime;
5458
unsigned int accelReadCounter = 0;
@@ -63,20 +67,6 @@ const unsigned int PRINT_RATE = 500;
6367
//Function definitions
6468
void printSensorReadings();
6569

66-
void setupDevice()
67-
{
68-
// [commInterface] determines whether we'll use I2C or SPI
69-
// to communicate with the LSM9DS1.
70-
// Use either IMU_MODE_I2C or IMU_MODE_SPI
71-
imu.settings.device.commInterface = IMU_MODE_I2C;
72-
// [mAddress] sets the I2C address or SPI CS pin of the
73-
// LSM9DS1's magnetometer.
74-
imu.settings.device.mAddress = 0x1E; // Use I2C addres 0x1E
75-
// [agAddress] sets the I2C address or SPI CS pin of the
76-
// LSM9DS1's accelerometer/gyroscope.
77-
imu.settings.device.agAddress = 0x6B; // I2C address 0x6B
78-
}
79-
8070
void setupGyro()
8171
{
8272
// [enabled] turns the gyro on or off.
@@ -192,18 +182,19 @@ void setupTemperature()
192182

193183
uint16_t initLSM9DS1()
194184
{
195-
setupDevice(); // Setup general device parameters
196185
setupGyro(); // Set up gyroscope parameters
197186
setupAccel(); // Set up accelerometer parameters
198187
setupMag(); // Set up magnetometer parameters
199188
setupTemperature(); // Set up temp sensor parameter
200189

201-
return imu.begin();
190+
return imu.begin(LSM9DS1_AG, LSM9DS1_M, Wire); // for SPI use beginSPI()
202191
}
203192

204193
void setup()
205194
{
206195
Serial.begin(115200);
196+
197+
Wire.begin();
207198

208199
Serial.println("Initializing the LSM9DS1");
209200
uint16_t status = initLSM9DS1();
@@ -300,4 +291,3 @@ void printSensorReadings()
300291
Serial.println(" Hz");
301292
Serial.println();
302293
}
303-

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ gBiasRaw KEYWORD2
2828
aBiasRaw KEYWORD2
2929
mBiasRaw KEYWORD2
3030
begin KEYWORD2
31+
beginSPI KEYWORD2
3132
calibrate KEYWORD2
3233
calibrateMag KEYWORD2
3334
magOffset KEYWORD2

0 commit comments

Comments
 (0)