A library for interfacing the Melopero LSM9DS1 9-DOF breakout board with Arduino.
If you were looking for the Python3 library for the Raspberry Pi click HERE
| Melopero LSM9DS1 | Description |
|---|---|
| 3V3 | Input power pin. Apply 3.3V to this pin |
| SCL | I2C or SPI Serial Clock pin |
| SDA | I2C SDA pin or SPI MOSI pin |
| GND | Ground pin |
| CSAG | Accelerometer+Gyro SPI Chip Select |
| CSM | Magnetometer SPI Chip Select |
| SDOAG | Accelerometer+Gyro SPI MISO pin |
| SDOM | Magnetometer SPI MISO pin |
| INT1 | Accelerometer+Gyro Interrupt pin |
| INTM | Magnetometer Interrupt pin |
| INT2 | Another Interrupt pin for the accelerometer+gyro. This pin is not supported in our library |
| DEN | Gyroscope data enable pin. This pin is not supported in our library |
| DRDY | Magnetometer data ready pin. This pin is not supported in our library |
You will need:
- Arduino IDE, you can download it here: download python3
- the Melopero LSM9DS1 breakout: buy here
Connect the sensor to Arduino
(use only 3.3V power and logic, do not connect this sensor board directly to 5V)
This sensor communicates over I2C or SPI.
I2C connections:
| Melopero LSM9DS1 | Arduino |
|---|---|
| 3V3 | VCC |
| SCL | SCL |
| SDA | SDA |
| GND | GND |
SPI connections:
| Melopero LSM9DS1 | Arduino |
|---|---|
| 3V3 | VCC |
| SCL | SCLK |
| SDA | MOSI |
| SDOAG, SDOM | MISO |
| CSAG | any digital pin |
| CSM | any digital pin |
Optional interrupt pins:
| Melopero LSM9DS1 | Arduino |
|---|---|
| INT1 (interrupt pin for Accelerometer and Gyroscope) |
Any pin that supports interrupts |
| INTM (interrupt pin for Magnetometer) |
Any pin that supports interrupts |
This library can be installed directly from the Library manager of the Arduino IDE.
Open the Arduino IDE, select Sketch-->Include Library-->Manage Libraries.
Type "melopero lsm9ds1", select the library and click on "Install".
The installation will include some examples, available under File-->Examples-->Melopero LSM9DS1.
//Author: Leonardo La Rocca
//email: info@melopero.com
//
//In this example it is shown how to configure the device to work with
//the I2C communication protocol.
//First make sure that your connections are setup correctly:
//I2C pinout:
//LSM9DS1 <------> Arduino MKR
// 3v3 <------> VCC
// SCL <------> SCL (12)
// SDA <------> SDA (11)
// GND <------> GND
//
//Note: Do not connect the device to the 5V pin!
#include <Melopero_LSM9DS1.h>
Melopero_LSM9DS1 device;
int waitMillis = 1000;
void setup() {
Serial.begin(9600);
//Setup the device to use i2c communication.
//device.getErrorString returns a readable interpretation for the
//status returned by most of the functions.
Serial.print("starting, setup i2c: ");
Serial.println(device.getErrorString(device.useI2C()));
//Reset all the settings
Serial.print("Resetting settings... ");
Serial.println(device.getErrorString(device.resetSettings()));
//Setup the output data rates
Serial.print("Setting Gyro output data rate: ");
Serial.println(device.setGyroODR(OutputDataRate::Hz_10));
Serial.print("Setting Accelerometer output data rate: ");
Serial.println(device.setAccODR(OutputDataRate::Hz_10));
Serial.print("Setting Magnetometer output data rate: ");
Serial.println(device.setMagODR(MagnetometerOutputDataRate::Hz_10));
}
void loop() {
delay(waitMillis);
//first update the measurements
device.updateGyroMeasurements();
device.updateAccMeasurements();
device.updateMagMeasurements();
//then print them out
Serial.println("******** measurements ********");
printGyroMeasurements();
Serial.println();
printAccMeasurements();
Serial.println();
printMagMeasurements();
Serial.println();
}
void printGyroMeasurements(){
//device.gyroMeasurements is an array of three elements (one for each axis)
//that contains the last measurements for the gyroscope.
//These measurements are converted to dps, to read the raw values you
//can use device.gyroRawMeasurements .
Serial.print("[Gyro dps (degrees per second)]:");
Serial.print(" x : ");
Serial.print(device.gyroMeasurements[0]);
Serial.print(" y : ");
Serial.print(device.gyroMeasurements[1]);
Serial.print(" z : ");
Serial.print(device.gyroMeasurements[2]);
}
void printAccMeasurements(){
//device.accMeasurements is an array of three elements (one for each axis)
//that contains the last measurements for the accelerometer.
//These measurements are converted to g, to read the raw values you
//can use device.accRawMeasurements .
Serial.print("[Accelerometer g (gravity)]:");
Serial.print(" x : ");
Serial.print(device.accMeasurements[0]);
Serial.print(" y : ");
Serial.print(device.accMeasurements[1]);
Serial.print(" z : ");
Serial.print(device.accMeasurements[2]);
}
void printMagMeasurements(){
//device.magMeasurements is an array of three elements (one for each axis)
//that contains the last measurements for the magnetometer.
//These measurements are converted to Gauss, to read the raw values you
//can use device.magRawMeasurements .
Serial.print("[Magnetometer G (Gauss)]:");
Serial.print(" x : ");
Serial.print(device.magMeasurements[0]);
Serial.print(" y : ");
Serial.print(device.magMeasurements[1]);
Serial.print(" z : ");
Serial.print(device.magMeasurements[2]);
}//Author: Leonardo La Rocca
//email: info@melopero.com
//
//In this example it is shown how to configure the device to work with
//the SPI communication protocol.
//First make sure that your connections are setup correctly:
//SPI pinout:
//LSM9DS1 <------> Arduino MKR
// 3v3 <------> VCC
// SCL <------> SCK (9)
// SDA <------> MOSI (8)
// CSG <------> CS1 (6)
// CSM <------> CS2 (7)
// SDOG, SDOM <------> MISO (10)
//
//Note: SDOG and SDOM must be connected both to the same pin (MISO) therefore if
// you want to use both devices with the SPI protocol you have to connect
// SDOG and SDOM . (For example on a breadboard).
//Note: do not connect the device to the 5V pin!
#include <Melopero_LSM9DS1.h>
Melopero_LSM9DS1 device;
int gyroSelect = 6;
int magSelect = 7;
int waitMillis = 1000;
void setup() {
Serial.begin(9600);
//Setup the device to use SPI communication.
//device.getErrorString returns a readable interpretation for the
//status returned by most of the functions.
Serial.print("starting, setup SPI: ");
Serial.println(device.getErrorString(device.useSPI(gyroSelect, magSelect)));
//Reset all the settings
Serial.print("Resetting settings... ");
Serial.println(device.getErrorString(device.resetSettings()));
//Setup the output data rates
Serial.print("Setting Gyro output data rate: ");
Serial.println(device.setGyroODR(OutputDataRate::Hz_10));
Serial.print("Setting Accelerometer output data rate: ");
Serial.println(device.setAccODR(OutputDataRate::Hz_10));
Serial.print("Setting Magnetometer output data rate: ");
Serial.println(device.setMagODR(MagnetometerOutputDataRate::Hz_10));
}
void loop() {
delay(waitMillis);
//first update the measurements
device.updateGyroMeasurements();
device.updateAccMeasurements();
device.updateMagMeasurements();
//then print them out
Serial.println("******** measurements ********");
printGyroMeasurements();
Serial.println();
printAccMeasurements();
Serial.println();
printMagMeasurements();
Serial.println();
}
void printGyroMeasurements(){
//device.gyroMeasurements is an array of three elements (one for each axis)
//that contains the last measurements for the gyroscope.
//These measurements are converted to dps, to read the raw values you
//can use device.gyroRawMeasurements .
Serial.print("[Gyro dps (degrees per second)]:");
Serial.print(" x : ");
Serial.print(device.gyroMeasurements[0]);
Serial.print(" y : ");
Serial.print(device.gyroMeasurements[1]);
Serial.print(" z : ");
Serial.print(device.gyroMeasurements[2]);
}
void printAccMeasurements(){
//device.accMeasurements is an array of three elements (one for each axis)
//that contains the last measurements for the accelerometer.
//These measurements are converted to g, to read the raw values you
//can use device.accRawMeasurements .
Serial.print("[Accelerometer g (gravity)]:");
Serial.print(" x : ");
Serial.print(device.accMeasurements[0]);
Serial.print(" y : ");
Serial.print(device.accMeasurements[1]);
Serial.print(" z : ");
Serial.print(device.accMeasurements[2]);
}
void printMagMeasurements(){
//device.magMeasurements is an array of three elements (one for each axis)
//that contains the last measurements for the magnetometer.
//These measurements are converted to Gauss, to read the raw values you
//can use device.magRawMeasurements .
Serial.print("[Magnetometer G (Gauss)]:");
Serial.print(" x : ");
Serial.print(device.magMeasurements[0]);
Serial.print(" y : ");
Serial.print(device.magMeasurements[1]);
Serial.print(" z : ");
Serial.print(device.magMeasurements[2]);
}You'll find other examples using the interrupts pins HERE:
This breakout board is compatible only with 3.3V power and logic.
