DoF9 Unit
This library is the driver for Unit DoF9.
Support the following products:
UiFlow2 Example
Read Heading
Open the cores3_unit_dof9_example.m5f2 project in UiFlow2.
This example shows how to read and display the heading.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
Read Heading
This example shows how to read and display the heading.
MicroPython Code Block:
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from hardware import Pin 9from hardware import I2C 10from unit import DoF9Unit 11import time 12 13 14title = None 15live = None 16heading_caption = None 17label_heading = None 18degree_unit = None 19compensation_caption = None 20sensor_caption = None 21i2c0 = None 22dof9_0 = None 23 24 25last_time = None 26UPDATE_DURATION_MS = None 27 28 29def setup(): 30 global \ 31 title, \ 32 live, \ 33 heading_caption, \ 34 label_heading, \ 35 degree_unit, \ 36 compensation_caption, \ 37 sensor_caption, \ 38 i2c0, \ 39 dof9_0, \ 40 last_time, \ 41 UPDATE_DURATION_MS 42 43 M5.begin() 44 Widgets.setRotation(0) 45 Widgets.fillScreen(0x10171D) 46 title = Widgets.Label( 47 "DoF9 / Compass", 12, 8, 1.0, 0x4CD7D0, 0x10171D, Widgets.FONTS.Montserrat18 48 ) 49 live = Widgets.Label("Live", 272, 11, 1.0, 0x63D08C, 0x10171D, Widgets.FONTS.Montserrat14) 50 heading_caption = Widgets.Label( 51 "Magnetic Heading", 12, 48, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14 52 ) 53 label_heading = Widgets.Label( 54 "---.-", 12, 70, 1.0, 0xF2F6F8, 0x10171D, Widgets.FONTS.Montserrat40 55 ) 56 degree_unit = Widgets.Label( 57 "DEG", 174, 91, 1.0, 0xFFB454, 0x10171D, Widgets.FONTS.Montserrat18 58 ) 59 compensation_caption = Widgets.Label( 60 "TILT COMPENSATED", 12, 153, 1.0, 0x8FA1AD, 0x10171D, Widgets.FONTS.Montserrat14 61 ) 62 sensor_caption = Widgets.Label( 63 "BMI270 + BMM350", 12, 207, 1.0, 0x63A8FF, 0x10171D, Widgets.FONTS.Montserrat14 64 ) 65 66 i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=400000) 67 dof9_0 = DoF9Unit(i2c0, addr=0x68) 68 UPDATE_DURATION_MS = 100 69 last_time = time.ticks_ms() 70 71 72def loop(): 73 global \ 74 title, \ 75 live, \ 76 heading_caption, \ 77 label_heading, \ 78 degree_unit, \ 79 compensation_caption, \ 80 sensor_caption, \ 81 i2c0, \ 82 dof9_0, \ 83 last_time, \ 84 UPDATE_DURATION_MS 85 M5.update() 86 if (time.ticks_diff((time.ticks_ms()), last_time)) >= UPDATE_DURATION_MS: 87 last_time = time.ticks_ms() 88 label_heading.setText(str(round(dof9_0.get_heading(), 1))) 89 90 91if __name__ == "__main__": 92 try: 93 setup() 94 while True: 95 loop() 96 except (Exception, KeyboardInterrupt) as e: 97 try: 98 from utility import print_error_msg 99 100 print_error_msg(e) 101 except ImportError: 102 print("please update to latest firmware")
Example output:
None
API
DoF9Unit
Constructors
- class DoF9Unit(i2c, addr=0x68)
Create a DoF9Unit on the host I2C bus. The BMI270 address can be
0x68or0x69(default:0x68). The BMM350 address remains0x14.- Parameters:
- Raises:
TypeError – addr is not an integer.
ValueError – addr is not 0x68 or 0x69.
UnitError – No BMI270 responds at the selected address.
OSError – Sensor initialization or I2C communication fails.
UiFlow2 Code Block:

MicroPython Code Block:
dof = DoF9Unit(i2c) # Default BMI270 address: 0x68 # Use this instead when the BMI270 is configured at 0x69: # dof = DoF9Unit(i2c, addr=0x69)
Methods
The complete DoF9 interface is listed below.
- get_accel()
Return mapped acceleration as
(x, y, z)inm/s2.UiFlow2 Code Block:

- get_accel_raw()
Return signed 16-bit BMI270 counts from
-32768to32767.UiFlow2 Code Block:

- get_gyro()
Return mapped, offset-corrected angular velocity as
(x, y, z)inrad/s.UiFlow2 Code Block:

- get_gyro_raw()
Return signed 16-bit BMI270 gyroscope counts from
-32768to32767.UiFlow2 Code Block:

- get_mag()
Return mapped and calibrated magnetic field as
(x, y, z)inuT.UiFlow2 Code Block:

- get_mag_raw()
Return signed 24-bit BMM350 counts from
-8388608to8388607.UiFlow2 Code Block:

- get_temperature(sensor='imu')
Return BMI270 or BMM350 temperature in degrees Celsius.
sensoris"imu"or"magnetometer".- Parameters:
sensor (str) – Temperature source.
- Returns:
Temperature in degrees Celsius.
- Return type:
UiFlow2 Code Block:

- get_heading()
Sample the sensors and return tilt-compensated heading from 0 to less than 360 degrees.
- Returns:
Heading in degrees.
- Return type:
UiFlow2 Code Block:

- get_attitude()
Sample the sensors and return
(yaw, pitch, roll)in degrees.UiFlow2 Code Block:

- set_accel_range(accel_scale)
Set one of
ACCEL_RANGE_2G,ACCEL_RANGE_4G,ACCEL_RANGE_8G, orACCEL_RANGE_16G.- Parameters:
accel_scale (int) – Full-scale range in g.
UiFlow2 Code Block:

- set_gyro_range(gyro_scale)
Set one of
GYRO_RANGE_125DPS,GYRO_RANGE_250DPS,GYRO_RANGE_500DPS,GYRO_RANGE_1000DPS, orGYRO_RANGE_2000DPS.- Parameters:
gyro_scale (int) – Full-scale range in deg/s.
UiFlow2 Code Block:

- set_accel_gyro_odr(accel_odr, gyro_odr)
Set BMI270 ODR using
ACCEL_ODR_VALUESandGYRO_ODR_VALUES.UiFlow2 Code Block:

- set_gyro_offsets(x, y, z)
Set mapped gyroscope offsets in
rad/s.- Parameters:
UiFlow2 Code Block:

- calibrate_gyro(samples=32)
Estimate offsets while stationary and return
Trueon success.- Parameters:
samples (int) – Number of samples.
- Returns:
Whether calibration succeeded.
- Return type:
UiFlow2 Code Block:

- set_magnetometer_calibration(offsets, scales)
Set hard-iron offsets in
uTand unitless soft-iron scale factors.- Parameters:
UiFlow2 Code Block:

- set_axis_mapping(sensor, x_axis, y_axis, z_axis)
Map
accelerometer,gyroscope, ormagnetometeraxes. Values1/-1,2/-2, and3/-3select source X/Y/Z with sign; source axes cannot repeat.- Parameters:
UiFlow2 Code Block:

- set_fusion_time_constant(seconds)
Set the non-negative complementary-filter time constant in seconds.
- Parameters:
seconds (float) – Non-negative filter time constant.
UiFlow2 Code Block:

- reset_fusion()
Reset the attitude filter; the next attitude sample reinitializes it.
UiFlow2 Code Block:


