|
| 1 | +/* |
| 2 | + Using the BNO085 for finding the direction of gravity. |
| 3 | + By: Anant Sharma |
| 4 | +
|
| 5 | + Date: January 23rd, 2023 |
| 6 | + SparkFun code, firmware, and software is released under the MIT License. |
| 7 | + Please see LICENSE.md for further details. |
| 8 | + Feel like supporting our work? Buy a board from SparkFun! |
| 9 | + https://www.sparkfun.com/products/14586 |
| 10 | + This example outputs a vector pointing towards the ground. |
| 11 | +
|
| 12 | + It takes about 1ms at 400kHz I2C to read a record from the sensor, but we are polling the sensor continually |
| 13 | + between updates from the sensor. Use the interrupt pin on the BNO080 breakout to avoid polling. |
| 14 | + |
| 15 | + Hardware Connections: |
| 16 | + Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other |
| 17 | + Plug the sensor onto the shield |
| 18 | + |
| 19 | + Serial.print it out at 9600 baud to serial monitor. |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <Wire.h> |
| 23 | +#include "SparkFun_BNO080_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_BNO080 |
| 24 | +BNO080 myIMU; |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +void setup() { |
| 29 | + Serial.begin(9600); |
| 30 | + Serial.println(); |
| 31 | + Serial.println("BNO080 Read Example"); |
| 32 | + |
| 33 | + Wire.begin(); |
| 34 | + |
| 35 | + //Are you using a ESP? Check this issue for more information: https://github.com/sparkfun/SparkFun_BNO080_Arduino_Library/issues/16 |
| 36 | +// //================================= |
| 37 | +// delay(100); // Wait for BNO to boot |
| 38 | +// // Start i2c and BNO080 |
| 39 | +// Wire.flush(); // Reset I2C |
| 40 | +// IMU.begin(BNO080_DEFAULT_ADDRESS, Wire); |
| 41 | +// Wire.begin(4, 5); |
| 42 | +// Wire.setClockStretchLimit(4000); |
| 43 | +// //================================= |
| 44 | + |
| 45 | + if (myIMU.begin() == false) |
| 46 | + { |
| 47 | + Serial.println("BNO080 not detected at default I2C address. Check your jumpers and the hookup guide. Freezing..."); |
| 48 | + while (1); |
| 49 | + } |
| 50 | + |
| 51 | + Wire.setClock(400000); //Increase I2C data rate to 400kHz |
| 52 | + |
| 53 | + myIMU.enableGravity(10); //Send data update every 50ms |
| 54 | + |
| 55 | + Serial.println(F("Rotation vector enabled")); |
| 56 | + Serial.println(F("Output in form i, j, k, real, accuracy")); |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +void loop() { |
| 61 | + //Look for reports from the IMU |
| 62 | + if (myIMU.dataAvailable() == true) |
| 63 | + { |
| 64 | + float gravityX = myIMU.getGravityX(); |
| 65 | + float gravityY = myIMU.getGravityY(); |
| 66 | + float gravityZ = myIMU.getGravityZ(); |
| 67 | + float gravityAccuracy = myIMU.getGravityAccuracy(); |
| 68 | + |
| 69 | + Serial.print(gravityX, 2); |
| 70 | + Serial.print(F(",")); |
| 71 | + Serial.print(gravityY, 2); |
| 72 | + Serial.print(F(",")); |
| 73 | + Serial.print(gravityZ, 2); |
| 74 | + Serial.print(F(",")); |
| 75 | + Serial.print(gravityAccuracy, 2); |
| 76 | + Serial.print(F(",")); |
| 77 | + |
| 78 | + |
| 79 | + Serial.println(); |
| 80 | + } |
| 81 | +} |
0 commit comments