|
| 1 | +// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// BNO055 + BMP280 BFF Demo |
| 5 | + |
| 6 | +#include <Wire.h> |
| 7 | +#include <Adafruit_Sensor.h> |
| 8 | +#include <Adafruit_BMP280.h> |
| 9 | +#include <Adafruit_BNO055.h> |
| 10 | +#include <utility/imumaths.h> |
| 11 | + |
| 12 | +Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28, &Wire); |
| 13 | +Adafruit_BMP280 bmp; |
| 14 | + |
| 15 | +void setup(void) |
| 16 | +{ |
| 17 | + Serial.begin(115200); |
| 18 | + |
| 19 | + while (!Serial) delay(10); // wait for serial port to open! |
| 20 | + |
| 21 | + Serial.println("Adafruit BNO055 + BMP280 BFF Demo"); |
| 22 | + |
| 23 | + /* Initialise the sensor */ |
| 24 | + if (!bno.begin()) |
| 25 | + { |
| 26 | + /* There was a problem detecting the BNO055 ... check your connections */ |
| 27 | + Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); |
| 28 | + while (1); |
| 29 | + } |
| 30 | + if (!bmp.begin()) { |
| 31 | + Serial.print("Ooops, no BMP280 detected ... Check your wiring or I2C ADDR!"); |
| 32 | + while (1); |
| 33 | + } |
| 34 | + bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */ |
| 35 | + Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */ |
| 36 | + Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */ |
| 37 | + Adafruit_BMP280::FILTER_X16, /* Filtering. */ |
| 38 | + Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */ |
| 39 | + Serial.println("Found BNO055 and BMP280 sensors!"); |
| 40 | + Serial.println(); |
| 41 | + delay(1000); |
| 42 | +} |
| 43 | + |
| 44 | +void loop(void) |
| 45 | +{ |
| 46 | + //could add VECTOR_ACCELEROMETER, VECTOR_MAGNETOMETER,VECTOR_GRAVITY... |
| 47 | + sensors_event_t orientationData , angVelocityData , linearAccelData, magnetometerData, accelerometerData, gravityData; |
| 48 | + bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER); |
| 49 | + bno.getEvent(&angVelocityData, Adafruit_BNO055::VECTOR_GYROSCOPE); |
| 50 | + bno.getEvent(&linearAccelData, Adafruit_BNO055::VECTOR_LINEARACCEL); |
| 51 | + bno.getEvent(&magnetometerData, Adafruit_BNO055::VECTOR_MAGNETOMETER); |
| 52 | + bno.getEvent(&accelerometerData, Adafruit_BNO055::VECTOR_ACCELEROMETER); |
| 53 | + bno.getEvent(&gravityData, Adafruit_BNO055::VECTOR_GRAVITY); |
| 54 | + Serial.println("BNO055 data:"); |
| 55 | + printEvent(&orientationData); |
| 56 | + printEvent(&angVelocityData); |
| 57 | + printEvent(&linearAccelData); |
| 58 | + printEvent(&magnetometerData); |
| 59 | + printEvent(&accelerometerData); |
| 60 | + printEvent(&gravityData); |
| 61 | + Serial.println("--"); |
| 62 | + Serial.println("BMP280 data:"); |
| 63 | + Serial.print(F("Temperature = ")); |
| 64 | + Serial.print(bmp.readTemperature()); |
| 65 | + Serial.println(" *C"); |
| 66 | + |
| 67 | + Serial.print(F("Pressure = ")); |
| 68 | + Serial.print(bmp.readPressure()); |
| 69 | + Serial.println(" Pa"); |
| 70 | + |
| 71 | + Serial.print(F("Approx altitude = ")); |
| 72 | + Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */ |
| 73 | + Serial.println(" m"); |
| 74 | + |
| 75 | + Serial.println(); |
| 76 | + delay(2000); |
| 77 | +} |
| 78 | + |
| 79 | +void printEvent(sensors_event_t* event) { |
| 80 | + double x = -1000000, y = -1000000 , z = -1000000; //dumb values, easy to spot problem |
| 81 | + if (event->type == SENSOR_TYPE_ACCELEROMETER) { |
| 82 | + Serial.print("Accl:"); |
| 83 | + x = event->acceleration.x; |
| 84 | + y = event->acceleration.y; |
| 85 | + z = event->acceleration.z; |
| 86 | + } |
| 87 | + else if (event->type == SENSOR_TYPE_ORIENTATION) { |
| 88 | + Serial.print("Orient:"); |
| 89 | + x = event->orientation.x; |
| 90 | + y = event->orientation.y; |
| 91 | + z = event->orientation.z; |
| 92 | + } |
| 93 | + else if (event->type == SENSOR_TYPE_MAGNETIC_FIELD) { |
| 94 | + Serial.print("Mag:"); |
| 95 | + x = event->magnetic.x; |
| 96 | + y = event->magnetic.y; |
| 97 | + z = event->magnetic.z; |
| 98 | + } |
| 99 | + else if (event->type == SENSOR_TYPE_GYROSCOPE) { |
| 100 | + Serial.print("Gyro:"); |
| 101 | + x = event->gyro.x; |
| 102 | + y = event->gyro.y; |
| 103 | + z = event->gyro.z; |
| 104 | + } |
| 105 | + else if (event->type == SENSOR_TYPE_ROTATION_VECTOR) { |
| 106 | + Serial.print("Rot:"); |
| 107 | + x = event->gyro.x; |
| 108 | + y = event->gyro.y; |
| 109 | + z = event->gyro.z; |
| 110 | + } |
| 111 | + else if (event->type == SENSOR_TYPE_LINEAR_ACCELERATION) { |
| 112 | + Serial.print("Linear:"); |
| 113 | + x = event->acceleration.x; |
| 114 | + y = event->acceleration.y; |
| 115 | + z = event->acceleration.z; |
| 116 | + } |
| 117 | + else if (event->type == SENSOR_TYPE_GRAVITY) { |
| 118 | + Serial.print("Gravity:"); |
| 119 | + x = event->acceleration.x; |
| 120 | + y = event->acceleration.y; |
| 121 | + z = event->acceleration.z; |
| 122 | + } |
| 123 | + else { |
| 124 | + Serial.print("Unk:"); |
| 125 | + } |
| 126 | + |
| 127 | + Serial.print("\tx= "); |
| 128 | + Serial.print(x); |
| 129 | + Serial.print(" |\ty= "); |
| 130 | + Serial.print(y); |
| 131 | + Serial.print(" |\tz= "); |
| 132 | + Serial.println(z); |
| 133 | +} |
0 commit comments