This is a CircuitPython library for interacting with the CMPS12 sensor, a tilt-compensated digital compass that utilizes a 3-axis magnetometer, 3-axis gyroscope, and 3-axis accelerometer. The library allows obtaining orientation readings (Bearing, pitch, roll), temperature, raw sensor data, and calibration status from the CMPS12 sensor via I2C communication. Supports communication with the upgraded version of the sensor, the CMPS14.
- A CircuitPython-compatible development board, such as the Raspberry Pi Pico W.
- CircuitPython installed on the board. Follow the installation guide at CircuitPython.org.
- Additional libraries:
adafruit_bus_device
-
Set up CircuitPython on the Raspberry Pi Pico W:
- Download the CircuitPython version compatible with the Raspberry Pi Pico W from CircuitPython.org.
- Copy the UF2 file onto the board.
-
Install the CMPS12 library:
- Copy the
cmps_i2c.py
file into thelib/
folder on your CircuitPython board.
- Copy the
-
Install
adafruit_bus_device
if it is not already on your device:- Download the library from the Adafruit bundle and copy
adafruit_bus_device/
intolib/
.
- Download the library from the Adafruit bundle and copy
-
Main code file
- Copy or replace the
code.py
file into the/CIRCUITPY
orroot
folder on your CircuitPython board.
- Copy or replace the
The example file code.py
demonstrates how to use the library to read data from the CMPS12 sensor.
import board
import busio
from cmps_i2c import CMPS12
# Configure the I2C bus
i2c = busio.I2C(board.GP15, board.GP14)
# Initialize the CMPS12 sensor
sensor = CMPS12(i2c)
if not sensor.begin():
print("Error: Failed to initialize the sensor")
else:
print("CMPS12 sensor initialized correctly")
while True:
# Read data
bearing = sensor.get_bearing_360()
pitch = sensor.get_pitch_90()
roll = sensor.get_roll_90()
cal_status = sensor.get_cal_stat()
print(f"Bearing: {bearing}°, Pitch: {pitch}°, Roll: {roll}°, Calibration: {cal_status}")
Description: Initializes the sensor and checks if it responds correctly.
- Return:
True
if the sensor initializes correctly,False
otherwise.
Description: Returns the orientation angle (heading) in degrees, ranging from 0-359.9°.
- Return:
float
with the angle in degrees.
Description: Returns the pitch angle in degrees within the range -90° to 90°.
- Return:
int
with the inclination angle.
Description: Returns the pitch angle in degrees within the range -180° to 180°.
- Return:
int
with the inclination angle.
Description: Returns the roll angle in degrees within the range -90° to 90°.
- Return:
int
with the roll angle.
Description: Returns the raw magnetometer readings for the X, Y, and Z axes.
- Return:
tuple (x, y, z)
with 16-bit signed magnetometer values.
Description: Returns the raw accelerometer readings for the X, Y, and Z axes.
- Return:
tuple (x, y, z)
with 16-bit signed accelerometer values.
Description: Returns the raw gyroscope readings for the X, Y, and Z axes.
- Return:
tuple (x, y, z)
with 16-bit signed gyroscope values.
Description: Returns the temperature in degrees Celsius.
- Return:
int
with the temperature in °C.
Description: Returns the sensor's calibration status.
- Return:
CalStat.CAL_LOW = 0
,CalStat.CAL_MED = 2
, orCalStat.CAL_HIGH = 1
.
Description: Stores the current calibration in the CMPS12 memory.
- Return:
True
if the storage is successful.
Description: Erases the stored calibration from the sensor.
- Return:
True
if the operation is successful.
- Author: Julian Andres Castro Pardo
- Contributors: Based on the manufacturer documentation of CMPS12.
This project is licensed under the MIT License.