├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── githubci.yml ├── .gitignore ├── Adafruit_LSM9DS0.cpp ├── Adafruit_LSM9DS0.h ├── README.md ├── code-of-conduct.md ├── examples ├── lsm9doftest │ └── lsm9doftest.ino └── sensorapi │ └── sensorapi.ino ├── library.properties └── license.txt /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /.github/workflows/githubci.yml: -------------------------------------------------------------------------------- 1 | name: Arduino Library CI 2 | 3 | on: [pull_request, push, repository_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/setup-python@v4 11 | with: 12 | python-version: '3.x' 13 | - uses: actions/checkout@v3 14 | - uses: actions/checkout@v3 15 | with: 16 | repository: adafruit/ci-arduino 17 | path: ci 18 | 19 | - name: pre-install 20 | run: bash ci/actions_install.sh 21 | 22 | - name: test platforms 23 | run: python3 ci/build_platform.py main_platforms 24 | 25 | - name: clang 26 | run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . 27 | 28 | - name: doxygen 29 | env: 30 | GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} 31 | PRETTYNAME : "Adafruit LSM9DS0 Library" 32 | run: bash ci/doxy_gen_and_deploy.sh 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | html/ 2 | -------------------------------------------------------------------------------- /Adafruit_LSM9DS0.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_LSM9DS0.cpp 3 | * 4 | * @mainpage Adafruit LSM9DS0 Accelerometer 5 | * 6 | * @section intro_sec Introduction 7 | * 8 | * This is a library for the LSM9DS0 Accelerometer and magnentometer/compass 9 | * 10 | * Designed specifically to work with the Adafruit LSM9DS0 Breakouts 11 | * 12 | * These sensors use I2C to communicate, 2 pins are required to interface. 13 | * 14 | * Adafruit invests time and resources providing this open source code, 15 | * please support Adafruit andopen-source hardware by purchasing products 16 | * from Adafruit! 17 | * 18 | * @section author Author 19 | * 20 | * Written by Kevin Townsend for Adafruit Industries. 21 | * 22 | * @section license License 23 | * 24 | * BSD license, all text above must be included in any redistribution 25 | * 26 | */ 27 | #include 28 | 29 | /*************************************************************************** 30 | CONSTRUCTOR 31 | ***************************************************************************/ 32 | 33 | void Adafruit_LSM9DS0::initI2C(TwoWire *wireBus, int32_t sensorID) { 34 | _i2c = true; 35 | _wire = wireBus; 36 | _lsm9dso_sensorid_accel = sensorID + 1; 37 | _lsm9dso_sensorid_mag = sensorID + 2; 38 | _lsm9dso_sensorid_gyro = sensorID + 3; 39 | _lsm9dso_sensorid_temp = sensorID + 4; 40 | _accelSensor = Sensor(this, &Adafruit_LSM9DS0::readAccel, 41 | &Adafruit_LSM9DS0::getAccelEvent, 42 | &Adafruit_LSM9DS0::getAccelSensor); 43 | _magSensor = 44 | Sensor(this, &Adafruit_LSM9DS0::readMag, &Adafruit_LSM9DS0::getMagEvent, 45 | &Adafruit_LSM9DS0::getMagSensor); 46 | _gyroSensor = 47 | Sensor(this, &Adafruit_LSM9DS0::readGyro, &Adafruit_LSM9DS0::getGyroEvent, 48 | &Adafruit_LSM9DS0::getGyroSensor); 49 | _tempSensor = 50 | Sensor(this, &Adafruit_LSM9DS0::readTemp, &Adafruit_LSM9DS0::getTempEvent, 51 | &Adafruit_LSM9DS0::getTempSensor); 52 | } 53 | 54 | /**************************************************************************/ 55 | /*! 56 | @brief Instantiate with default hardware I2C interface 57 | @param sensorID Unique identifier you'd like for the sensors 58 | */ 59 | /**************************************************************************/ 60 | Adafruit_LSM9DS0::Adafruit_LSM9DS0(int32_t sensorID) { 61 | initI2C(&Wire, sensorID); 62 | } 63 | 64 | /**************************************************************************/ 65 | /*! 66 | @brief Instantiate with hardware I2C interface 67 | @param wireBus The I2C wire interface you'd like to use 68 | @param sensorID Unique identifier you'd like for the sensors 69 | */ 70 | /**************************************************************************/ 71 | Adafruit_LSM9DS0::Adafruit_LSM9DS0(TwoWire *wireBus, int32_t sensorID) { 72 | initI2C(wireBus, sensorID); 73 | } 74 | 75 | /**************************************************************************/ 76 | /*! 77 | @brief Instantiate with hardware SPI interface 78 | @param xmcs SPI CS pin for accelerometer/mag subchip 79 | @param gcs SPI CS pin for gyro subchip 80 | @param sensorID Unique identifier you'd like for the sensors 81 | */ 82 | /**************************************************************************/ 83 | Adafruit_LSM9DS0::Adafruit_LSM9DS0(int8_t xmcs, int8_t gcs, int32_t sensorID) { 84 | _i2c = false; 85 | // hardware SPI! 86 | _csg = gcs; 87 | _csxm = xmcs; 88 | _mosi = _miso = _clk = -1; 89 | _lsm9dso_sensorid_accel = sensorID + 1; 90 | _lsm9dso_sensorid_mag = sensorID + 2; 91 | _lsm9dso_sensorid_gyro = sensorID + 3; 92 | _lsm9dso_sensorid_temp = sensorID + 4; 93 | _accelSensor = Sensor(this, &Adafruit_LSM9DS0::readAccel, 94 | &Adafruit_LSM9DS0::getAccelEvent, 95 | &Adafruit_LSM9DS0::getAccelSensor); 96 | _magSensor = 97 | Sensor(this, &Adafruit_LSM9DS0::readMag, &Adafruit_LSM9DS0::getMagEvent, 98 | &Adafruit_LSM9DS0::getMagSensor); 99 | _gyroSensor = 100 | Sensor(this, &Adafruit_LSM9DS0::readGyro, &Adafruit_LSM9DS0::getGyroEvent, 101 | &Adafruit_LSM9DS0::getGyroSensor); 102 | _tempSensor = 103 | Sensor(this, &Adafruit_LSM9DS0::readTemp, &Adafruit_LSM9DS0::getTempEvent, 104 | &Adafruit_LSM9DS0::getTempSensor); 105 | } 106 | 107 | /**************************************************************************/ 108 | /*! 109 | @brief Instantiate with software SPI interface 110 | @param clk SPI clock pin 111 | @param miso SPI MISO pin 112 | @param mosi SPI MOSI pin 113 | @param xmcs SPI CS pin for accelerometer/mag subchip 114 | @param gcs SPI CS pin for gyro subchip 115 | @param sensorID Unique identifier you'd like for the sensors 116 | */ 117 | /**************************************************************************/ 118 | Adafruit_LSM9DS0::Adafruit_LSM9DS0(int8_t clk, int8_t miso, int8_t mosi, 119 | int8_t xmcs, int8_t gcs, int32_t sensorID) { 120 | _i2c = false; 121 | // software SPI! 122 | _csg = gcs; 123 | _csxm = xmcs; 124 | _mosi = mosi; 125 | _miso = miso; 126 | _clk = clk; 127 | _lsm9dso_sensorid_accel = sensorID + 1; 128 | _lsm9dso_sensorid_mag = sensorID + 2; 129 | _lsm9dso_sensorid_gyro = sensorID + 3; 130 | _lsm9dso_sensorid_temp = sensorID + 4; 131 | _accelSensor = Sensor(this, &Adafruit_LSM9DS0::readAccel, 132 | &Adafruit_LSM9DS0::getAccelEvent, 133 | &Adafruit_LSM9DS0::getAccelSensor); 134 | _magSensor = 135 | Sensor(this, &Adafruit_LSM9DS0::readMag, &Adafruit_LSM9DS0::getMagEvent, 136 | &Adafruit_LSM9DS0::getMagSensor); 137 | _gyroSensor = 138 | Sensor(this, &Adafruit_LSM9DS0::readGyro, &Adafruit_LSM9DS0::getGyroEvent, 139 | &Adafruit_LSM9DS0::getGyroSensor); 140 | _tempSensor = 141 | Sensor(this, &Adafruit_LSM9DS0::readTemp, &Adafruit_LSM9DS0::getTempEvent, 142 | &Adafruit_LSM9DS0::getTempSensor); 143 | } 144 | 145 | /**************************************************************************/ 146 | /*! 147 | @brief Initialize I2C or SPI and detect/initialize subsensors 148 | @returns True if both subsensors were detected on the desired interface 149 | */ 150 | /**************************************************************************/ 151 | bool Adafruit_LSM9DS0::begin() { 152 | if (_i2c) { 153 | _wire->begin(); 154 | } else if (_clk == -1) { 155 | // Hardware SPI 156 | pinMode(_csxm, OUTPUT); 157 | pinMode(_csg, OUTPUT); 158 | digitalWrite(_csxm, HIGH); 159 | digitalWrite(_csg, HIGH); 160 | SPI.begin(); 161 | } else { 162 | // Sofware SPI 163 | pinMode(_clk, OUTPUT); 164 | pinMode(_mosi, OUTPUT); 165 | pinMode(_csxm, OUTPUT); 166 | pinMode(_csg, OUTPUT); 167 | digitalWrite(_csxm, HIGH); 168 | digitalWrite(_csg, HIGH); 169 | digitalWrite(_clk, HIGH); 170 | } 171 | 172 | uint8_t id = read8(XMTYPE, LSM9DS0_REGISTER_WHO_AM_I_XM); 173 | // Serial.print ("XM whoami: 0x"); 174 | // Serial.println(id, HEX); 175 | if (id != LSM9DS0_XM_ID) 176 | return false; 177 | 178 | id = read8(GYROTYPE, LSM9DS0_REGISTER_WHO_AM_I_G); 179 | // Serial.print ("G whoami: 0x"); 180 | // Serial.println(id, HEX); 181 | if (id != LSM9DS0_G_ID) 182 | return false; 183 | 184 | // Enable the accelerometer continous 185 | write8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG1_XM, 0x67); // 100hz XYZ 186 | write8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG5_XM, 0b11110000); 187 | // enable mag continuous 188 | write8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG7_XM, 0b00000000); 189 | // enable gyro continuous 190 | write8(GYROTYPE, LSM9DS0_REGISTER_CTRL_REG1_G, 0x0F); // on XYZ 191 | // enable the temperature sensor (output rate same as the mag sensor) 192 | uint8_t tempReg = read8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG5_XM); 193 | write8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG5_XM, tempReg | (1 << 7)); 194 | 195 | /* 196 | for (uint8_t i=0; i<0x30; i++) { 197 | Serial.print("$"); Serial.print(i, HEX); 198 | Serial.print(" = 0x"); 199 | Serial.println(read8(XMTYPE, i), HEX); 200 | } 201 | */ 202 | 203 | // Set default ranges for the various sensors 204 | setupAccel(LSM9DS0_ACCELRANGE_2G); 205 | setupMag(LSM9DS0_MAGGAIN_2GAUSS); 206 | setupGyro(LSM9DS0_GYROSCALE_245DPS); 207 | 208 | return true; 209 | } 210 | 211 | /*************************************************************************** 212 | PUBLIC FUNCTIONS 213 | ***************************************************************************/ 214 | 215 | /**************************************************************************/ 216 | /*! 217 | @brief Read all four sensor subcomponents 218 | */ 219 | /**************************************************************************/ 220 | void Adafruit_LSM9DS0::read() { 221 | /* Read all the sensors. */ 222 | readAccel(); 223 | readMag(); 224 | readGyro(); 225 | readTemp(); 226 | } 227 | 228 | /**************************************************************************/ 229 | /*! 230 | @brief Read the sensor accelerometer sensor component 231 | */ 232 | /**************************************************************************/ 233 | void Adafruit_LSM9DS0::readAccel() { 234 | // Read the accelerometer 235 | byte buffer[6]; 236 | readBuffer(XMTYPE, 0x80 | LSM9DS0_REGISTER_OUT_X_L_A, 6, buffer); 237 | 238 | uint8_t xlo = buffer[0]; 239 | int16_t xhi = buffer[1]; 240 | uint8_t ylo = buffer[2]; 241 | int16_t yhi = buffer[3]; 242 | uint8_t zlo = buffer[4]; 243 | int16_t zhi = buffer[5]; 244 | 245 | // Shift values to create properly formed integer (low byte first) 246 | xhi <<= 8; 247 | xhi |= xlo; 248 | yhi <<= 8; 249 | yhi |= ylo; 250 | zhi <<= 8; 251 | zhi |= zlo; 252 | accelData.x = xhi; 253 | accelData.y = yhi; 254 | accelData.z = zhi; 255 | } 256 | 257 | /**************************************************************************/ 258 | /*! 259 | @brief Read the sensor magnetometer sensor component 260 | */ 261 | /**************************************************************************/ 262 | void Adafruit_LSM9DS0::readMag() { 263 | // Read the magnetometer 264 | byte buffer[6]; 265 | readBuffer(XMTYPE, 0x80 | LSM9DS0_REGISTER_OUT_X_L_M, 6, buffer); 266 | 267 | uint8_t xlo = buffer[0]; 268 | int16_t xhi = buffer[1]; 269 | uint8_t ylo = buffer[2]; 270 | int16_t yhi = buffer[3]; 271 | uint8_t zlo = buffer[4]; 272 | int16_t zhi = buffer[5]; 273 | 274 | // Shift values to create properly formed integer (low byte first) 275 | xhi <<= 8; 276 | xhi |= xlo; 277 | yhi <<= 8; 278 | yhi |= ylo; 279 | zhi <<= 8; 280 | zhi |= zlo; 281 | magData.x = xhi; 282 | magData.y = yhi; 283 | magData.z = zhi; 284 | } 285 | 286 | /**************************************************************************/ 287 | /*! 288 | @brief Read the sensor gyroscope sensor component 289 | */ 290 | /**************************************************************************/ 291 | void Adafruit_LSM9DS0::readGyro() { 292 | // Read gyro 293 | byte buffer[6]; 294 | readBuffer(GYROTYPE, 0x80 | LSM9DS0_REGISTER_OUT_X_L_G, 6, buffer); 295 | 296 | uint8_t xlo = buffer[0]; 297 | int16_t xhi = buffer[1]; 298 | uint8_t ylo = buffer[2]; 299 | int16_t yhi = buffer[3]; 300 | uint8_t zlo = buffer[4]; 301 | int16_t zhi = buffer[5]; 302 | 303 | // Shift values to create properly formed integer (low byte first) 304 | xhi <<= 8; 305 | xhi |= xlo; 306 | yhi <<= 8; 307 | yhi |= ylo; 308 | zhi <<= 8; 309 | zhi |= zlo; 310 | 311 | gyroData.x = xhi; 312 | gyroData.y = yhi; 313 | gyroData.z = zhi; 314 | } 315 | 316 | /**************************************************************************/ 317 | /*! 318 | @brief Read the sensor temperature sensor component 319 | */ 320 | /**************************************************************************/ 321 | void Adafruit_LSM9DS0::readTemp() { 322 | // Read temp sensor 323 | byte buffer[2]; 324 | readBuffer(XMTYPE, 0x80 | LSM9DS0_REGISTER_TEMP_OUT_L_XM, 2, buffer); 325 | uint8_t xlo = buffer[0]; 326 | int16_t xhi = buffer[1]; 327 | 328 | xhi <<= 8; 329 | xhi |= xlo; 330 | 331 | // Shift values to create properly formed integer (low byte first) 332 | temperature = xhi; 333 | } 334 | 335 | /**************************************************************************/ 336 | /*! 337 | @brief Configure the accelerometer ranging 338 | @param range Can be LSM9DS0_ACCELRANGE_2G, LSM9DS0_ACCELRANGE_4G, 339 | LSM9DS0_ACCELRANGE_6G, LSM9DS0_ACCELRANGE_8G, LSM9DS0_ACCELRANGE_16G 340 | */ 341 | /**************************************************************************/ 342 | void Adafruit_LSM9DS0::setupAccel(lsm9ds0AccelRange_t range) { 343 | uint8_t reg = read8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG2_XM); 344 | reg &= ~(0b00111000); 345 | reg |= range; 346 | write8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG2_XM, reg); 347 | 348 | switch (range) { 349 | case LSM9DS0_ACCELRANGE_2G: 350 | _accel_mg_lsb = LSM9DS0_ACCEL_MG_LSB_2G; 351 | break; 352 | case LSM9DS0_ACCELRANGE_4G: 353 | _accel_mg_lsb = LSM9DS0_ACCEL_MG_LSB_4G; 354 | break; 355 | case LSM9DS0_ACCELRANGE_6G: 356 | _accel_mg_lsb = LSM9DS0_ACCEL_MG_LSB_6G; 357 | break; 358 | case LSM9DS0_ACCELRANGE_8G: 359 | _accel_mg_lsb = LSM9DS0_ACCEL_MG_LSB_8G; 360 | break; 361 | case LSM9DS0_ACCELRANGE_16G: 362 | _accel_mg_lsb = LSM9DS0_ACCEL_MG_LSB_16G; 363 | break; 364 | } 365 | } 366 | 367 | /**************************************************************************/ 368 | /*! 369 | @brief Configure the magnetometer gain 370 | @param gain Can be LSM9DS0_MAGGAIN_2GAUSS, LSM9DS0_MAGGAIN_4GAUSS, 371 | LSM9DS0_MAGGAIN_8GAUSS, LSM9DS0_MAGGAIN_12GAUSS 372 | */ 373 | /**************************************************************************/ 374 | void Adafruit_LSM9DS0::setupMag(lsm9ds0MagGain_t gain) { 375 | uint8_t reg = read8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG6_XM); 376 | reg &= ~(0b01100000); 377 | reg |= gain; 378 | write8(XMTYPE, LSM9DS0_REGISTER_CTRL_REG6_XM, reg); 379 | 380 | switch (gain) { 381 | case LSM9DS0_MAGGAIN_2GAUSS: 382 | _mag_mgauss_lsb = LSM9DS0_MAG_MGAUSS_2GAUSS; 383 | break; 384 | case LSM9DS0_MAGGAIN_4GAUSS: 385 | _mag_mgauss_lsb = LSM9DS0_MAG_MGAUSS_4GAUSS; 386 | break; 387 | case LSM9DS0_MAGGAIN_8GAUSS: 388 | _mag_mgauss_lsb = LSM9DS0_MAG_MGAUSS_8GAUSS; 389 | break; 390 | case LSM9DS0_MAGGAIN_12GAUSS: 391 | _mag_mgauss_lsb = LSM9DS0_MAG_MGAUSS_12GAUSS; 392 | break; 393 | } 394 | } 395 | 396 | /**************************************************************************/ 397 | /*! 398 | @brief Configure the gyroscope scaling 399 | @param scale Can be LSM9DS0_GYROSCALE_245DPS, LSM9DS0_GYROSCALE_500DPS 400 | or LSM9DS0_GYROSCALE_2000DPS 401 | */ 402 | /**************************************************************************/ 403 | void Adafruit_LSM9DS0::setupGyro(lsm9ds0GyroScale_t scale) { 404 | uint8_t reg = read8(GYROTYPE, LSM9DS0_REGISTER_CTRL_REG4_G); 405 | reg &= ~(0b00110000); 406 | reg |= scale; 407 | write8(GYROTYPE, LSM9DS0_REGISTER_CTRL_REG4_G, reg); 408 | 409 | switch (scale) { 410 | case LSM9DS0_GYROSCALE_245DPS: 411 | _gyro_dps_digit = LSM9DS0_GYRO_DPS_DIGIT_245DPS; 412 | break; 413 | case LSM9DS0_GYROSCALE_500DPS: 414 | _gyro_dps_digit = LSM9DS0_GYRO_DPS_DIGIT_500DPS; 415 | break; 416 | case LSM9DS0_GYROSCALE_2000DPS: 417 | _gyro_dps_digit = LSM9DS0_GYRO_DPS_DIGIT_2000DPS; 418 | break; 419 | } 420 | } 421 | 422 | /*************************************************************************** 423 | UNIFIED SENSOR FUNCTIONS 424 | ***************************************************************************/ 425 | 426 | /**************************************************************************/ 427 | /*! 428 | @brief Gets the most recent accel sensor events for all 4 sensors 429 | @param accelEvent The accelerometer event object we will fill, pass NULL to 430 | skip 431 | @param magEvent The magnetometer event object we will fill, pass NULL to 432 | skip 433 | @param gyroEvent The gyroscope event object we will fill, pass NULL to skip 434 | @param tempEvent The temperature event object we will fill, pass NULL to 435 | skip 436 | @returns True on successful reads 437 | */ 438 | /**************************************************************************/ 439 | bool Adafruit_LSM9DS0::getEvent(sensors_event_t *accelEvent, 440 | sensors_event_t *magEvent, 441 | sensors_event_t *gyroEvent, 442 | sensors_event_t *tempEvent) { 443 | /* Grab new sensor reading and timestamp. */ 444 | read(); 445 | uint32_t timestamp = millis(); 446 | 447 | /* Update appropriate sensor events. */ 448 | if (accelEvent) 449 | getAccelEvent(accelEvent, timestamp); 450 | if (magEvent) 451 | getMagEvent(magEvent, timestamp); 452 | if (gyroEvent) 453 | getGyroEvent(gyroEvent, timestamp); 454 | if (tempEvent) 455 | getTempEvent(tempEvent, timestamp); 456 | 457 | return true; 458 | } 459 | 460 | /**************************************************************************/ 461 | /*! 462 | @brief Gets the sensor_t data for all 4 sub-sensors at once call 463 | @param accel The accelerometer sensor_t object we will fill, pass NULL to 464 | skip 465 | @param mag The magnetometer sensor_t object we will fill, pass NULL to skip 466 | @param gyro The gyroscope sensor_t object we will fill, pass NULL to skip 467 | @param temp The temperature sensor_t object we will fill, pass NULL to skip 468 | */ 469 | /**************************************************************************/ 470 | void Adafruit_LSM9DS0::getSensor(sensor_t *accel, sensor_t *mag, sensor_t *gyro, 471 | sensor_t *temp) { 472 | /* Update appropriate sensor metadata. */ 473 | if (accel) 474 | getAccelSensor(accel); 475 | if (mag) 476 | getMagSensor(mag); 477 | if (gyro) 478 | getGyroSensor(gyro); 479 | if (temp) 480 | getTempSensor(temp); 481 | } 482 | 483 | /*************************************************************************** 484 | PRIVATE FUNCTIONS 485 | ***************************************************************************/ 486 | void Adafruit_LSM9DS0::write8(boolean type, byte reg, byte value) { 487 | byte address, _cs; 488 | 489 | if (type == GYROTYPE) { 490 | address = LSM9DS0_ADDRESS_GYRO; 491 | _cs = _csg; 492 | } else { 493 | address = LSM9DS0_ADDRESS_ACCELMAG; 494 | _cs = _csxm; 495 | } 496 | if (_i2c) { 497 | _wire->beginTransmission(address); 498 | _wire->write(reg); 499 | _wire->write(value); 500 | _wire->endTransmission(); 501 | } else { 502 | SPI.beginTransaction(SPISettings(200000, MSBFIRST, SPI_MODE0)); 503 | digitalWrite(_cs, LOW); 504 | // set address 505 | spixfer(reg | 0x40); // write multiple 506 | spixfer(value); 507 | digitalWrite(_cs, HIGH); 508 | SPI.endTransaction(); 509 | } 510 | } 511 | 512 | byte Adafruit_LSM9DS0::read8(boolean type, byte reg) { 513 | uint8_t value; 514 | 515 | readBuffer(type, reg, 1, &value); 516 | 517 | return value; 518 | } 519 | 520 | byte Adafruit_LSM9DS0::readBuffer(boolean type, byte reg, byte len, 521 | uint8_t *buffer) { 522 | byte address, _cs; 523 | 524 | if (type == GYROTYPE) { 525 | address = LSM9DS0_ADDRESS_GYRO; 526 | _cs = _csg; 527 | } else { 528 | address = LSM9DS0_ADDRESS_ACCELMAG; 529 | _cs = _csxm; 530 | } 531 | 532 | if (_i2c) { 533 | #ifdef __SAM3X8E__ 534 | _wire->requestFrom( 535 | address, len, reg, 1, 536 | true); // see 537 | // http://forum.arduino.cc/index.php?topic=385377.msg2947227#msg2947227 538 | #else 539 | _wire->beginTransmission(address); 540 | _wire->write(reg); 541 | _wire->endTransmission(); 542 | _wire->requestFrom(address, (byte)len); 543 | #endif 544 | 545 | for (uint8_t i = 0; i < len; i++) { 546 | buffer[i] = _wire->read(); 547 | } 548 | } else { 549 | SPI.beginTransaction(SPISettings(200000, MSBFIRST, SPI_MODE0)); 550 | digitalWrite(_cs, LOW); 551 | // set address 552 | spixfer(reg | 0x80 | 0x40); // read multiple 553 | for (uint8_t i = 0; i < len; i++) { 554 | buffer[i] = spixfer(0); 555 | } 556 | digitalWrite(_cs, HIGH); 557 | SPI.endTransaction(); 558 | } 559 | 560 | return len; 561 | } 562 | 563 | uint8_t Adafruit_LSM9DS0::spixfer(uint8_t data) { 564 | 565 | if (_clk == -1) { 566 | // Serial.println("Hardware SPI"); 567 | return SPI.transfer(data); 568 | } else { 569 | // Serial.println("Software SPI"); 570 | uint8_t reply = 0; 571 | for (int i = 7; i >= 0; i--) { 572 | reply <<= 1; 573 | digitalWrite(_clk, LOW); 574 | digitalWrite(_mosi, data & (1 << i)); 575 | digitalWrite(_clk, HIGH); 576 | if (digitalRead(_miso)) 577 | reply |= 1; 578 | } 579 | return reply; 580 | } 581 | } 582 | 583 | /**************************************************************************/ 584 | /*! 585 | @brief Fill in the details about the most recent accelerometer data read 586 | @param event The sensor_event_t object we will fill! 587 | @param timestamp Unused 588 | */ 589 | /**************************************************************************/ 590 | void Adafruit_LSM9DS0::getAccelEvent(sensors_event_t *event, 591 | uint32_t timestamp) { 592 | memset(event, 0, sizeof(sensors_event_t)); 593 | event->version = sizeof(sensors_event_t); 594 | event->sensor_id = _lsm9dso_sensorid_accel; 595 | event->type = SENSOR_TYPE_ACCELEROMETER; 596 | event->timestamp = timestamp; 597 | event->acceleration.x = accelData.x * _accel_mg_lsb; 598 | event->acceleration.x /= 1000; 599 | event->acceleration.x *= SENSORS_GRAVITY_STANDARD; 600 | event->acceleration.y = accelData.y * _accel_mg_lsb; 601 | event->acceleration.y /= 1000; 602 | event->acceleration.y *= SENSORS_GRAVITY_STANDARD; 603 | event->acceleration.z = accelData.z * _accel_mg_lsb; 604 | event->acceleration.z /= 1000; 605 | event->acceleration.z *= SENSORS_GRAVITY_STANDARD; 606 | } 607 | 608 | /**************************************************************************/ 609 | /*! 610 | @brief Fill in the details about the most recent magnetometer data read 611 | @param event The sensor_event_t object we will fill! 612 | @param timestamp Unused 613 | */ 614 | /**************************************************************************/ 615 | void Adafruit_LSM9DS0::getMagEvent(sensors_event_t *event, uint32_t timestamp) { 616 | memset(event, 0, sizeof(sensors_event_t)); 617 | event->version = sizeof(sensors_event_t); 618 | event->sensor_id = _lsm9dso_sensorid_mag; 619 | event->type = SENSOR_TYPE_MAGNETIC_FIELD; 620 | event->timestamp = timestamp; 621 | event->magnetic.x = magData.x * _mag_mgauss_lsb / 10; 622 | event->magnetic.y = magData.y * _mag_mgauss_lsb / 10; 623 | event->magnetic.z = magData.z * _mag_mgauss_lsb / 10; 624 | } 625 | 626 | /**************************************************************************/ 627 | /*! 628 | @brief Fill in the details about the most recent gyroscope data read 629 | @param event The sensor_event_t object we will fill! 630 | @param timestamp The millis timestamp when the read occured 631 | */ 632 | /**************************************************************************/ 633 | void Adafruit_LSM9DS0::getGyroEvent(sensors_event_t *event, 634 | uint32_t timestamp) { 635 | memset(event, 0, sizeof(sensors_event_t)); 636 | event->version = sizeof(sensors_event_t); 637 | event->sensor_id = _lsm9dso_sensorid_accel; 638 | event->type = SENSOR_TYPE_GYROSCOPE; 639 | event->timestamp = timestamp; 640 | event->gyro.x = gyroData.x * _gyro_dps_digit * SENSORS_DPS_TO_RADS; 641 | event->gyro.y = gyroData.y * _gyro_dps_digit * SENSORS_DPS_TO_RADS; 642 | event->gyro.z = gyroData.z * _gyro_dps_digit * SENSORS_DPS_TO_RADS; 643 | } 644 | 645 | /**************************************************************************/ 646 | /*! 647 | @brief Fill in the details about the most recent temperature data read 648 | @param event The sensor_event_t object we will fill! 649 | @param timestamp The millis timestamp when the read occured 650 | */ 651 | /**************************************************************************/ 652 | void Adafruit_LSM9DS0::getTempEvent(sensors_event_t *event, 653 | uint32_t timestamp) { 654 | memset(event, 0, sizeof(sensors_event_t)); 655 | event->version = sizeof(sensors_event_t); 656 | event->sensor_id = _lsm9dso_sensorid_temp; 657 | event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; 658 | event->timestamp = timestamp; 659 | // This is just a guess since the staring point (21C here) isn't documented :( 660 | event->temperature = 21.0 + (float)temperature / 8; 661 | // event->temperature /= LSM9DS0_TEMP_LSB_DEGREE_CELSIUS; 662 | } 663 | 664 | /**************************************************************************/ 665 | /*! 666 | @brief Fill in the details about the accelerometer sensor component 667 | @param sensor The sensor_t object we will fill! 668 | */ 669 | /**************************************************************************/ 670 | void Adafruit_LSM9DS0::getAccelSensor(sensor_t *sensor) { 671 | memset(sensor, 0, sizeof(sensor_t)); 672 | strncpy(sensor->name, "LSM9DS0_A", sizeof(sensor->name) - 1); 673 | sensor->name[sizeof(sensor->name) - 1] = 0; 674 | sensor->version = 1; 675 | sensor->sensor_id = _lsm9dso_sensorid_accel; 676 | sensor->type = SENSOR_TYPE_ACCELEROMETER; 677 | sensor->min_delay = 0; 678 | sensor->max_value = 0.0; // ToDo 679 | sensor->min_value = 0.0; // ToDo 680 | sensor->resolution = 0.0; // ToDo 681 | } 682 | 683 | /**************************************************************************/ 684 | /*! 685 | @brief Fill in the details about the magnetometer sensor component 686 | @param sensor The sensor_t object we will fill! 687 | */ 688 | /**************************************************************************/ 689 | void Adafruit_LSM9DS0::getMagSensor(sensor_t *sensor) { 690 | memset(sensor, 0, sizeof(sensor_t)); 691 | strncpy(sensor->name, "LSM9DS0_M", sizeof(sensor->name) - 1); 692 | sensor->name[sizeof(sensor->name) - 1] = 0; 693 | sensor->version = 1; 694 | sensor->sensor_id = _lsm9dso_sensorid_mag; 695 | sensor->type = SENSOR_TYPE_MAGNETIC_FIELD; 696 | sensor->min_delay = 0; 697 | sensor->max_value = 0.0; // ToDo 698 | sensor->min_value = 0.0; // ToDo 699 | sensor->resolution = 0.0; // ToDo 700 | } 701 | 702 | /**************************************************************************/ 703 | /*! 704 | @brief Fill in the details about the gyroscope sensor component 705 | @param sensor The sensor_t object we will fill! 706 | */ 707 | /**************************************************************************/ 708 | void Adafruit_LSM9DS0::getGyroSensor(sensor_t *sensor) { 709 | memset(sensor, 0, sizeof(sensor_t)); 710 | strncpy(sensor->name, "LSM9DS0_G", sizeof(sensor->name) - 1); 711 | sensor->name[sizeof(sensor->name) - 1] = 0; 712 | sensor->version = 1; 713 | sensor->sensor_id = _lsm9dso_sensorid_gyro; 714 | sensor->type = SENSOR_TYPE_GYROSCOPE; 715 | sensor->min_delay = 0; 716 | sensor->max_value = 0.0; // ToDo 717 | sensor->min_value = 0.0; // ToDo 718 | sensor->resolution = 0.0; // ToDo 719 | } 720 | 721 | /**************************************************************************/ 722 | /*! 723 | @brief Fill in the details about the temperature sensor component 724 | @param sensor The sensor_t object we will fill! 725 | */ 726 | /**************************************************************************/ 727 | void Adafruit_LSM9DS0::getTempSensor(sensor_t *sensor) { 728 | memset(sensor, 0, sizeof(sensor_t)); 729 | strncpy(sensor->name, "LSM9DS0_T", sizeof(sensor->name) - 1); 730 | sensor->name[sizeof(sensor->name) - 1] = 0; 731 | sensor->version = 1; 732 | sensor->sensor_id = _lsm9dso_sensorid_temp; 733 | sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; 734 | sensor->min_delay = 0; 735 | sensor->max_value = 0.0; // ToDo 736 | sensor->min_value = 0.0; // ToDo 737 | sensor->resolution = 0.0; // ToDo 738 | } 739 | -------------------------------------------------------------------------------- /Adafruit_LSM9DS0.h: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_LSM9DS0.h 3 | * 4 | */ 5 | 6 | #ifndef __LSM9DS0_H__ 7 | #define __LSM9DS0_H__ 8 | 9 | #if (ARDUINO >= 100) 10 | #include "Arduino.h" 11 | #else 12 | #include "WProgram.h" 13 | #endif 14 | #include "Wire.h" 15 | #include 16 | #include 17 | 18 | #define LSM9DS0_ADDRESS_ACCELMAG (0x1D) //!< 3B >> 1 = 7bit default 19 | #define LSM9DS0_ADDRESS_GYRO (0x6B) //!< D6 >> 1 = 7bit default 20 | #define LSM9DS0_XM_ID \ 21 | (0b01001001) //!< Accelerometer & magnetometer ID register, WHO_AM_I_XM 22 | #define LSM9DS0_G_ID (0b11010100) //!< Gyroscope ID register, WHO_AM_I_G 23 | 24 | // Linear Acceleration: mg per LSB 25 | #define LSM9DS0_ACCEL_MG_LSB_2G \ 26 | (0.061F) //!< Linear acceleration sensitivity FS=+-2g 27 | #define LSM9DS0_ACCEL_MG_LSB_4G \ 28 | (0.122F) //!< Linear acceleration sensitivity FS=+-4g 29 | 30 | #define LSM9DS0_ACCEL_MG_LSB_6G \ 31 | (0.183F) //!< Linear acceleration sensitivity FS=+-6g 32 | #define LSM9DS0_ACCEL_MG_LSB_8G \ 33 | (0.244F) //!< Linear acceleration sensitivity FS=+-8g 34 | #define LSM9DS0_ACCEL_MG_LSB_16G \ 35 | (0.732F) //!< Linear acceleration sensitivity FS=+-16g 36 | 37 | // Magnetic Field Strength: gauss range 38 | #define LSM9DS0_MAG_MGAUSS_2GAUSS (0.08F) //!< Magnetic sensitivity FS=+-2 gauss 39 | #define LSM9DS0_MAG_MGAUSS_4GAUSS (0.16F) //!< Magnetic sensitivity FS=+-4 gauss 40 | #define LSM9DS0_MAG_MGAUSS_8GAUSS (0.32F) //!< Magnetic sensitivity FS=+-8 gauss 41 | #define LSM9DS0_MAG_MGAUSS_12GAUSS \ 42 | (0.48F) //!< Magnetic sensitivity FS=+-12 gauss 43 | 44 | // Angular Rate: dps per LSB 45 | #define LSM9DS0_GYRO_DPS_DIGIT_245DPS \ 46 | (0.00875F) //!< Angular rate sensitivity FS=+-245dps 47 | #define LSM9DS0_GYRO_DPS_DIGIT_500DPS \ 48 | (0.01750F) //!< Angular rate sensitivity FS=+-500dps 49 | #define LSM9DS0_GYRO_DPS_DIGIT_2000DPS \ 50 | (0.07000F) //!< Angular rate sensitivity FS=+-2000dps 51 | 52 | // Temperature: LSB per degree celsius 53 | #define LSM9DS0_TEMP_LSB_DEGREE_CELSIUS \ 54 | (8) //!< Temperature sensor output change vs. temperature. 1°C = 8, 25° = 200, 55 | //!< etc. 56 | 57 | #define GYROTYPE (true) //!< Used to enable gyroscope device 58 | #define XMTYPE (false) //!< Used to enable accellerometer & magnetometer device 59 | 60 | /* Forward reference required for function pointers below. */ 61 | class Adafruit_LSM9DS0; 62 | 63 | /* Pointer to member functions for read, get event, and get sensor. These are 64 | * used */ 65 | /* by the Adafruit_LSM9DS0::Sensor class to read and retrieve individual 66 | * sensors. */ 67 | typedef void (Adafruit_LSM9DS0::*lsm9ds0_read_func)( 68 | void); //!< Pointer to member functions to read. Used by 69 | //!< Adafruit_LSM9DS0::Sensor class. 70 | typedef void (Adafruit_LSM9DS0::*lsm9ds0_get_event_func)( 71 | sensors_event_t *, uint32_t); //!< Pointer to member functions to get event. 72 | //!< Used by Adafruit_LSM9DS0::Sensor class. 73 | typedef void (Adafruit_LSM9DS0::*lsm9ds0_get_sensor_func)( 74 | sensor_t *); //!< Pointer to member functions to get sensor function. Used 75 | //!< by Adafruit_LSM9DS0::Sensor class. 76 | 77 | /**! Interface object for LSM9DS0 9-DoF sensor */ 78 | class Adafruit_LSM9DS0 { 79 | public: 80 | Adafruit_LSM9DS0(int32_t sensorID = 0); 81 | Adafruit_LSM9DS0(TwoWire *wireBus, int32_t sensorID = 0); 82 | Adafruit_LSM9DS0(int8_t xmcs, int8_t gcs, int32_t sensorID = 0); 83 | Adafruit_LSM9DS0(int8_t clk, int8_t miso, int8_t mosi, int8_t xmcs, 84 | int8_t gcs, int32_t sensorID = 0); 85 | 86 | /**! Register mapping for gyro component */ 87 | typedef enum { 88 | LSM9DS0_REGISTER_WHO_AM_I_G = 0x0F, 89 | LSM9DS0_REGISTER_CTRL_REG1_G = 0x20, 90 | LSM9DS0_REGISTER_CTRL_REG3_G = 0x22, 91 | LSM9DS0_REGISTER_CTRL_REG4_G = 0x23, 92 | LSM9DS0_REGISTER_OUT_X_L_G = 0x28, 93 | LSM9DS0_REGISTER_OUT_X_H_G = 0x29, 94 | LSM9DS0_REGISTER_OUT_Y_L_G = 0x2A, 95 | LSM9DS0_REGISTER_OUT_Y_H_G = 0x2B, 96 | LSM9DS0_REGISTER_OUT_Z_L_G = 0x2C, 97 | LSM9DS0_REGISTER_OUT_Z_H_G = 0x2D, 98 | } lsm9ds0GyroRegisters_t; 99 | 100 | /**! Register mapping for accelerometer/mag component */ 101 | typedef enum { 102 | LSM9DS0_REGISTER_TEMP_OUT_L_XM = 0x05, 103 | LSM9DS0_REGISTER_TEMP_OUT_H_XM = 0x06, 104 | LSM9DS0_REGISTER_STATUS_REG_M = 0x07, 105 | LSM9DS0_REGISTER_OUT_X_L_M = 0x08, 106 | LSM9DS0_REGISTER_OUT_X_H_M = 0x09, 107 | LSM9DS0_REGISTER_OUT_Y_L_M = 0x0A, 108 | LSM9DS0_REGISTER_OUT_Y_H_M = 0x0B, 109 | LSM9DS0_REGISTER_OUT_Z_L_M = 0x0C, 110 | LSM9DS0_REGISTER_OUT_Z_H_M = 0x0D, 111 | LSM9DS0_REGISTER_WHO_AM_I_XM = 0x0F, 112 | LSM9DS0_REGISTER_INT_CTRL_REG_M = 0x12, 113 | LSM9DS0_REGISTER_INT_SRC_REG_M = 0x13, 114 | LSM9DS0_REGISTER_CTRL_REG1_XM = 0x20, 115 | LSM9DS0_REGISTER_CTRL_REG2_XM = 0x21, 116 | LSM9DS0_REGISTER_CTRL_REG5_XM = 0x24, 117 | LSM9DS0_REGISTER_CTRL_REG6_XM = 0x25, 118 | LSM9DS0_REGISTER_CTRL_REG7_XM = 0x26, 119 | LSM9DS0_REGISTER_OUT_X_L_A = 0x28, 120 | LSM9DS0_REGISTER_OUT_X_H_A = 0x29, 121 | LSM9DS0_REGISTER_OUT_Y_L_A = 0x2A, 122 | LSM9DS0_REGISTER_OUT_Y_H_A = 0x2B, 123 | LSM9DS0_REGISTER_OUT_Z_L_A = 0x2C, 124 | LSM9DS0_REGISTER_OUT_Z_H_A = 0x2D, 125 | } lsm9ds0MagAccelRegisters_t; 126 | 127 | /**! Enumeration for accelerometer range (2/4/6/8/16 g) */ 128 | typedef enum { 129 | LSM9DS0_ACCELRANGE_2G = (0b000 << 3), 130 | LSM9DS0_ACCELRANGE_4G = (0b001 << 3), 131 | LSM9DS0_ACCELRANGE_6G = (0b010 << 3), 132 | LSM9DS0_ACCELRANGE_8G = (0b011 << 3), 133 | LSM9DS0_ACCELRANGE_16G = (0b100 << 3) 134 | } lsm9ds0AccelRange_t; 135 | 136 | /**! Enumeration for accelerometer data rage 3.125 - 1600 Hz */ 137 | typedef enum { 138 | LSM9DS0_ACCELDATARATE_POWERDOWN = (0b0000 << 4), 139 | LSM9DS0_ACCELDATARATE_3_125HZ = (0b0001 << 4), 140 | LSM9DS0_ACCELDATARATE_6_25HZ = (0b0010 << 4), 141 | LSM9DS0_ACCELDATARATE_12_5HZ = (0b0011 << 4), 142 | LSM9DS0_ACCELDATARATE_25HZ = (0b0100 << 4), 143 | LSM9DS0_ACCELDATARATE_50HZ = (0b0101 << 4), 144 | LSM9DS0_ACCELDATARATE_100HZ = (0b0110 << 4), 145 | LSM9DS0_ACCELDATARATE_200HZ = (0b0111 << 4), 146 | LSM9DS0_ACCELDATARATE_400HZ = (0b1000 << 4), 147 | LSM9DS0_ACCELDATARATE_800HZ = (0b1001 << 4), 148 | LSM9DS0_ACCELDATARATE_1600HZ = (0b1010 << 4) 149 | } lm9ds0AccelDataRate_t; 150 | 151 | /**! Enumeration for magnetometer scaling (2/4/8/12 gauss) */ 152 | typedef enum { 153 | LSM9DS0_MAGGAIN_2GAUSS = (0b00 << 5), // +/- 2 gauss 154 | LSM9DS0_MAGGAIN_4GAUSS = (0b01 << 5), // +/- 4 gauss 155 | LSM9DS0_MAGGAIN_8GAUSS = (0b10 << 5), // +/- 8 gauss 156 | LSM9DS0_MAGGAIN_12GAUSS = (0b11 << 5) // +/- 12 gauss 157 | } lsm9ds0MagGain_t; 158 | 159 | /**! Enumeration for magnetometer data rate: 3.125 Hz - 100 Hz */ 160 | typedef enum { 161 | LSM9DS0_MAGDATARATE_3_125HZ = (0b000 << 2), 162 | LSM9DS0_MAGDATARATE_6_25HZ = (0b001 << 2), 163 | LSM9DS0_MAGDATARATE_12_5HZ = (0b010 << 2), 164 | LSM9DS0_MAGDATARATE_25HZ = (0b011 << 2), 165 | LSM9DS0_MAGDATARATE_50HZ = (0b100 << 2), 166 | LSM9DS0_MAGDATARATE_100HZ = (0b101 << 2) 167 | } lsm9ds0MagDataRate_t; 168 | 169 | /**! Enumeration for gyroscope scaling (245/500/2000 dps) */ 170 | typedef enum { 171 | LSM9DS0_GYROSCALE_245DPS = 172 | (0b00 << 4), // +/- 245 degrees per second rotation 173 | LSM9DS0_GYROSCALE_500DPS = 174 | (0b01 << 4), // +/- 500 degrees per second rotation 175 | LSM9DS0_GYROSCALE_2000DPS = 176 | (0b10 << 4) // +/- 2000 degrees per second rotation 177 | } lsm9ds0GyroScale_t; 178 | 179 | /**! 3D floating point vector with X Y Z components */ 180 | typedef struct vector_s { 181 | float x; ///< X component 182 | float y; ///< Y component 183 | float z; ///< Z component 184 | } lsm9ds0Vector_t; 185 | 186 | lsm9ds0Vector_t 187 | accelData; ///< Last read accelerometer data will be available here 188 | lsm9ds0Vector_t 189 | magData; ///< Last read magnetometer data will be available here 190 | lsm9ds0Vector_t gyroData; ///< Last read gyroscope data will be available here 191 | int16_t temperature; ///< Last read temperzture data will be available here 192 | 193 | bool begin(void); 194 | void read(void); 195 | void readAccel(void); 196 | void readMag(void); 197 | void readGyro(void); 198 | void readTemp(void); 199 | void setupAccel(lsm9ds0AccelRange_t range); 200 | void setupMag(lsm9ds0MagGain_t gain); 201 | void setupGyro(lsm9ds0GyroScale_t scale); 202 | 203 | /* Adafruit Unified Sensor Functions (not standard yet ... the current base 204 | * class only */ 205 | /* supports one sensor type, and we need to update the unified base class to 206 | * support */ 207 | /* multiple sensors in a single driver, returning an array */ 208 | bool getEvent(sensors_event_t *accel, sensors_event_t *mag, 209 | sensors_event_t *gyro, sensors_event_t *temp); 210 | void getSensor(sensor_t *accel, sensor_t *mag, sensor_t *gyro, 211 | sensor_t *temp); 212 | 213 | /**! Subclass to expose each sensor on the LSM9DS1 as an Adafruit_Sensor 214 | * instance. */ 215 | class Sensor : public Adafruit_Sensor { 216 | public: 217 | /*! @brief Basic instatiator */ 218 | Sensor() {} 219 | /*! @brief Instantiate based on existing sensor 220 | @param copy The Sensor object to 'copy' from */ 221 | Sensor(const Sensor ©) 222 | : _parent(copy._parent), _readFunc(copy._readFunc), 223 | _eventFunc(copy._eventFunc), _sensorFunc(copy._sensorFunc) {} 224 | 225 | /*! @brief Instantiate based on existing sensor 226 | @param parent The LSM9DS1 parent object 227 | @param readFunc The no-argument function to call to read data 228 | @param eventFunc The sensor_event_t function to call to get event data 229 | @param sensorFunc The sensor_t function to call to get sensor metadata 230 | */ 231 | Sensor(Adafruit_LSM9DS0 *parent, lsm9ds0_read_func readFunc, 232 | lsm9ds0_get_event_func eventFunc, lsm9ds0_get_sensor_func sensorFunc) 233 | : _parent(parent), _readFunc(readFunc), _eventFunc(eventFunc), 234 | _sensorFunc(sensorFunc) {} 235 | /*! @brief Get sensor event data 236 | @param event Pointer to sensor_event_t to fill in 237 | @returns True on successful read */ 238 | virtual bool getEvent(sensors_event_t *event) { 239 | /* Take new reading. */ 240 | (_parent->*_readFunc)(); 241 | /* Fill in event data. */ 242 | (_parent->*_eventFunc)(event, millis()); 243 | return true; 244 | } 245 | 246 | /*! @brief Get sensor metadata - type and range information 247 | @param sensor Pointer to sensor_t to fill in */ 248 | virtual void getSensor(sensor_t *sensor) { 249 | /* Fill in sensor metadata. */ 250 | (_parent->*_sensorFunc)(sensor); 251 | } 252 | 253 | private: 254 | Adafruit_LSM9DS0 *_parent; 255 | lsm9ds0_read_func _readFunc; 256 | lsm9ds0_get_event_func _eventFunc; 257 | lsm9ds0_get_sensor_func _sensorFunc; 258 | }; 259 | 260 | /*! @brief Return Adafruit_Sensor compatible interface for accelerometer 261 | @returns Reference to Adafruit_Sensor subclassed object */ 262 | Sensor &getAccel(void) { return _accelSensor; } 263 | /*! @brief Return Adafruit_Sensor compatible interface for gyroscope 264 | @returns Reference to Adafruit_Sensor subclassed object */ 265 | Sensor &getGyro(void) { return _gyroSensor; } 266 | /*! @brief Return Adafruit_Sensor compatible interface for temperature 267 | @returns Reference to Adafruit_Sensor subclassed object */ 268 | Sensor &getTemp(void) { return _tempSensor; } 269 | /*! @brief Return Adafruit_Sensor compatible interface for magnetometer 270 | @returns Reference to Adafruit_Sensor subclassed object */ 271 | Sensor &getMag(void) { return _magSensor; } 272 | 273 | private: 274 | void write8(boolean type, byte reg, byte value); 275 | byte read8(boolean type, byte reg); 276 | byte readBuffer(boolean type, byte reg, byte len, uint8_t *buffer); 277 | uint8_t spixfer(uint8_t data); 278 | void initI2C(TwoWire *wireBus, int32_t sensorID); 279 | 280 | boolean _i2c; 281 | TwoWire *_wire; 282 | int8_t _csg, _csxm, _mosi, _miso, _clk; 283 | uint8_t mySPCR, SPCRback; 284 | float _accel_mg_lsb; 285 | float _mag_mgauss_lsb; 286 | float _gyro_dps_digit; 287 | int32_t _lsm9dso_sensorid_accel; 288 | int32_t _lsm9dso_sensorid_mag; 289 | int32_t _lsm9dso_sensorid_gyro; 290 | int32_t _lsm9dso_sensorid_temp; 291 | Sensor _accelSensor; 292 | Sensor _magSensor; 293 | Sensor _gyroSensor; 294 | Sensor _tempSensor; 295 | 296 | /* Functions to get individual sensor measurements and metadata. */ 297 | /* Note that these functions will NOT update the sensor state before getting 298 | */ 299 | /* a new reading. You MUST call read() manually to update the sensor state */ 300 | /* before calling these functions! */ 301 | void getAccelEvent(sensors_event_t *event, uint32_t timestamp); 302 | void getMagEvent(sensors_event_t *event, uint32_t timestamp); 303 | void getGyroEvent(sensors_event_t *event, uint32_t timestamp); 304 | void getTempEvent(sensors_event_t *event, uint32_t timestamp); 305 | void getAccelSensor(sensor_t *sensor); 306 | void getMagSensor(sensor_t *sensor); 307 | void getGyroSensor(sensor_t *sensor); 308 | void getTempSensor(sensor_t *sensor); 309 | }; 310 | 311 | #endif 312 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Adafruit LSM9DS0 Library [![Build Status](https://github.com/adafruit/Adafruit_LSM9DS0_Library/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_LSM9DS0_Library/actions)[![Documentation](https://github.com/adafruit/ci-arduino/blob/master/assets/doxygen_badge.svg)](http://adafruit.github.io/Adafruit_LSM9DS0_Library/html/index.html) 2 | 3 | [](https://www.adafruit.com/products/2021) 4 | 5 | 6 | This is the Arduino library for the Adafruit triple-axis accelerometer/magnetometer/gyroscope LSM9DS0 breakouts 7 | 8 | Designed specifically to work with the Adafruit LSM9DS0 Breakout & Flora Sensor 9 | * https://www.adafruit.com/products/2020 10 | * https://www.adafruit.com/products/2021 11 | 12 | **note** both of the above sensors are discontinued. Check out the update, the [LSM9DS1 breakout](https://www.adafruit.com/product/3387) and [and its corresponding library](https://github.com/adafruit/Adafruit_LSM9DS1) 13 | 14 | 15 | Check out the links above for our tutorials and wiring diagrams 16 | 17 | 18 | 19 | ## Compatibility 20 | 21 | MCU | Tested Works | Doesn't Work | Not Tested | Notes 22 | ------------------ | :----------: | :----------: | :---------: | ----- 23 | Atmega328 @ 16MHz | X | | | 24 | Atmega328 @ 12MHz | X | | | 25 | Atmega32u4 @ 16MHz | X | | | 26 | Atmega32u4 @ 8MHz | X | | | 27 | ESP8266 | X | | | 28 | Atmega2560 @ 16MHz | X | | | 29 | ATSAM3X8E | X | | | 30 | ATSAM21D | X | | | 31 | ATtiny85 @ 16MHz | | | X | 32 | ATtiny85 @ 8MHz | | | X | 33 | Intel Curie @ 32MHz | X | | | 34 | STM32F2 | | | X | 35 | 36 | * ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini 37 | * ATmega328 @ 12MHz : Adafruit Pro Trinket 3V 38 | * ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0 39 | * ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro 40 | * ESP8266 : Adafruit Huzzah 41 | * ATmega2560 @ 16MHz : Arduino Mega 42 | * ATSAM3X8E : Arduino Due 43 | * ATSAM21D : Arduino Zero, M0 Pro 44 | * ATtiny85 @ 16MHz : Adafruit Trinket 5V 45 | * ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V 46 | 47 | 48 | 49 | # Installation 50 | To install, use the Arduino Library Manager and search for "Adafruit LSM9DS0 Library" and install the library. 51 | 52 | ## Dependencies 53 | * [Adafruit BusIO](https://github.com/adafruit/Adafruit_BusIO) 54 | * [Adafruit Unified Sensor Driver](https://github.com/adafruit/Adafruit_Sensor) 55 | * [Adafruit GFX Library](https://github.com/adafruit/Adafruit-GFX-Library) 56 | 57 | # Contributing 58 | 59 | Contributions are welcome! Please read our [Code of Conduct](https://github.com/adafruit/Adafruit_LSM9DS0_Library/blob/master/CODE_OF_CONDUCT.md>) 60 | before contributing to help this project stay welcoming. 61 | 62 | ## Documentation and doxygen 63 | Documentation is produced by doxygen. Contributions should include documentation for any new code added. 64 | 65 | Some examples of how to use doxygen can be found in these guide pages: 66 | 67 | https://learn.adafruit.com/the-well-automated-arduino-library/doxygen 68 | 69 | https://learn.adafruit.com/the-well-automated-arduino-library/doxygen-tips 70 | 71 | ## Formatting and clang-format 72 | This library uses [`clang-format`](https://releases.llvm.org/download.html) to standardize the formatting of `.cpp` and `.h` files. 73 | Contributions should be formatted using `clang-format`: 74 | 75 | The `-i` flag will make the changes to the file. 76 | ```bash 77 | clang-format -i *.cpp *.h 78 | ``` 79 | If you prefer to make the changes yourself, running `clang-format` without the `-i` flag will print out a formatted version of the file. You can save this to a file and diff it against the original to see the changes. 80 | 81 | Note that the formatting output by `clang-format` is what the automated formatting checker will expect. Any diffs from this formatting will result in a failed build until they are addressed. Using the `-i` flag is highly recommended. 82 | 83 | ### clang-format resources 84 | * [Binary builds and source available on the LLVM downloads page](https://releases.llvm.org/download.html) 85 | * [Documentation and IDE integration](https://clang.llvm.org/docs/ClangFormat.html) 86 | 87 | ## About this Driver 88 | Written by Kevin (KTOWN) Townsend for Adafruit Industries. 89 | BSD license, check license.txt for more information 90 | All text above must be included in any redistribution 91 | 92 | -------------------------------------------------------------------------------- /code-of-conduct.md: -------------------------------------------------------------------------------- 1 | # Adafruit Community Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and leaders pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level or type of 9 | experience, education, socio-economic status, nationality, personal appearance, 10 | race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | We are committed to providing a friendly, safe and welcoming environment for 15 | all. 16 | 17 | Examples of behavior that contributes to creating a positive environment 18 | include: 19 | 20 | * Be kind and courteous to others 21 | * Using welcoming and inclusive language 22 | * Being respectful of differing viewpoints and experiences 23 | * Collaborating with other community members 24 | * Gracefully accepting constructive criticism 25 | * Focusing on what is best for the community 26 | * Showing empathy towards other community members 27 | 28 | Examples of unacceptable behavior by participants include: 29 | 30 | * The use of sexualized language or imagery and sexual attention or advances 31 | * The use of inappropriate images, including in a community member's avatar 32 | * The use of inappropriate language, including in a community member's nickname 33 | * Any spamming, flaming, baiting or other attention-stealing behavior 34 | * Excessive or unwelcome helping; answering outside the scope of the question 35 | asked 36 | * Trolling, insulting/derogatory comments, and personal or political attacks 37 | * Public or private harassment 38 | * Publishing others' private information, such as a physical or electronic 39 | address, without explicit permission 40 | * Other conduct which could reasonably be considered inappropriate 41 | 42 | The goal of the standards and moderation guidelines outlined here is to build 43 | and maintain a respectful community. We ask that you don’t just aim to be 44 | "technically unimpeachable", but rather try to be your best self. 45 | 46 | We value many things beyond technical expertise, including collaboration and 47 | supporting others within our community. Providing a positive experience for 48 | other community members can have a much more significant impact than simply 49 | providing the correct answer. 50 | 51 | ## Our Responsibilities 52 | 53 | Project leaders are responsible for clarifying the standards of acceptable 54 | behavior and are expected to take appropriate and fair corrective action in 55 | response to any instances of unacceptable behavior. 56 | 57 | Project leaders have the right and responsibility to remove, edit, or 58 | reject messages, comments, commits, code, issues, and other contributions 59 | that are not aligned to this Code of Conduct, or to ban temporarily or 60 | permanently any community member for other behaviors that they deem 61 | inappropriate, threatening, offensive, or harmful. 62 | 63 | ## Moderation 64 | 65 | Instances of behaviors that violate the Adafruit Community Code of Conduct 66 | may be reported by any member of the community. Community members are 67 | encouraged to report these situations, including situations they witness 68 | involving other community members. 69 | 70 | You may report in the following ways: 71 | 72 | In any situation, you may send an email to . 73 | 74 | On the Adafruit Discord, you may send an open message from any channel 75 | to all Community Helpers by tagging @community helpers. You may also send an 76 | open message from any channel, or a direct message to @kattni#1507, 77 | @tannewt#4653, @Dan Halbert#1614, @cater#2442, @sommersoft#0222, or 78 | @Andon#8175. 79 | 80 | Email and direct message reports will be kept confidential. 81 | 82 | In situations on Discord where the issue is particularly egregious, possibly 83 | illegal, requires immediate action, or violates the Discord terms of service, 84 | you should also report the message directly to Discord. 85 | 86 | These are the steps for upholding our community’s standards of conduct. 87 | 88 | 1. Any member of the community may report any situation that violates the 89 | Adafruit Community Code of Conduct. All reports will be reviewed and 90 | investigated. 91 | 2. If the behavior is an egregious violation, the community member who 92 | committed the violation may be banned immediately, without warning. 93 | 3. Otherwise, moderators will first respond to such behavior with a warning. 94 | 4. Moderators follow a soft "three strikes" policy - the community member may 95 | be given another chance, if they are receptive to the warning and change their 96 | behavior. 97 | 5. If the community member is unreceptive or unreasonable when warned by a 98 | moderator, or the warning goes unheeded, they may be banned for a first or 99 | second offense. Repeated offenses will result in the community member being 100 | banned. 101 | 102 | ## Scope 103 | 104 | This Code of Conduct and the enforcement policies listed above apply to all 105 | Adafruit Community venues. This includes but is not limited to any community 106 | spaces (both public and private), the entire Adafruit Discord server, and 107 | Adafruit GitHub repositories. Examples of Adafruit Community spaces include 108 | but are not limited to meet-ups, audio chats on the Adafruit Discord, or 109 | interaction at a conference. 110 | 111 | This Code of Conduct applies both within project spaces and in public spaces 112 | when an individual is representing the project or its community. As a community 113 | member, you are representing our community, and are expected to behave 114 | accordingly. 115 | 116 | ## Attribution 117 | 118 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 119 | version 1.4, available at 120 | , 121 | and the [Rust Code of Conduct](https://www.rust-lang.org/en-US/conduct.html). 122 | 123 | For other projects adopting the Adafruit Community Code of 124 | Conduct, please contact the maintainers of those projects for enforcement. 125 | If you wish to use this code of conduct for your own project, consider 126 | explicitly mentioning your moderation policy or making a copy with your 127 | own moderation policy so as to avoid confusion. 128 | -------------------------------------------------------------------------------- /examples/lsm9doftest/lsm9doftest.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include // not used in this demo but required! 5 | 6 | // i2c 7 | Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(); 8 | 9 | // You can also use software SPI 10 | //Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(13, 12, 11, 10, 9); 11 | // Or hardware SPI! In this case, only CS pins are passed in 12 | //Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(10, 9); 13 | 14 | void setupSensor() 15 | { 16 | // 1.) Set the accelerometer range 17 | lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G); 18 | //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G); 19 | //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G); 20 | //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G); 21 | //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G); 22 | 23 | // 2.) Set the magnetometer sensitivity 24 | lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS); 25 | //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS); 26 | //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS); 27 | //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS); 28 | 29 | // 3.) Setup the gyroscope 30 | lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS); 31 | //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS); 32 | //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_2000DPS); 33 | } 34 | 35 | void setup() 36 | { 37 | #ifndef ESP8266 38 | while (!Serial); // will pause Zero, Leonardo, etc until serial console opens 39 | #endif 40 | Serial.begin(9600); 41 | Serial.println("LSM raw read demo"); 42 | 43 | // Try to initialise and warn if we couldn't detect the chip 44 | if (!lsm.begin()) 45 | { 46 | Serial.println("Oops ... unable to initialize the LSM9DS0. Check your wiring!"); 47 | while (1); 48 | } 49 | Serial.println("Found LSM9DS0 9DOF"); 50 | Serial.println(""); 51 | Serial.println(""); 52 | Serial.println("Setting up LSM9DS0 9DOF"); 53 | setupSensor(); 54 | delay(1); 55 | } 56 | 57 | void loop() 58 | { 59 | lsm.read(); 60 | 61 | Serial.print("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" "); 62 | Serial.print("Y: "); Serial.print((int)lsm.accelData.y); Serial.print(" "); 63 | Serial.print("Z: "); Serial.println((int)lsm.accelData.z); Serial.print(" "); 64 | Serial.print("Mag X: "); Serial.print((int)lsm.magData.x); Serial.print(" "); 65 | Serial.print("Y: "); Serial.print((int)lsm.magData.y); Serial.print(" "); 66 | Serial.print("Z: "); Serial.println((int)lsm.magData.z); Serial.print(" "); 67 | Serial.print("Gyro X: "); Serial.print((int)lsm.gyroData.x); Serial.print(" "); 68 | Serial.print("Y: "); Serial.print((int)lsm.gyroData.y); Serial.print(" "); 69 | Serial.print("Z: "); Serial.println((int)lsm.gyroData.z); Serial.println(" "); 70 | Serial.print("Temp: "); Serial.print((int)lsm.temperature); Serial.println(" "); 71 | delay(200); 72 | } 73 | -------------------------------------------------------------------------------- /examples/sensorapi/sensorapi.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* This driver uses the Adafruit unified sensor library (Adafruit_Sensor), 7 | which provides a common 'type' for sensor data and some helper functions. 8 | 9 | To use this driver you will also need to download the Adafruit_Sensor 10 | library and include it in your libraries folder. 11 | 12 | You should also assign a unique ID to this sensor for use with 13 | the Adafruit Sensor API so that you can identify this particular 14 | sensor in any data logs, etc. To assign a unique ID, simply 15 | provide an appropriate value in the constructor below (12345 16 | is used by default in this example). 17 | 18 | Connections (For default I2C) 19 | =========== 20 | Connect SCL to analog 5 21 | Connect SDA to analog 4 22 | Connect VDD to 5V DC 23 | Connect GROUND to common ground 24 | 25 | History 26 | ======= 27 | 2014/JULY/25 - First version (KTOWN) 28 | */ 29 | 30 | /* Assign a unique base ID for this sensor */ 31 | Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(1000); // Use I2C, ID #1000 32 | 33 | 34 | /* Or, use Hardware SPI: 35 | SCK -> SPI CLK 36 | SDA -> SPI MOSI 37 | G_SDO + XM_SDO -> tied together to SPI MISO 38 | then select any two pins for the two CS lines: 39 | */ 40 | 41 | #define LSM9DS0_XM_CS 10 42 | #define LSM9DS0_GYRO_CS 9 43 | //Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(LSM9DS0_XM_CS, LSM9DS0_GYRO_CS, 1000); 44 | 45 | /* Or, use Software SPI: 46 | G_SDO + XM_SDO -> tied together to the MISO pin! 47 | then select any pins for the SPI lines, and the two CS pins above 48 | */ 49 | 50 | #define LSM9DS0_SCLK 13 51 | #define LSM9DS0_MISO 12 52 | #define LSM9DS0_MOSI 11 53 | 54 | //Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(LSM9DS0_SCLK, LSM9DS0_MISO, LSM9DS0_MOSI, LSM9DS0_XM_CS, LSM9DS0_GYRO_CS, 1000); 55 | 56 | 57 | /**************************************************************************/ 58 | /* 59 | Displays some basic information on this sensor from the unified 60 | sensor API sensor_t type (see Adafruit_Sensor for more information) 61 | */ 62 | /**************************************************************************/ 63 | void displaySensorDetails(void) 64 | { 65 | sensor_t accel, mag, gyro, temp; 66 | 67 | lsm.getSensor(&accel, &mag, &gyro, &temp); 68 | 69 | Serial.println(F("------------------------------------")); 70 | Serial.print (F("Sensor: ")); Serial.println(accel.name); 71 | Serial.print (F("Driver Ver: ")); Serial.println(accel.version); 72 | Serial.print (F("Unique ID: ")); Serial.println(accel.sensor_id); 73 | Serial.print (F("Max Value: ")); Serial.print(accel.max_value); Serial.println(F(" m/s^2")); 74 | Serial.print (F("Min Value: ")); Serial.print(accel.min_value); Serial.println(F(" m/s^2")); 75 | Serial.print (F("Resolution: ")); Serial.print(accel.resolution); Serial.println(F(" m/s^2")); 76 | Serial.println(F("------------------------------------")); 77 | Serial.println(F("")); 78 | 79 | Serial.println(F("------------------------------------")); 80 | Serial.print (F("Sensor: ")); Serial.println(mag.name); 81 | Serial.print (F("Driver Ver: ")); Serial.println(mag.version); 82 | Serial.print (F("Unique ID: ")); Serial.println(mag.sensor_id); 83 | Serial.print (F("Max Value: ")); Serial.print(mag.max_value); Serial.println(F(" uT")); 84 | Serial.print (F("Min Value: ")); Serial.print(mag.min_value); Serial.println(F(" uT")); 85 | Serial.print (F("Resolution: ")); Serial.print(mag.resolution); Serial.println(F(" uT")); 86 | Serial.println(F("------------------------------------")); 87 | Serial.println(F("")); 88 | 89 | Serial.println(F("------------------------------------")); 90 | Serial.print (F("Sensor: ")); Serial.println(gyro.name); 91 | Serial.print (F("Driver Ver: ")); Serial.println(gyro.version); 92 | Serial.print (F("Unique ID: ")); Serial.println(gyro.sensor_id); 93 | Serial.print (F("Max Value: ")); Serial.print(gyro.max_value); Serial.println(F(" rad/s")); 94 | Serial.print (F("Min Value: ")); Serial.print(gyro.min_value); Serial.println(F(" rad/s")); 95 | Serial.print (F("Resolution: ")); Serial.print(gyro.resolution); Serial.println(F(" rad/s")); 96 | Serial.println(F("------------------------------------")); 97 | Serial.println(F("")); 98 | 99 | Serial.println(F("------------------------------------")); 100 | Serial.print (F("Sensor: ")); Serial.println(temp.name); 101 | Serial.print (F("Driver Ver: ")); Serial.println(temp.version); 102 | Serial.print (F("Unique ID: ")); Serial.println(temp.sensor_id); 103 | Serial.print (F("Max Value: ")); Serial.print(temp.max_value); Serial.println(F(" C")); 104 | Serial.print (F("Min Value: ")); Serial.print(temp.min_value); Serial.println(F(" C")); 105 | Serial.print (F("Resolution: ")); Serial.print(temp.resolution); Serial.println(F(" C")); 106 | Serial.println(F("------------------------------------")); 107 | Serial.println(F("")); 108 | 109 | delay(500); 110 | } 111 | 112 | /**************************************************************************/ 113 | /* 114 | Configures the gain and integration time for the TSL2561 115 | */ 116 | /**************************************************************************/ 117 | void configureSensor(void) 118 | { 119 | // 1.) Set the accelerometer range 120 | lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G); 121 | //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G); 122 | //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G); 123 | //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G); 124 | //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G); 125 | 126 | // 2.) Set the magnetometer sensitivity 127 | lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS); 128 | //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS); 129 | //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS); 130 | //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS); 131 | 132 | // 3.) Setup the gyroscope 133 | lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS); 134 | //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS); 135 | //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_2000DPS); 136 | } 137 | 138 | /**************************************************************************/ 139 | /* 140 | Arduino setup function (automatically called at startup) 141 | */ 142 | /**************************************************************************/ 143 | void setup(void) 144 | { 145 | #ifndef ESP8266 146 | while (!Serial); // will pause Zero, Leonardo, etc until serial console opens 147 | #endif 148 | Serial.begin(9600); 149 | Serial.println(F("LSM9DS0 9DOF Sensor Test")); Serial.println(""); 150 | 151 | /* Initialise the sensor */ 152 | if(!lsm.begin()) 153 | { 154 | /* There was a problem detecting the LSM9DS0 ... check your connections */ 155 | Serial.print(F("Ooops, no LSM9DS0 detected ... Check your wiring or I2C ADDR!")); 156 | while(1); 157 | } 158 | Serial.println(F("Found LSM9DS0 9DOF")); 159 | 160 | /* Display some basic information on this sensor */ 161 | displaySensorDetails(); 162 | 163 | /* Setup the sensor gain and integration time */ 164 | configureSensor(); 165 | 166 | /* We're ready to go! */ 167 | Serial.println(""); 168 | } 169 | 170 | /**************************************************************************/ 171 | /* 172 | Arduino loop function, called once 'setup' is complete (your own code 173 | should go here) 174 | */ 175 | /**************************************************************************/ 176 | void loop(void) 177 | { 178 | /* Get a new sensor event */ 179 | sensors_event_t accel, mag, gyro, temp; 180 | 181 | lsm.getEvent(&accel, &mag, &gyro, &temp); 182 | 183 | // print out accelleration data 184 | Serial.print("Accel X: "); Serial.print(accel.acceleration.x); Serial.print(" "); 185 | Serial.print(" \tY: "); Serial.print(accel.acceleration.y); Serial.print(" "); 186 | Serial.print(" \tZ: "); Serial.print(accel.acceleration.z); Serial.println(" \tm/s^2"); 187 | 188 | // print out magnetometer data 189 | Serial.print("Magn. X: "); Serial.print(mag.magnetic.x); Serial.print(" "); 190 | Serial.print(" \tY: "); Serial.print(mag.magnetic.y); Serial.print(" "); 191 | Serial.print(" \tZ: "); Serial.print(mag.magnetic.z); Serial.println(" \tuT"); 192 | 193 | // print out gyroscopic data 194 | Serial.print("Gyro X: "); Serial.print(gyro.gyro.x); Serial.print(" "); 195 | Serial.print(" \tY: "); Serial.print(gyro.gyro.y); Serial.print(" "); 196 | Serial.print(" \tZ: "); Serial.print(gyro.gyro.z); Serial.println(" \trad/s"); 197 | 198 | // print out temperature data 199 | Serial.print("Temp: "); Serial.print(temp.temperature); Serial.println(" *C"); 200 | 201 | Serial.println("**********************\n"); 202 | 203 | delay(250); 204 | } -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit LSM9DS0 Library 2 | version=2.0.4 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Arduino library for LSM9DS0 9-DOF sensor board. 6 | paragraph=Arduino library for LSM9DS0 9-DOF sensor board. 7 | category=Sensors 8 | url=https://github.com/adafruit/Adafruit_LSM9DS0_Library 9 | architectures=* 10 | depends=Adafruit Unified Sensor 11 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Software License Agreement (BSD License) 2 | 3 | Copyright (c) 2020 Kevin (KTOWN) Townsend for Adafruit Industries 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 3. Neither the name of the copyright holders nor the 14 | names of its contributors may be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 18 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | --------------------------------------------------------------------------------