Adafruit BNO055: absolute orientation from a nine-axis sensor
The Bosch BNO055 is an accelerometer, gyroscope and magnetometer with its own processor that fuses the three. Where an MPU6050 hands you raw motion and leaves the math to your sketch, the BNO055 reports finished orientation: heading, roll and pitch in degrees, or a quaternion, plus gravity and linear acceleration separated for you. The Adafruit library reads all of it over I2C at 0x28 (or 0x29 with ADR high).
Built in on Mokxi: #include <Adafruit_BNO055.h> and it builds, with nothing to install. Free, and no account needed.
What Adafruit BNO055 does
The chip calibrates itself as it moves. getCalibration() gives a 0 to 3 level for the system, gyroscope, accelerometer and magnetometer, and the heading is only trustworthy once the magnetometer reaches 3, usually after waving the sensor in a figure eight. Once calibrated, getSensorOffsets() lets you save the offsets and setSensorOffsets() restores them at the next start, so you skip the dance.
Mokxi has no BNO055 part yet, so the library compiles but finds nothing to talk to. As on real hardware with nothing wired, begin() polls address 0x28 for 850 ms and returns false, and the example prints its "no BNO055 detected" line and stops; any reading would be 0. Use it to check a sketch builds before uploading. Upstream sketches also include utility/imumaths.h, which does not resolve in Mokxi; Adafruit_BNO055.h already brings the imu:: types, so delete that line.
| Library Manager name | Adafruit BNO055 |
|---|---|
| Author | Adafruit |
| Include | #include <Adafruit_BNO055.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Builds and runs, and says plainly that the hardware behind it is not simulated. |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <Adafruit_BNO055.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "Adafruit BNO055" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit BNO055 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.6.4, so a sketch written here builds there too.
What to know in the simulator: Mokxi has no BNO055 part yet: begin() polls 0x28 for 850 ms as upstream does and returns false, and readings are 0. Upstream sketches also include utility/imumaths.h, which does not resolve here (firmware/lib/utility is the C++ <utility> header); Adafruit_BNO055.h already brings the imu:: types, so leave that line out.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28, &Wire)
The sensor with an ID of your choosing (55 in the examples), its I2C address and the bus.
bno.begin()
Finds the chip and starts it in NDOF fusion mode. Returns false if nothing answers.
bno.setExtCrystalUse(true)
Uses the breakout’s 32 kHz crystal for steadier timing. Call it after begin() on boards that have one.
bno.getEvent(&event)
Fills a sensors_event_t with orientation in degrees: event.orientation.x is heading, y roll and z pitch.
sensors_event_t event;
bno.getEvent(&event);
Serial.println(event.orientation.x);imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER)
One of the chip’s vectors: VECTOR_EULER, VECTOR_ACCELEROMETER, VECTOR_GYROSCOPE, VECTOR_MAGNETOMETER, VECTOR_LINEARACCEL or VECTOR_GRAVITY.
imu::Quaternion quat = bno.getQuat()
Orientation as a quaternion, which avoids the gimbal lock Euler angles hit near 90 degrees of pitch.
bno.getCalibration(&system, &gyro, &accel, &mag)
Calibration levels from 0 (none) to 3 (full) for the fused system and each sensor.
bno.getSensorOffsets(calibData)
Copies the calibration offsets out once isFullyCalibrated() is true, to save in EEPROM.
Example: Orientation from a BNO055 (sensorapi)
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#define BNO055_SAMPLERATE_DELAY_MS (100)
// Check I2C device address and correct line below (by default address is 0x29 or 0x28)
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28, &Wire);
void displaySensorDetails(void) {
sensor_t sensor;
bno.getSensor(&sensor);
Serial.print("Sensor: "); Serial.println(sensor.name);
Serial.print("Unique ID: "); Serial.println(sensor.sensor_id);
}
void setup(void) {
Serial.begin(115200);
Serial.println("Orientation Sensor Test"); Serial.println("");
if (!bno.begin()) {
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
while (1);
}
delay(1000);
displaySensorDetails();
bno.setExtCrystalUse(true);
}
void loop(void) {
sensors_event_t event;
bno.getEvent(&event);
Serial.print("X: "); Serial.print(event.orientation.x, 4);
Serial.print("\tY: "); Serial.print(event.orientation.y, 4);
Serial.print("\tZ: "); Serial.print(event.orientation.z, 4);
uint8_t system, gyro, accel, mag;
bno.getCalibration(&system, &gyro, &accel, &mag);
Serial.print("\tSys:"); Serial.print(system, DEC);
Serial.print(" G:"); Serial.print(gyro, DEC);
Serial.print(" A:"); Serial.print(accel, DEC);
Serial.print(" M:"); Serial.println(mag, DEC);
imu::Quaternion quat = bno.getQuat();
Serial.print("qW: "); Serial.println(quat.w(), 4);
delay(BNO055_SAMPLERATE_DELAY_MS);
}Common errors and how to fix them
Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!
begin() got no answer. Check SDA and SCL, power, and the address: 0x28 with ADR low, 0x29 with it high. In Mokxi this is always the result, since there is no BNO055 part yet.
utility/imumaths.h: No such file or directory
Mokxi resolves utility as the C++ standard header, so that include fails here. Remove the line; Adafruit_BNO055.h already provides imu::Vector and imu::Quaternion.
Heading drifts or jumps by tens of degrees
The magnetometer is not calibrated, or a motor, speaker or steel nearby bends the field. Wait for getCalibration() to show 3 for the magnetometer and mount the sensor away from magnets.
Questions and answers
BNO055 or MPU6050?
The MPU6050 is cheaper and gives raw motion; you fuse it yourself, for example with Madgwick. The BNO055 does the fusion on chip and adds a magnetometer for a true heading.
Try Adafruit BNO055 without installing anything
Open the example, change the sketch, and keep your version in a free account.