It's easy to use the BNO055 sensor with Python and CircuitPython, and the Adafruit CircuitPython BNO055 library.  This library allows you to easily write Python code that reads the acceleration and orientation of the 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 Microcontroller Wiring - I2C

First wire up a BNO055 to your board exactly as shown on the previous pages for Arduino using the I2C interface.  Here's an example of wiring a Feather M4 to the sensor with I2C:

  • Board 3V to sensor VIN (red wire for STEMMA QT)
  • Board GND to sensor GND (black wire for STEMMA QT)
  • Board SCL to sensor SCL (yellow wire for STEMMA QT)
  • Board SDA to sensor SDA (blue wire for STEMMA QT)

CircuitPython Microcontroller Wiring - UART

Here's an example of wiring a Feather M4 to the sensor with UART:

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board TX to sensor SCL
  • Board RX to sensor SDA
  • sensor PS1 to sensor VIN

Python Computer Wiring - I2C

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 with I2C:

  • Pi 3V3 to sensor VIN (red wire for STEMMA QT)
  • Pi GND to sensor GND (black wire for STEMMA QT)
  • Pi SCL to sensor SCL (yellow wire for STEMMA QT)
  • Pi SDA to sensor SDA (blue wire for STEMMA QT)
Older versions of the Raspberry Pi firmware do not have I2C clock stretching support so they don't work well with the BNO. Please ensure your firmware is updated to the latest version before continuing and slow down the I2C as explained here https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/i2c-clock-stretching

Python Computer Wiring - UART

Here's the Raspberry Pi wired with UART:

  • Pi 3V3 to sensor VIN
  • Pi GND to sensor GND
  • Pi TXD to sensor SCL
  • Pi RXD to sensor SDA
  • sensor PS1 to sensor VIN
You will also need to configure the Pi to enable the UART and disable the login console from using it.

CircuitPython Installation of BNO055 Library

Next you'll need to install the Adafruit CircuitPython BNO055 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.  For example the Circuit Playground Express guide has a great page on how to install the library bundle for express boards.

The lib folder on your CIRCUITPY drive should contain at least the following libraries:

  • adafruit_bno055.mpy
  • adafruit_bus_device
  • adafruit_register

Before continuing make sure your board's lib folder or root filesystem has the adafruit_bno055.mpy, adafruit_bus_device, and adafruit_register files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

The BNO055 CircuitPython library is large, and cannot be imported on a SAMD21 due to lack of RAM space.

Python Installation of BNO055 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-bno055

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!

Older versions of the Raspberry Pi firmware do not have I2C clock stretching support so they don't work well with the BNO. Please ensure your firmware is updated to the latest version before continuing and slow down the I2C as explained here https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/i2c-clock-stretching

To use this sensor, you must enable i2c slowdown on the Raspberry Pi device tree overlay. Check out this guide for instructions!

CircuitPython & Python Usage

To demonstrate the usage of the sensor we'll initialize it and read the acceleration, orientation (in Euler angles), and more from the board's Python REPL.  The difference between I2C and UART is only with the initialization. After that, you can use the sensor the same with either connection.

I2C Initialization

If you are using the I2C connection, create you sensor object as follows:

import board
import busio
import adafruit_bno055
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_bno055.BNO055_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 board
import busio
import adafruit_bno055
uart = busio.UART(board.TX, board.RX)
sensor = adafruit_bno055.BNO055_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 serial
import adafruit_bno055
uart = serial.Serial("/dev/serial0")
sensor = adafruit_bno055.BNO055_UART(uart)

Usage

Now you're ready to read values from the sensor using any of these properties:

  • temperature - The sensor temperature in degrees Celsius.
  • acceleration - This is a 3-tuple of X, Y, Z axis accelerometer values in meters per second squared.
  • magnetic - This is a 3-tuple of X, Y, Z axis magnetometer values in microteslas.
  • gyro - This is a 3-tuple of X, Y, Z axis gyroscope values in degrees per second.
  • euler - This is a 3-tuple of orientation Euler angle values.
  • quaternion - This is a 4-tuple of orientation quaternion values.
  • linear_acceleration - This is a 3-tuple of X, Y, Z linear acceleration values (i.e. without effect of gravity) in meters per second squared.
  • gravity - This is a 3-tuple of X, Y, Z gravity acceleration values (i.e. without the effect of linear acceleration) in meters per second squared.

That's all there is to using the BNO055 sensor with CircuitPython!

Here's a complete example that prints each of the properties every second. Save this as code.py on your board and look for the output in the serial REPL.

Full Example Code

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_bno055


i2c = board.I2C()  # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
sensor = adafruit_bno055.BNO055_I2C(i2c)

# If you are going to use UART uncomment these lines
# uart = board.UART()
# sensor = adafruit_bno055.BNO055_UART(uart)

last_val = 0xFFFF


def temperature():
    global last_val  # pylint: disable=global-statement
    result = sensor.temperature
    if abs(result - last_val) == 128:
        result = sensor.temperature
        if abs(result - last_val) == 128:
            return 0b00111111 & result
    last_val = result
    return result


while True:
    print("Temperature: {} degrees C".format(sensor.temperature))
    """
    print(
        "Temperature: {} degrees C".format(temperature())
    )  # Uncomment if using a Raspberry Pi
    """
    print("Accelerometer (m/s^2): {}".format(sensor.acceleration))
    print("Magnetometer (microteslas): {}".format(sensor.magnetic))
    print("Gyroscope (rad/sec): {}".format(sensor.gyro))
    print("Euler angle: {}".format(sensor.euler))
    print("Quaternion: {}".format(sensor.quaternion))
    print("Linear acceleration (m/s^2): {}".format(sensor.linear_acceleration))
    print("Gravity (m/s^2): {}".format(sensor.gravity))
    print()

    time.sleep(1)

This guide was first published on Apr 22, 2015. It was last updated on Mar 28, 2024.

This page (Python & CircuitPython) was last updated on Mar 28, 2024.

Text editor powered by tinymce.