LowLevelExample.ino 3.0 KB

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