|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + @file LSM6DSOX_HelloWorld.ino |
| 4 | + @author Benhalor |
| 5 | + @version V1.0.1 |
| 6 | + @date January 2020 |
| 7 | + @brief Basic example for using the LSM6DSOX library . |
| 8 | + ****************************************************************************** |
| 9 | + @attention |
| 10 | + <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> |
| 11 | + Redistribution and use in source and binary forms, with or without modification, |
| 12 | + are permitted provided that the following conditions are met: |
| 13 | + 1. Redistributions of source code must retain the above copyright notice, |
| 14 | + this list of conditions and the following disclaimer. |
| 15 | + 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + this list of conditions and the following disclaimer in the documentation |
| 17 | + and/or other materials provided with the distribution. |
| 18 | + 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 19 | + may be used to endorse or promote products derived from this software |
| 20 | + without specific prior written permission. |
| 21 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 25 | + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 27 | + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 28 | + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 29 | + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + ****************************************************************************** |
| 32 | +*/ |
| 33 | + |
| 34 | +/* WIRING |
| 35 | + In order to use the Adafruit lsm6dsox sensor with a ST nucleo board, |
| 36 | + plug Nucleo "+3.3V" to AdafruitLSM6DOX "VIN", |
| 37 | + plug Nucleo "GND" to AdafruitLSM6DOX "GND", |
| 38 | + plug Nucleo "SCL"(D15) to AdafruitLSM6DOX "SCL", |
| 39 | + plug Nucleo "SDA"(D14) to AdafruitLSM6DOX "SDA".*/ |
| 40 | + |
| 41 | +#include "LSM6DSOXSensor.h" |
| 42 | + |
| 43 | +// Declare LSM6DSOX sensor. Sensor address can have 2 values LSM6DSOX_I2C_ADD_L (corresponds to 0x6A I2C address) or LSM6DSOX_I2C_ADD_H (corresponds to 0x6B I2C address) |
| 44 | +// On Adafruit lsm6dsox sensor, LSM6DSOX_I2C_ADD_L is the default address |
| 45 | +LSM6DSOXSensor lsm6dsoxSensor = LSM6DSOXSensor(&Wire, LSM6DSOX_I2C_ADD_L); |
| 46 | + |
| 47 | +void setup() { |
| 48 | + Serial.begin(115200); |
| 49 | + Wire.begin(); |
| 50 | + |
| 51 | + // Default clock is 100kHz. LSM6DSOX also supports 400kHz, let's use it |
| 52 | + Wire.setClock(400000); |
| 53 | + |
| 54 | + // Init the sensor |
| 55 | + lsm6dsoxSensor.begin(); |
| 56 | + |
| 57 | + // Enable accelerometer and gyroscope, and check success |
| 58 | + if (lsm6dsoxSensor.Enable_X() == LSM6DSOX_OK && lsm6dsoxSensor.Enable_X() == LSM6DSOX_OK) { |
| 59 | + Serial.println("Success enabling accelero and gyro"); |
| 60 | + } else { |
| 61 | + Serial.println("Error enabling accelero and gyro"); |
| 62 | + } |
| 63 | + |
| 64 | + // Read ID of device and check that it is correct |
| 65 | + uint8_t id; |
| 66 | + lsm6dsoxSensor.ReadID(&id); |
| 67 | + if (id != LSM6DSOX_ID) { |
| 68 | + Serial.println("Wrong ID for LSM6DSOX sensor. Check that device is plugged"); |
| 69 | + } else { |
| 70 | + Serial.println("Receviced correct ID for LSM6DSOX sensor"); |
| 71 | + } |
| 72 | + |
| 73 | + // Set accelerometer scale at +- 2G. Available values are +- 2, 4, 8, 16 G |
| 74 | + lsm6dsoxSensor.Set_X_FS(2); |
| 75 | + |
| 76 | + // Set gyroscope scale at +- 125 degres per second. Available values are +- 125, 250, 500, 1000, 2000 dps |
| 77 | + lsm6dsoxSensor.Set_G_FS(125); |
| 78 | + |
| 79 | + |
| 80 | + // Set Accelerometer sample rate to 208 Hz. Available values are +- 12.0, 26.0, 52.0, 104.0, 208.0, 416.0, 833.0, 1667.0, 3333.0, 6667.0 Hz |
| 81 | + lsm6dsoxSensor.Set_X_ODR(208.0f); |
| 82 | + |
| 83 | + |
| 84 | + // Set Gyroscope sample rate to 208 Hz. Available values are +- 12.0, 26.0, 52.0, 104.0, 208.0, 416.0, 833.0, 1667.0, 3333.0, 6667.0 Hz |
| 85 | + lsm6dsoxSensor.Set_G_ODR(208.0f); |
| 86 | + |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +void loop() { |
| 91 | + |
| 92 | + // Read accelerometer |
| 93 | + uint8_t acceleroStatus; |
| 94 | + lsm6dsoxSensor.Get_X_DRDY_Status(&acceleroStatus); |
| 95 | + if (acceleroStatus == 1) { // Status == 1 means a new data is available |
| 96 | + int32_t acceleration[3]; |
| 97 | + lsm6dsoxSensor.Get_X_Axes(acceleration); |
| 98 | + // Plot data for each axe in mg |
| 99 | + Serial.print("AccelerationX="); Serial.print(acceleration[0]); Serial.print("mg, AccelerationY="); Serial.print(acceleration[1]); Serial.print("mg, AccelerationZ="); Serial.print(acceleration[2]); Serial.println("mg"); |
| 100 | + } |
| 101 | + |
| 102 | + // Read gyroscope |
| 103 | + uint8_t gyroStatus; |
| 104 | + lsm6dsoxSensor.Get_G_DRDY_Status(&gyroStatus); |
| 105 | + if (gyroStatus == 1) { // Status == 1 means a new data is available |
| 106 | + int32_t rotation[3]; |
| 107 | + lsm6dsoxSensor.Get_X_Axes(rotation); |
| 108 | + // Plot data for each axe in milli degres per second |
| 109 | + Serial.print("RotationX="); Serial.print(rotation[0]); Serial.print("mdps, RotationY="); Serial.print(rotation[1]); Serial.print("mdps, RotationZ="); Serial.print(rotation[2]); Serial.println("mdps"); |
| 110 | + } |
| 111 | + |
| 112 | + delay(10); |
| 113 | + |
| 114 | +} |
0 commit comments