BMM150_HelloWorld Christian Dupaty 03/2021 Library and demo for BMM150 see datasheet here : https://www.bosch-sensortec.com/products/motion-sensors/magnetometers-bmm150/ Adaptation of BOSCH driver https://github.com/BoschSensortec/BMM150-Sensor-API for ARM MBED and tested on NUCLEO-L073RZ and GEOMAGNETIC CLICK https://www.mikroe.com/geomagnetic-click

Dependencies:   mbed bmm150GeoMagnetic

BMM150_HelloWorld

Christian Dupaty 03/2021

Library and demo for BMM150 see datasheet here : https://www.bosch-sensortec.com/products/motion-sensors/magnetometers-bmm150/

Adaptation of BOSCH driver https://github.com/BoschSensortec/BMM150-Sensor-API for ARM MBED and tested on NUCLEO-L073RZ and GEOMAGNETIC CLICK https://www.mikroe.com/geomagnetic-click

https://os.mbed.com/media/uploads/cdupaty/geomagneticclick.jpg

The BMM150 sensor supports I2C and SPI serial communications. The choice of protocol is made on the PS pin (see datasheet page 32), the default mode is SPI. Unfortunately, MIKROE did not place a configuration micro-switch on the GeoMagnetic-click module, but straps that are difficult to move. By default, the module is configured in SPI mode (all the straps are on the left as in the photo). To place the module in I2C mode, all the straps must be moved to the right.

However, it is possible to implement I2C communications without moving all the straps:

- Move only the strap at the bottom on the photo to the right. - On the left connector we have now :

- CS is connected to CSB-BMM150

- SCK is connected to SCL-I2C-BMM150

- MISO is linked to SDO-BMM150

- MOSI is connected to SDA-I2C-BMM150

BMM150 DataSheet page 36, CSB and SDO are used to define the I2C address. In the program, this address is chosen in BMM150_defs.h line 37 (here address 0x13, therefore CSB = 1 and SDO = 1) define BMM150_I2C_Address (0x13 << 1)

SCL and SDA lines must be connected to 3.3v by pullup resistor (4.4K to 10K) to comply with the I2C standard

Supply with the NUCLEO card in 3.3v and GND

The heading is given periocally by UART over USB in an ASCII terminal (ex TeraTerm)

https://os.mbed.com/media/uploads/cdupaty/bmm150_cap.jpg

Video here ... https://photos.app.goo.gl/zqKQNfw5694xbMZCA

Committer:
cdupaty
Date:
Thu May 20 07:36:32 2021 +0000
Revision:
8:a268ea639af1
Parent:
7:82dd77013b34
OK with library updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cdupaty 3:caca5cdad681 1 /*
cdupaty 3:caca5cdad681 2 Christian Dupaty 03/2021
cdupaty 3:caca5cdad681 3 Library and demo for BMM150 see datasheet here : https://www.bosch-sensortec.com/products/motion-sensors/magnetometers-bmm150/
cdupaty 3:caca5cdad681 4 Adaptation of BOSCH driver https://github.com/BoschSensortec/BMM150-Sensor-API
cdupaty 3:caca5cdad681 5 for ARM MBED and tested on NUCLEO-L073RZ and GEOMAGNETIC CLICK
cdupaty 3:caca5cdad681 6 https://www.mikroe.com/geomagnetic-click
cdupaty 3:caca5cdad681 7 */
cdupaty 3:caca5cdad681 8
cdupaty 3:caca5cdad681 9 #include "mbed.h"
cdupaty 8:a268ea639af1 10
cdupaty 3:caca5cdad681 11 // libraries
cdupaty 3:caca5cdad681 12 #include "bmm150.h"
cdupaty 3:caca5cdad681 13 #include "bmm150_defs.h"
cdupaty 3:caca5cdad681 14 #include <math.h>
cdupaty 3:caca5cdad681 15 #define PI 3.141592653589793238462643383279L
cdupaty 4:577548612451 16 #define DPI 6.283185307179586476925286766559L
cdupaty 3:caca5cdad681 17
cdupaty 7:82dd77013b34 18 // serial object for debug
cdupaty 6:c9c788d429f9 19 Serial pc(SERIAL_TX, SERIAL_RX); // for debug
cdupaty 7:82dd77013b34 20 // BMM150 instance
cdupaty 6:c9c788d429f9 21 BMM150 bmm(PB_9,PB_8); // sda,scl in this order
cdupaty 3:caca5cdad681 22
cdupaty 3:caca5cdad681 23 void setup() {
cdupaty 3:caca5cdad681 24 pc.baud(9600);
cdupaty 3:caca5cdad681 25
cdupaty 3:caca5cdad681 26 if (bmm.initialize() == BMM150_E_ID_NOT_CONFORM)
cdupaty 3:caca5cdad681 27 {
cdupaty 4:577548612451 28 pc.printf("Chip ID can not be read, BMM150 not found!\n");
cdupaty 3:caca5cdad681 29 while (1); // All is lost
cdupaty 3:caca5cdad681 30 } else {
cdupaty 4:577548612451 31 pc.printf("Initialize done, BMM150 has been founded!\n");
cdupaty 3:caca5cdad681 32 }
cdupaty 3:caca5cdad681 33
cdupaty 3:caca5cdad681 34 }
cdupaty 3:caca5cdad681 35
cdupaty 3:caca5cdad681 36 int main()
cdupaty 3:caca5cdad681 37 {
cdupaty 3:caca5cdad681 38 bmm150_mag_data value;
cdupaty 3:caca5cdad681 39 setup(); // Arduino style
cdupaty 3:caca5cdad681 40 while(1)
cdupaty 3:caca5cdad681 41 {
cdupaty 3:caca5cdad681 42 bmm.read_mag_data();
cdupaty 3:caca5cdad681 43
cdupaty 3:caca5cdad681 44 value.x = bmm.raw_mag_data.raw_datax;
cdupaty 3:caca5cdad681 45 value.y = bmm.raw_mag_data.raw_datay;
cdupaty 3:caca5cdad681 46 value.z = bmm.raw_mag_data.raw_dataz;
cdupaty 3:caca5cdad681 47
cdupaty 3:caca5cdad681 48 double xyHeading = atan2((double)value.x, (double)value.y);
cdupaty 3:caca5cdad681 49 double zxHeading = atan2((double)value.z, (double)value.x);
cdupaty 3:caca5cdad681 50 double heading = xyHeading;
cdupaty 3:caca5cdad681 51
cdupaty 3:caca5cdad681 52 if (heading < 0.0)
cdupaty 3:caca5cdad681 53 {
cdupaty 4:577548612451 54 heading += DPI;
cdupaty 3:caca5cdad681 55 }
cdupaty 4:577548612451 56 if (heading > DPI)
cdupaty 3:caca5cdad681 57 {
cdupaty 4:577548612451 58 heading -= DPI;
cdupaty 3:caca5cdad681 59 }
cdupaty 3:caca5cdad681 60 double headingDegrees = heading * 180.0 / PI;
cdupaty 3:caca5cdad681 61 double xyHeadingDegrees = xyHeading * 180.0 / PI;
cdupaty 3:caca5cdad681 62 double zxHeadingDegrees = zxHeading * 180.0 / PI;
cdupaty 3:caca5cdad681 63
cdupaty 4:577548612451 64 pc.printf("Heading: %3.2f deg\n ",headingDegrees);
cdupaty 3:caca5cdad681 65
cdupaty 4:577548612451 66 wait_ms(1000);
cdupaty 3:caca5cdad681 67 }
cdupaty 3:caca5cdad681 68 }