You can easily wire this breakout to any microcontroller, we'll be using a Metro, Adafruit's Arduino compatible board. For another kind of microcontroller, just make sure it has I2C or SPI, then port the code - its pretty simple stuff!
Let's start with just I2C interfacing since it requires the fewest number of wires. Here is how you would wire up one of the newer revision boards with the black PCB by using the new Stemma QT connectors:
- Connect board VIN (red wire) to Arduino 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
- Connect board GND (black wire) to Arduino GND
- Connect board SCL (yellow wire) to Arduino SCL
- Connect board SDA (blue wire) to Arduino SDA
If you prefer to use a breadboard wire up the sensor like this:
- Connect board VIN (red wire) to Arduino 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
- Connect board GND (black wire) to Arduino GND
- Connect board SCL (yellow wire) to Arduino SCL
- Connect board SDA (blue wire) to Arduino SDA
If you have one of the classic blue versions of the LSM9DS1, you'll wire it up like so:
-
Connect Vin to the power supply, 3-5V is fine. Use the same
voltage that the microcontroller logic is based off of. For most
Arduinos, that is 5V
- Connect GND to common power/data ground
- Connect the SCL pin to the I2C clock SCL pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A5, on a Mega it is also known as digital 21 and on a Leonardo/Micro, digital 3
- Connect the SDA pin to the I2C data SDA pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A4, on a Mega it is also known as digital 20 and on a Leonardo/Micro, digital 2
Download Arduino libraries
To begin reading sensor data, you will need to download the Adafruit_LSM9DS1 library and the Adafruit_Sensor library from the Arduino library manager.
Open up the Arduino library manager:
Search for the Adafruit LSM9DS1 library and install it
Search for the Adafruit Unified Sensor library and install it (You may have to scroll down pretty far to find it)
We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
Load Demo Sketch
Now you can open up File->Examples->Adafruit_LSM9DS1->lsm9ds1 and upload to your Arduino wired up to the sensor
Then open up the Serial console at 115200 baud to read the sensor output! You'll get 9 distinct data points, accelerometer x/y/z in meters/s2, magetometer x/y/z in gauss and gyroscope in x/y/x degrees/second
We suggest using this Adafruit_Sensor interface as shown in this demo, since it will let you swap sensors without having to worry about units compatibility. Try twisting and moving the board around to see the sensors change value.
Library Reference
The library we have is simple and easy to use
You can create the Adafruit_LSM9DS1 object with:
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1(); // i2c sensor
I2C does not have pins, as they are fixed in hardware.
If you're using "hardware" SPI, you will have to wire up the pins as follows:
- SCL -> SPI CLK
- SDA -> SPI MOSI
- SDO_AG & SDO_M -> SPI MISO (both together)
You can determine the hardware SPI pins for your Arduino here Then pick two pins for the CS lines
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1(LSM9DS1_XGCS, LSM9DS1_MCS);
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1(LSM9DS1_SCK, LSM9DS1_MISO, LSM9DS1_MOSI, LSM9DS1_XGCS, LSM9DS1_MCS);
Begin!
To initialize the sensor, call lsm.begin() which will check the sensor can be found. It returns true/false depending on these checks. We suggest you wrap begin() in a statement that will check if the sensor was located:if(!lsm.begin()) { /* There was a problem detecting the LSM9DS1 ... check your connections */ Serial.print(F("Ooops, no LSM9DS1 detected ... Check your wiring!")); while(1); }
Set Ranges
These chips have tons of registers, we basically provide interface code for the most useful stuff, such as setting the range. Each subsensor has it's own range. Higher ranges have less precision but can measure larger movements!Set up the ranges with the setup functions:
// 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);
Read data
Read data using the Adafruit_Sensor API by first creating four events, one for each sub-sensor:sensors_event_t accel, mag, gyro, temp;
lsm.getEvent(&accel, &mag, &gyro, &temp);
The data is snapshotted at once, so you can read and manage the data later.
For the Accelerometer event you can read accel.acceleration.x, accel.acceleration.y or accel.acceleration.z which are in meters/second*second.
For the Magnetometer event you can read mag.magnetic.x, mag.magnetic.y or mag.magnetic.z which are in gauss.
For the Gyro event you can read gyro.gyro.x, gyro.gyro.y or gyro.gyro.z, which are in degrees-per-second (dps)
The temperature event data is in temp.temperature, but we don't guarantee that the temperature data is in degrees C
Page last edited March 08, 2024
Text editor powered by tinymce.