CircuitPython Code

It's easy to use the BNO055 sensor with 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.

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 M0 to the sensor with I2C:

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCL to sensor SCL
  • Board SDA to sensor SDA

Next you'll need to install the Adafruit CircuitPython BNO055 library on your CircuitPython board.  Remember this module is for Adafruit CircuitPython firmware and not MicroPython.org firmware!

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 both express and non-express boards.

Remember for non-express boards like the Trinket M0, Gemma M0, and Feather/Metro M0 basic you'll need to manually install the necessary libraries from the bundle:

  • adafruit_bno055.mpy
  • adafruit_bus_device
  • adafruit_register

You can also download the adafruit_bno055.mpy from its releases page on Github.

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.

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.  First initialize the I2C bus and create an instance of the sensor by running:

import board
import busio
import adafruit_bno055
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_bno055.BNO055(i2c)

Remember if you're using a board that doesn't support hardware I2C (like the ESP8266) you need to use the bitbangio module instead:

import board
import bitbangio
import adafruit_bno055
i2c = bitbangio.I2C(board.SCL, board.SDA)
sensor = adafruit_bno055.BNO055(i2c)

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

  • temperature - The sensor temperature in degrees Celsius.
  • accelerometer - This is a 3-tuple of X, Y, Z axis accelerometer values in meters per second squared.
  • magnetometer - This is a 3-tuple of X, Y, Z axis magnetometer values in microteslas.
  • gyroscope - 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.
print('Temperature: {} degrees C'.format(sensor.temperature))
print('Accelerometer (m/s^2): {}'.format(sensor.accelerometer))
print('Magnetometer (microteslas): {}'.format(sensor.magnetometer))
print('Gyroscope (deg/sec): {}'.format(sensor.gyroscope))
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))

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

Here's a complete example that prints the acceleration and orientation (as Euler angles) every second.  Save this as main.py on your board and look for the output in the serial REPL.  Remember if using the ESP8266 and bitbangio module you need to change the initialization as mentioned above!

import board
import busio
import time

import adafruit_bno055


# Initialize I2C and the sensor.
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_bno055.BNO055(i2c)

# Main loop runs forever printing acceleration and Euler angles every second.
while True:
    print('Accelerometer (m/s^2): {}'.format(sensor.accelerometer))
    print('Euler angle: {}'.format(sensor.euler))
    time.sleep(1.0)
Last updated on 2018-02-06 at 01.45.26 PM Published on 2015-04-22 at 02.24.11 PM