使用arduino输出QMC5883L的三轴磁场和YAW角

本文介绍了如何在Arduino Nano上通过IIC接口读取QMC5883L磁力计的三轴原始数据,并利用MechaQMC5883库转换为yaw角。详细步骤包括硬件连接、I2C扫描验证、库的下载与使用,最终成功在串口监视器中输出yaw角。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过IIC输出三轴磁力原始数据

通过MechaQMC5883库输出yaw角

我找了不少关于QMC磁力计的文章,发现不管是商家给的程序还是博客的其他文章,都是基于stm32的,而QMC在arduino上的应用却没多少,于是就一边看着数据文档,一边在arduino上实现QMC。

这里用的是arduino nano,
引脚连接:
nano -> QMC
5V -> VCC
A5 -> SCL
A4 -> SDA
GND -> GND

首先在例程中找到I2C_scanner,打开,连接QMC,上传。

在这里插入图片描述

如果没有这个例程,这里给出:

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    https://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#include <Wire.h>

void setup() {
  Wire.begin();

  Serial.begin(9600);
  while (!Serial); // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}

void loop() {
  int nDevices = 0;

  Serial.println("Scanning...");

  for (byte address = 1; address < 127; ++address) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.print(address, HEX);
      Serial.println("  !");

      ++nDevices;
    } else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  } else {
    Serial.println("done\n");
  }
  delay(5000); // Wait 5 seconds for next scan
}

上传后,打开串口监视器,可以看见QMC的地址

在这里插入图片描述
然后编写程序:

#include <Wire.h>           //使用IIC库
#define address 0x0D        //QMC5883L的设备地址设置

void setup(){
  Serial.begin(9600);  //初始化串口和I2C口
  Wire.begin();
//初始化模块
  Wire.beginTransmission(address);
  Wire.write(0x09); //选择控制寄存器0X09
  Wire.write(0x1D); //设置0x09寄存器为:OSR:512Hz,RNG:+/-8Gauss,ODG:200Hz,MODE:连续工作模式
  Wire.endTransmission();
}

void loop(){

  int x,y,z; //triple axis data

  Wire.beginTransmission(address);//读写开始
  Wire.write(0x00); //选择X,Y,Z所在数据储存寄存器
  Wire.endTransmission();

//读取X、Y、Z三个方向的坐标值
  Wire.requestFrom(address, 6);
  if(6<=Wire.available()){
    x = Wire.read()<<8; //X msb
    x |= Wire.read(); //X lsb
    z = Wire.read()<<8; //Z msb
    z |= Wire.read(); //Z lsb
    y = Wire.read()<<8; //Y msb
    y |= Wire.read(); //Y lsb
  }
//打印坐标到串口
  Serial.print("x: ");
  Serial.print(x);
  Serial.print("  y: ");
  Serial.print(y);
  Serial.print("  z: ");
  Serial.println(z);
  delay(100);
}

这里会输出三轴磁力数据:

在这里插入图片描述
但这些还没有转为角度,使用下面这个链接中的库转为yaw角:

https://github.com/keepworking/Mecha_QMC5883L

下载这个库,并保存至你的arduino库位置中:

…\Arduino\libraries

将文件名修改为MechaQMC5883

编写以下代码:

#include <Wire.h>
#include <MechaQMC5883.h>

MechaQMC5883 qmc;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  qmc.init();
  //qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}

void loop() {
  int x,y,z;
  int yaw;
  qmc.read(&x,&y,&z);
  yaw = qmc.azimuth(&y,&x);

  Serial.print("x: ");
  Serial.print(x);
  Serial.print(" y: ");
  Serial.print(y);
  Serial.print(" z: ");
  Serial.print(z);
  Serial.print(" yaw: ");
  Serial.print(yaw);
  Serial.println();
  delay(100);
}

上传,打开串口监视器:

在这里插入图片描述
已经输出yaw角!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

livercy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值