Skip to content

Instantly share code, notes, and snippets.

@jterskine
Created August 15, 2019 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jterskine/fb22fe77225f21ac91ff1daa24a591f0 to your computer and use it in GitHub Desktop.
Save jterskine/fb22fe77225f21ac91ff1daa24a591f0 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM9DS1.h>
#include <Adafruit_Sensor.h> // not used in this demo but required!
// i2c
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();
#define LSM9DS1_SCK A5
#define LSM9DS1_MISO 12
#define LSM9DS1_MOSI A4
#define LSM9DS1_XGCS 6
#define LSM9DS1_MCS 5
// You can also use software SPI
//Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1(LSM9DS1_SCK, LSM9DS1_MISO, LSM9DS1_MOSI, LSM9DS1_XGCS, LSM9DS1_MCS);
// Or hardware SPI! In this case, only CS pins are passed in
//Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1(LSM9DS1_XGCS, LSM9DS1_MCS);
void setupSensor()
{
// 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);
}
void setup()
{
//For measuring serial write times
pinMode(9, OUTPUT);
Serial1.begin(115200);
while (!Serial1) {
delay(1);
}
//Serial.println("LSM9DS1 data read demo");
// Try to initialise and warn if we couldn't detect the chip
if (!lsm.begin())
{
//Serial.println("Oops ... unable to initialize the LSM9DS1. Check your wiring!");
//while (1);
}
//Serial.println("Found LSM9DS1 9DOF");
// helper to just set the default scaling we want, see above!
setupSensor();
}
static bool run_test = false;
void loop()
{
if(Serial1.available() > 0){
char c = Serial1.read();
if('s' == c){
run_test = true;
}else if ('p' == c){
run_test = false;
}
}
if(run_test){
lsm.read(); //ask it to read in the data */
//Get a new sensor event
sensors_event_t a, m, g, temp;
lsm.getEvent(&a, &m, &g, &temp);
digitalWrite(9,HIGH);
Serial1.write((const char *)a.acceleration.v,sizeof(a.acceleration.v));
Serial1.write((const char *)g.orientation.v,sizeof(g.orientation.v));
Serial1.write((const char *)m.magnetic.v,sizeof(m.magnetic.v));
digitalWrite(9,LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment