LowLevelExample.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*****************************************************************************/
  2. // LowLevelExample.ino
  3. // Hardware: Grove - 6-Axis Accelerometer&Gyroscope
  4. // Arduino IDE: Arduino-1.65
  5. // Author: Lambor
  6. // Date: Oct,2015
  7. // Version: v1.0
  8. //
  9. // Modified by:
  10. // Data:
  11. // Description:
  12. //
  13. // by www.seeedstudio.com
  14. //
  15. // This library is free software; you can redistribute it and/or
  16. // modify it under the terms of the GNU Lesser General Public
  17. // License as published by the Free Software Foundation; either
  18. // version 2.1 of the License, or (at your option) any later version.
  19. //
  20. // This library is distributed in the hope that it will be useful,
  21. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. // Lesser General Public License for more details.
  24. //
  25. // You should have received a copy of the GNU Lesser General Public
  26. // License along with this library; if not, write to the Free Software
  27. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. //
  29. /*******************************************************************************/
  30. #include "SparkFunLSM6DS3.h"
  31. #include "Wire.h"
  32. #include "SPI.h"
  33. uint16_t errorsAndWarnings = 0;
  34. //Create instance of LSM6DS3Core
  35. LSM6DS3Core myIMU( I2C_MODE, 0x6A ); //I2C device address 0x6A
  36. void setup() {
  37. //Init Serial port
  38. Serial.begin(9600);
  39. //Call .beginCore() to configure the IMU
  40. if( myIMU.beginCore() != 0 )
  41. {
  42. Serial.print("\nDevice Error.\n");
  43. }
  44. else
  45. {
  46. Serial.print("\nDevice OK.\n");
  47. }
  48. uint8_t dataToWrite = 0; //Temporary variable
  49. //Setup the accelerometer******************************
  50. dataToWrite = 0; //Start Fresh!
  51. dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_100Hz;
  52. dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_8g;
  53. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_104Hz;
  54. //Now, write the patched together data
  55. errorsAndWarnings += myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite);
  56. //Set the ODR bit
  57. errorsAndWarnings += myIMU.readRegister(&dataToWrite, LSM6DS3_ACC_GYRO_CTRL4_C);
  58. dataToWrite &= ~((uint8_t)LSM6DS3_ACC_GYRO_BW_SCAL_ODR_ENABLED);
  59. }
  60. void loop()
  61. {
  62. int16_t temp;
  63. //Get all parameters
  64. Serial.print("\nAccelerometer Counts:\n");
  65. //Acelerometer axis X
  66. if( myIMU.readRegisterInt16(&temp, LSM6DS3_ACC_GYRO_OUTX_L_XL) != 0 )
  67. {
  68. errorsAndWarnings++;
  69. }
  70. Serial.print(" X = ");
  71. Serial.println(temp);
  72. //Acelerometer axis Y
  73. if( myIMU.readRegisterInt16(&temp, LSM6DS3_ACC_GYRO_OUTY_L_XL) != 0 )
  74. {
  75. errorsAndWarnings++;
  76. }
  77. Serial.print(" Y = ");
  78. Serial.println(temp);
  79. //Acelerometer axis Z
  80. if( myIMU.readRegisterInt16(&temp, LSM6DS3_ACC_GYRO_OUTZ_L_XL) != 0 )
  81. {
  82. errorsAndWarnings++;
  83. }
  84. Serial.print(" Z = ");
  85. Serial.println(temp);
  86. Serial.println();
  87. Serial.print("Total reported Errors and Warnings: ");
  88. Serial.println(errorsAndWarnings);
  89. delay(1000);
  90. }