Skip to content

SoorajR-ai/STM32F411_QMC5883L

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

QMC5883L Sensor Library for STM32

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.

Table of Contents

Introduction

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.

Features

  • 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.

Getting Started

Prerequisites

  • STM32 microcontroller (e.g., STM32F4 series).
  • STM32 HAL library.
  • QMC5883L sensor module.
  • I2C support on your STM32.

Installation

  1. Clone this repository:
    git clone https://github.com/SoorajR-ai/STM32F411_QMC5883L.git
  2. Include the qmc.c and qmc.h files in your STM32 project.
  3. Ensure the I2C peripheral is properly configured in your STM32 project settings.

Usage

  1. Initialize the HAL and configure the system clock in your main.c.
  2. Initialize the I2C peripheral and the QMC5883L sensor:
    uint8_t status = QMC_init(&hi2c1);
  3. 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);
    }

Examples

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...

API Reference

uint8_t QMC_init(I2C_HandleTypeDef *hi2c_device)

Initializes the QMC5883L sensor. Returns 1 if successful, 0 otherwise.

xyz QMC_readXYZ()

Reads and returns the magnetic field data in X, Y, and Z axes.

int16_t QMC_getAzimuth()

Calculates and returns the azimuth in degrees (0 = North).

uint8_t QMC_getBearing()

Calculates and returns the bearing as an index (0-15) representing the compass direction.

Segment Bearing Value Direction
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

Contributing

Contributions are welcome! If you have improvements, bug fixes, or new features, please submit a pull request.

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published