This repository contains a library for interfacing with the QMC5883L magnetometer sensor using STM32 microcontrollers. The library provides a straightforward API to read magnetic field data and compute azimuth and bearing from the sensor.
The QMC5883L is a three-axis digital magnetometer that can measure the magnetic field in the X, Y, and Z axes. This library simplifies the process of interfacing with the QMC5883L sensor by providing functions to initialize the sensor and retrieve measurements.
- Easy-to-use API for reading magnetic field data.
- Supports I2C communication with STM32.
- Functions to calculate azimuth and bearing based on the magnetic field data.
- STM32 microcontroller (e.g., STM32F4 series).
- STM32 HAL library.
- QMC5883L sensor module.
- I2C support on your STM32.
- Clone this repository:
git clone https://github.com/SoorajR-ai/STM32F411_QMC5883L.git
- Include the
qmc.c
andqmc.h
files in your STM32 project. - Ensure the I2C peripheral is properly configured in your STM32 project settings.
- Initialize the HAL and configure the system clock in your
main.c
. - Initialize the I2C peripheral and the QMC5883L sensor:
uint8_t status = QMC_init(&hi2c1);
- Read the magnetic field data and compute azimuth and bearing in your main loop:
while (1) { xyz data = QMC_readXYZ(); // Access data.x, data.y, data.z int azimuth = QMC_getAzimuth(); // 0 = North int bearing = QMC_getBearing(); // 0 = North, 15 = South-West HAL_Delay(100); }
Here's a simple example of how to use the library in your main.c
file:
#include "main.h" #include "../QMC5883L/qmc.h"
I2C_HandleTypeDef hi2c1;
void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_I2C1_Init(void);
int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_I2C1_Init();
uint8_t status = QMC_init(&hi2c1); while (1) { xyz data = QMC_readXYZ(); int azimuth = QMC_getAzimuth(); int bearing = QMC_getBearing(); HAL_Delay(100); }
}
// Remaining configuration and initialization code...
Initializes the QMC5883L sensor. Returns 1
if successful, 0
otherwise.
Reads and returns the magnetic field data in X, Y, and Z axes.
Calculates and returns the azimuth in degrees (0 = North).
Calculates and returns the bearing as an index (0-15) representing the compass direction.
Segment | Bearing Value | Direction |
---|---|---|
0 | 0° | North |
1 | 22.5° | North-East |
2 | 45° | East |
3 | 67.5° | South-East |
4 | 90° | South |
5 | 112.5° | South-West |
6 | 135° | West |
7 | 157.5° | North-West |
8 | 180° | South |
9 | 202.5° | South-East |
10 | 225° | East |
11 | 247.5° | North-East |
12 | 270° | North |
13 | 292.5° | North-West |
14 | 315° | West |
15 | 337.5° | South-West |
Contributions are welcome! If you have improvements, bug fixes, or new features, please submit a pull request.
This project is licensed under the MIT License.