LowLevelExample.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. Serial.print("\nDevice Error.\n");
  42. } else {
  43. Serial.print("\nDevice OK.\n");
  44. }
  45. uint8_t dataToWrite = 0; //Temporary variable
  46. //Setup the accelerometer******************************
  47. dataToWrite = 0; //Start Fresh!
  48. dataToWrite |= LSM6DS3_ACC_GYRO_BW_XL_100Hz;
  49. dataToWrite |= LSM6DS3_ACC_GYRO_FS_XL_8g;
  50. dataToWrite |= LSM6DS3_ACC_GYRO_ODR_XL_104Hz;
  51. //Now, write the patched together data
  52. errorsAndWarnings += myIMU.writeRegister(LSM6DS3_ACC_GYRO_CTRL1_XL, dataToWrite);
  53. //Set the ODR bit
  54. errorsAndWarnings += myIMU.readRegister(&dataToWrite, LSM6DS3_ACC_GYRO_CTRL4_C);
  55. dataToWrite &= ~((uint8_t)LSM6DS3_ACC_GYRO_BW_SCAL_ODR_ENABLED);
  56. }
  57. void loop() {
  58. int16_t temp;
  59. //Get all parameters
  60. Serial.print("\nAccelerometer Counts:\n");
  61. //Acelerometer axis X
  62. if (myIMU.readRegisterInt16(&temp, LSM6DS3_ACC_GYRO_OUTX_L_XL) != 0) {
  63. errorsAndWarnings++;
  64. }
  65. Serial.print(" X = ");
  66. Serial.println(temp);
  67. //Acelerometer axis Y
  68. if (myIMU.readRegisterInt16(&temp, LSM6DS3_ACC_GYRO_OUTY_L_XL) != 0) {
  69. errorsAndWarnings++;
  70. }
  71. Serial.print(" Y = ");
  72. Serial.println(temp);
  73. //Acelerometer axis Z
  74. if (myIMU.readRegisterInt16(&temp, LSM6DS3_ACC_GYRO_OUTZ_L_XL) != 0) {
  75. errorsAndWarnings++;
  76. }
  77. Serial.print(" Z = ");
  78. Serial.println(temp);
  79. Serial.println();
  80. Serial.print("Total reported Errors and Warnings: ");
  81. Serial.println(errorsAndWarnings);
  82. delay(1000);
  83. }