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 Mar 25 13:08:23 2021 +0000
Revision:
4:577548612451
Parent:
3:caca5cdad681
Child:
6:c9c788d429f9
New i2c_ functions

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 3:caca5cdad681 10 // libraries
cdupaty 3:caca5cdad681 11 #include "bmm150.h"
cdupaty 3:caca5cdad681 12 #include "bmm150_defs.h"
cdupaty 3:caca5cdad681 13 #include <math.h>
cdupaty 3:caca5cdad681 14 #define PI 3.141592653589793238462643383279L
cdupaty 4:577548612451 15 #define DPI 6.283185307179586476925286766559L
cdupaty 3:caca5cdad681 16
cdupaty 3:caca5cdad681 17
cdupaty 3:caca5cdad681 18 Serial pc(SERIAL_TX, SERIAL_RX);
cdupaty 3:caca5cdad681 19 BMM150 bmm(PB_9,PB_8); // sda,scl
cdupaty 3:caca5cdad681 20
cdupaty 3:caca5cdad681 21 void setup() {
cdupaty 3:caca5cdad681 22 pc.baud(9600);
cdupaty 3:caca5cdad681 23
cdupaty 3:caca5cdad681 24 if (bmm.initialize() == BMM150_E_ID_NOT_CONFORM)
cdupaty 3:caca5cdad681 25 {
cdupaty 4:577548612451 26 pc.printf("Chip ID can not be read, BMM150 not found!\n");
cdupaty 3:caca5cdad681 27 while (1); // All is lost
cdupaty 3:caca5cdad681 28 } else {
cdupaty 4:577548612451 29 pc.printf("Initialize done, BMM150 has been founded!\n");
cdupaty 3:caca5cdad681 30 }
cdupaty 3:caca5cdad681 31
cdupaty 3:caca5cdad681 32 }
cdupaty 3:caca5cdad681 33
cdupaty 3:caca5cdad681 34 int main()
cdupaty 3:caca5cdad681 35 {
cdupaty 3:caca5cdad681 36 bmm150_mag_data value;
cdupaty 3:caca5cdad681 37 setup(); // Arduino style
cdupaty 3:caca5cdad681 38 while(1)
cdupaty 3:caca5cdad681 39 {
cdupaty 3:caca5cdad681 40 bmm.read_mag_data();
cdupaty 3:caca5cdad681 41
cdupaty 3:caca5cdad681 42 value.x = bmm.raw_mag_data.raw_datax;
cdupaty 3:caca5cdad681 43 value.y = bmm.raw_mag_data.raw_datay;
cdupaty 3:caca5cdad681 44 value.z = bmm.raw_mag_data.raw_dataz;
cdupaty 3:caca5cdad681 45
cdupaty 3:caca5cdad681 46 double xyHeading = atan2((double)value.x, (double)value.y);
cdupaty 3:caca5cdad681 47 double zxHeading = atan2((double)value.z, (double)value.x);
cdupaty 3:caca5cdad681 48 double heading = xyHeading;
cdupaty 3:caca5cdad681 49
cdupaty 3:caca5cdad681 50 if (heading < 0.0)
cdupaty 3:caca5cdad681 51 {
cdupaty 4:577548612451 52 heading += DPI;
cdupaty 3:caca5cdad681 53 }
cdupaty 4:577548612451 54 if (heading > DPI)
cdupaty 3:caca5cdad681 55 {
cdupaty 4:577548612451 56 heading -= DPI;
cdupaty 3:caca5cdad681 57 }
cdupaty 3:caca5cdad681 58 double headingDegrees = heading * 180.0 / PI;
cdupaty 3:caca5cdad681 59 double xyHeadingDegrees = xyHeading * 180.0 / PI;
cdupaty 3:caca5cdad681 60 double zxHeadingDegrees = zxHeading * 180.0 / PI;
cdupaty 3:caca5cdad681 61
cdupaty 4:577548612451 62 pc.printf("Heading: %3.2f deg\n ",headingDegrees);
cdupaty 3:caca5cdad681 63
cdupaty 4:577548612451 64 wait_ms(1000);
cdupaty 3:caca5cdad681 65 }
cdupaty 3:caca5cdad681 66 }