- Connect board VIN (red wire) to Arduino 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
- Connect board GND (black wire) to Arduino GND
- Connect board SCL (yellow wire) to Arduino SCL
- Connect board SDA (blue wire) to Arduino SDA
SPI Wiring
Since this is a SPI-capable sensor, we can use hardware or 'software' SPI. To make wiring identical on all microcontrollers, we'll begin with 'software' SPI. The following pins should be used:
- Connect Vin to the power supply, 3V or 5V is fine. Use the same voltage that the microcontroller logic is based off of.
- Connect GND to common power/data ground
- Connect the SCL pin to Digital #13 but any pin can be used later
- Connect the DO pin to Digital #12 but any pin can be used later
- Connect the SDA pin to Digital #11 but any pin can be used later
- Connect the CS pin Digital #10 but any pin can be used later
Later on, once we get it working, we can adjust the library to use hardware SPI if you desire, or change the pins to others.
Library Installation
You can install the Adafruit LIS2MDL Library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit LIS2MDL, and select the Adafruit LIS2MDL library:
Then follow the same process for the Adafruit BusIO library.
Finally follow the same process for the Adafruit Unified Sensor library:
Load Example
Open up File -> Examples -> Adafruit LIS2MDL -> magsensor and upload to your Arduino which has been wired up to the sensor.
Depending on whether you are using I2C or SPI, change the pin names and comment or uncomment the following lines.
if (!lis2mdl.begin()) { // I2C mode //if (! lis2mdl.begin_SPI(LIS2MDL_CS)) { // hardware SPI mode //if (! lis2mdl.begin_SPI(LIS2MDL_CS, LIS2MDL_CLK, LIS2MDL_MISO, LIS2MDL_MOSI)) { // soft SPI
Once you upload the code and open the Serial Monitor (Tools->Serial Monitor) at 115200 baud, you will see the current configuration printed, followed by magnetometer measurements, similar to this:
The sensor class in the magnetometer library reports X, Y and Z axis magnetometer readings directly in micro-Teslas. The magsensor example code reads from the sensor and prints the micro-Tesla readings to the Serial Monitor.
In the absence of any strong local magnetic fields, the sensor readings should reflect the magnetic field of the earth (between 20 and 60 micro-Teslas). When the sensor is held level, by calculating the angle of the magnetic filed with respect to the X and Y axis, the device can be used as a compass.
#include <Adafruit_LIS2MDL.h> #include <Adafruit_Sensor.h> #include <Wire.h> /* Assign a unique ID to this sensor at the same time */ Adafruit_LIS2MDL lis2mdl = Adafruit_LIS2MDL(12345); #define LIS2MDL_CLK 13 #define LIS2MDL_MISO 12 #define LIS2MDL_MOSI 11 #define LIS2MDL_CS 10 void setup(void) { Serial.begin(115200); while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens Serial.println("LIS2MDL Magnetometer Test"); Serial.println(""); /* Enable auto-gain */ lis2mdl.enableAutoRange(true); /* Initialise the sensor */ if (!lis2mdl.begin()) { // I2C mode //if (! lis2mdl.begin_SPI(LIS2MDL_CS)) { // hardware SPI mode //if (! lis2mdl.begin_SPI(LIS2MDL_CS, LIS2MDL_CLK, LIS2MDL_MISO, LIS2MDL_MOSI)) { // soft SPI /* There was a problem detecting the LIS2MDL ... check your connections */ Serial.println("Ooops, no LIS2MDL detected ... Check your wiring!"); while (1) delay(10); } /* Display some basic information on this sensor */ lis2mdl.printSensorDetails(); } void loop(void) { /* Get a new sensor event */ sensors_event_t event; lis2mdl.getEvent(&event); /* Display the results (magnetic vector values are in micro-Tesla (uT)) */ Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" "); Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" "); Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" "); Serial.println("uT"); /* Note: You can also get the raw (non unified values) for */ /* the last data sample as follows. The .getEvent call populates */ /* the raw values used below. */ // Serial.print("X Raw: "); Serial.print(lis2mdl.raw.x); Serial.print(" "); // Serial.print("Y Raw: "); Serial.print(lis2mdl.raw.y); Serial.print(" "); // Serial.print("Z Raw: "); Serial.print(lis2mdl.raw.z); Serial.println(""); /* Delay before the next sample */ delay(100); }
Page last edited January 22, 2025
Text editor powered by tinymce.