Home Code Raspberry Pi and BMM150 digital geomagnetic sensor python example

Raspberry Pi and BMM150 digital geomagnetic sensor python example

by shedboy71

In this article we look at a digital geomagnetic sensor – this time its a BMM150 digital geomagnetic sensor

Sensor Information

BMM150 is a low power and low noise 3-axis digital geomagnetic sensor to be used in compass applications. The 12-pin wafer level chip scale package (WLCSP) with a footprint of 1.56 x 1.56 mm² and 0.60 mm height provides highest design flexibility to the developer of mobile devices.

Applications like virtual reality or gaming on mobile devices such as mobile phones, tablet PCs or portable media players require 9-axis inertial sensing including magnetic heading information.

Due to the stable performance over a large temperature range, the BMM150 is also especially suited for supporting drones in accurate heading.

BMM150 can be used with an inertial measurement unit (IMU) consisting of a 3-axis accelerometer and a 3-axis gyroscope like Bosch Sensortec’s BMI055.

 

Features

Parameter Technical data
Package CSWLP- (12 pin)
1.56×1.56×0.6 mm³
0.4 mm diagonal ball pitch
Temperature range -40°C … +85°C
Digital interfaces I²C and SPI
(2 interrupt pins)
Resolution 0.3μT
Supply voltage VDD: 1.62V to 3.6V
VDDIO: 1.2V to 3.6V
Zero-B offset ±50μT
Non-linearity <1% FS
Magnetic range typ. ±1300μT (x,y-axis)
±2500μT (z-axis)
Average current consumption 170 μA (low power preset)
500 μA (normal mode)
Interrupts New data, magnetic threshold high / low

Parts Required

You could easily use any Raspberry Pi for this project

Name Link
Raspberry Pi 4 Aliexpress product link

Amazon. com link

Ebay search

BMM150 sensor AliExpress Product link

Amazon link

Ebay link

Connecting wire Aliexpress product link

Amazon.com link

Ebay link

expansion board (optional) Raspberry Pi 3 Expansion Board

Schematic/Connection

Raspberry Pi Sensor
3v3 Vcc
GND Gnd
SDA SDA/SDI/SDO
SCL SCL/SCK

Code Example

This is the instructions from https://github.com/SeeedDocument/wiki_english/blob/master/docs/Grove-3-Axis_Digitial_Compass_v2.0.md to work with the sensor

 

#For raspberry pi
echo "deb https://seeed-studio.github.io/pi_repo/ stretch main" | sudo tee /etc/apt/sources.list.d/seeed.list

Add public GPG key

curl https://seeed-studio.github.io/pi_repo/public.key | sudo apt-key add -

Install MRAA & UPM

sudo apt update
sudo apt install python-mraa python-upm

Install librespeaker & respeakerd (only works on RPI)

sudo apt update
sudo apt install librespeaker respeakerd
cd ~
git clone https://github.com/Seeed-Studio/grove.py

Execute the following commands to run the code.

cd grove.py/grove
python grove_3_axis_compass_bmm150.py 

Following is the grove_3_axis_compass_bmm150.py code.

from __future__ import print_function
import time, sys, signal, atexit, math
try:
    from upm import pyupm_bmm150 as sensorObj
except ImportError:
    print('Error: Please install python-mraa python-upm module.\r\n' 
          'See instruction here https://github.com/Seeed-Studio/pi_repo#mraa--upm-package-repository-for-raspberry-pi ')


def main():
    # Instantiate a BMP250E instance using default i2c bus and address
    sensor = sensorObj.BMM150(0, 0x13)

    # For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS:
    # BMM150(0, -1, 10);

    ## Exit handlers ##
    # This function stops python from printing a stacktrace when you hit control-C
    def SIGINTHandler(signum, frame):
        raise SystemExit

    # This function lets you run code on exit
    def exitHandler():
        print("Exiting")
        sys.exit(0)

    # Register exit handlers
    atexit.register(exitHandler)
    signal.signal(signal.SIGINT, SIGINTHandler)

    # now output data every 250 milliseconds
    while (1):
        sensor.update()

        data = sensor.getMagnetometer()
        print("Magnetometer x: {0:.2f}".format(data[0]), end=' ')
        print(" y: {0:.2f}".format(data[1]), end=' ')
        print(" z: {0:.2f}".format(data[2]), end=' ')
        print(" uT")

        xyHeading = math.atan2(data[0], data[1])
        zxHeading = math.atan2(data[2], data[0])
        heading = xyHeading

        if heading < 0:
            heading += 2*math.pi
        if heading > 2*math.pi:
            heading -= 2*math.pi
        
        headingDegrees = heading * 180/(math.pi); 
        xyHeadingDegrees = xyHeading * 180 / (math.pi)
        zxHeadingDegrees = zxHeading * 180 / (math.pi)

        print('heading(axis_Y point to): {0:.2f} degree'.format(headingDegrees))
        time.sleep(.250)

if __name__ == '__main__':
    main()

If everything goes well, you will be able to see the following result in the terminal

pi@raspberrypi:~/grove.py/grove $ python grove_3_axis_compass_bmm150.py 
Magnetometer x: -34.12  y: 36.71  z: -21.25  uT
heading(axis_Y point to): 317.10 degree
Magnetometer x: -34.49  y: 38.20  z: -16.32  uT
heading(axis_Y point to): 317.92 degree
Magnetometer x: -34.12  y: 38.20  z: -9.87  uT
heading(axis_Y point to): 318.23 degree
Magnetometer x: -32.64  y: 38.94  z: -5.69  uT
heading(axis_Y point to): 320.03 degree
Magnetometer x: -31.52  y: 38.20  z: -2.28  uT
heading(axis_Y point to): 320.47 degree
Magnetometer x: -29.67  y: 38.20  z: 0.38  uT
heading(axis_Y point to): 322.16 degree
Magnetometer x: -26.33  y: 38.20  z: 4.55  uT
heading(axis_Y point to): 325.42 degree
^CExiting

You can quit this program by simply press ctrl+c.

 

Links

https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMM150-DS001.pdf

https://github.com/BoschSensortec/BMM150-Sensor-API

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More