It's easy to use the BNO08x with Python or CircuitPython, and the Adafruit CircuitPython BNO08x module. This module allows you to easily write Python code that reads motion data from the BNO08x sensor.
You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
CircuitPython I2C Wiring
First wire up a BNO08x to your board exactly as shown below. Here's an example of wiring a Feather M4 to the sensor with I2C using one of the handy STEMMA QT connectors:
- Board 3V to sensor VIN (red wire)
- Board GND to sensor GND (black wire)
- Board SCL to sensor SCL (yellow wire)
- Board SDA to sensor SDA (blue wire)
You can also use the standard 0.100"/2.54mm pitch headers to wire it up on a breadboard:
- Board 3V to sensor VIN (red wire)
- Board GND to sensor GND (black wire)
- Board SCL to sensor SCL (yellow wire)
- Board SDA to sensor SDA (blue wire)
CircuitPython UART Wiring
If you wish to use UART, you can enable the UART mode by pulling the P1 pin high and connecting the sensor to your RX and TX pins:
- Board 3V to sensor VIN (red wire)
- Board GND to sensor GND (black wire)
- Board TX to sensor SCL (yellow wire)
- Board RX to sensor SDA (blue wire)
- Board 3V to sensor P1 (purple wire)
Python Computer Wiring
Since there's dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired to the sensor using I2C and a STEMMA QT connector:
- Pi 3V to sensor VCC (red wire)
- Pi GND to sensor GND (black wire)
- Pi SCL to sensor SCL (yellow wire)
- Pi SDA to sensor SDA (blue wire)
Finally here is an example of how to wire up a Raspberry Pi to the sensor using a solderless breadboard
- Pi 3V to sensor VCC (red wire)
- Pi GND to sensor GND (black wire)
- Pi SCL to sensor SCL (yellow wire)
- Pi SDA to sensor SDA (blue wire)
dtparam=i2c_arm_baudrate=400000
and then rebooting.
Python UART Wiring
If you wish to use UART, you can enable the UART mode by pulling the P1 pin high and connecting the sensor to your RX and TX pins:
- Pi 3V3 to sensor VIN (red wire)
- Pi GND to sensor GND (black wire)
- Pi TX to sensor SCL (yellow wire)
- Pi RX to sensor SDA (blue wire)
- Pi 3V3 to sensor P1 (purple wire)
CircuitPython Installation of BNO08x Library
You'll need to install the Adafruit CircuitPython BNO08x library on your CircuitPython board.
First make sure you are running the latest version of Adafruit CircuitPython for your board.
Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle. Our CircuitPython starter guide has a great page on how to install the library bundle.
Before continuing make sure your board's lib folder or root filesystem has the adafruit_BNO08x and adafruit_bus_device folders copied over. Both of these are folders with library files in them. Make sure to copy the whole folder for both into your lib folder!
Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.
Python Installation of BNO08x Library
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
Once that's done, from your command line run the following command:
sudo pip3 install adafruit-circuitpython-bno08x
If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
To demonstrate the usage of the sensor we'll initialize it and enable the Rotation Vector report type and display the readings using properties in the CircuitPython REPL
I2C Initialization
If you are using the I2C connection, run the following code to import the necessary modules and initialize the I2C connection with the sensor:
import time import board import busio import adafruit_bno08x from adafruit_bno08x.i2c import BNO08X_I2C i2c = busio.I2C(board.SCL, board.SDA, frequency=800000) bno = BNO08X_I2C(i2c)
UART Initialization - CircuitPython
If you are using the UART connection with a board (like a Feather) running CircuitPython, create your sensor object as follows:
import adafruit_bno08x from adafruit_bno08x.uart import BNO08X_UART import board import busio uart = busio.UART(board.TX, board.RX, baudrate=3000000, receiver_buffer_size=2048) bno = BNO08X_UART(uart)
UART Initialization - Python
Check how you specific board supports UART and where the port entry is created and named. For the Raspberry Pi, this is done using the pyserial module and the UART used is /dev/serial0
. Then you create your sensor object as follows:
import adafruit_bno08x from adafruit_bno08x.uart import BNO08X_UART import serial uart = serial.Serial("/dev/serial0", 115200) bno = BNO08X_UART(uart)
Enable the Rotation Vector/Quaternion Report
Once the connection to the sensor is established we can enable one or more report types and the associated property
bno.enable_feature(adafruit_bno08x.BNO_REPORT_ROTATION_VECTOR)
Now you're ready to read values from the sensor using the quaternion property. This is a 4-tuple of orientation quaternion values.
print("Rotation Vector Quaternion:") quat_i, quat_j, quat_k, quat_real = bno.quaternion print( "I: %0.6f J: %0.6f K: %0.6f Real: %0.6f" % (quat_i, quat_j, quat_k, quat_real) )
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries # # SPDX-License-Identifier: Unlicense import time import board import busio from adafruit_bno08x import ( BNO_REPORT_ACCELEROMETER, BNO_REPORT_GYROSCOPE, BNO_REPORT_MAGNETOMETER, BNO_REPORT_ROTATION_VECTOR, ) from adafruit_bno08x.i2c import BNO08X_I2C i2c = busio.I2C(board.SCL, board.SDA, frequency=400000) bno = BNO08X_I2C(i2c) bno.enable_feature(BNO_REPORT_ACCELEROMETER) bno.enable_feature(BNO_REPORT_GYROSCOPE) bno.enable_feature(BNO_REPORT_MAGNETOMETER) bno.enable_feature(BNO_REPORT_ROTATION_VECTOR) while True: time.sleep(0.5) print("Acceleration:") accel_x, accel_y, accel_z = bno.acceleration # pylint:disable=no-member print("X: %0.6f Y: %0.6f Z: %0.6f m/s^2" % (accel_x, accel_y, accel_z)) print("") print("Gyro:") gyro_x, gyro_y, gyro_z = bno.gyro # pylint:disable=no-member print("X: %0.6f Y: %0.6f Z: %0.6f rads/s" % (gyro_x, gyro_y, gyro_z)) print("") print("Magnetometer:") mag_x, mag_y, mag_z = bno.magnetic # pylint:disable=no-member print("X: %0.6f Y: %0.6f Z: %0.6f uT" % (mag_x, mag_y, mag_z)) print("") print("Rotation Vector Quaternion:") quat_i, quat_j, quat_k, quat_real = bno.quaternion # pylint:disable=no-member print( "I: %0.6f J: %0.6f K: %0.6f Real: %0.6f" % (quat_i, quat_j, quat_k, quat_real) ) print("")
Text editor powered by tinymce.