Hey guys,
I've been working on following along with an Arduino project https://www.instructables.com/id/How-to-Build-a-GPS-Guided-Robot/ and apparently I've made a fairly common mistake in purchasing the QMC5883L Compass instead of the HMC5883L. The HMC was discontinued as of this year and the QMC is licensed to Chinese manufacturers. I was hoping to use the HMC5883L library that the original author of the project im trying to emulate used in his project but the two similar modules used different addresses and register locations. I had the idea of opening up the library and editing the addresses in the header file to make them match up. Is this possible? I ran an i2c checker to find the address of the sensor but how do I find the addresses of the registers? I found another code from someone how was able to find a few of the locations but I just want to be sure. Does a library to make these two compatible with the same functions exist? Because I have not been able to find one. Is there a reliable place to buy HMC5338 magentometers because most of the ones on amazon are the chinese version with the incorrect label.
HMC5883l.h
/*
HMC5883L.h - Header file for the HMC5883L Triple Axis Digital Compass Arduino Library.
Version: 1.1.0
(c) 2014 Korneliusz Jarzebski
www.jarzebski.pl
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HMC5883L_h
#define HMC5883L_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define HMC5883L_ADDRESS (0x1E)
#define HMC5883L_REG_CONFIG_A (0x00)
#define HMC5883L_REG_CONFIG_B (0x01)
#define HMC5883L_REG_MODE (0x02)
#define HMC5883L_REG_OUT_X_M (0x03)
#define HMC5883L_REG_OUT_X_L (0x04)
#define HMC5883L_REG_OUT_Z_M (0x05)
#define HMC5883L_REG_OUT_Z_L (0x06)
#define HMC5883L_REG_OUT_Y_M (0x07)
#define HMC5883L_REG_OUT_Y_L (0x08)
#define HMC5883L_REG_STATUS (0x09)
#define HMC5883L_REG_IDENT_A (0x0A)
#define HMC5883L_REG_IDENT_B (0x0B)
#define HMC5883L_REG_IDENT_C (0x0C)
typedef enum
{
HMC5883L_SAMPLES_8 = 0b11,
HMC5883L_SAMPLES_4 = 0b10,
HMC5883L_SAMPLES_2 = 0b01,
HMC5883L_SAMPLES_1 = 0b00
} hmc5883l_samples_t;
typedef enum
{
HMC5883L_DATARATE_75HZ = 0b110,
HMC5883L_DATARATE_30HZ = 0b101,
HMC5883L_DATARATE_15HZ = 0b100,
HMC5883L_DATARATE_7_5HZ = 0b011,
HMC5883L_DATARATE_3HZ = 0b010,
HMC5883L_DATARATE_1_5HZ = 0b001,
HMC5883L_DATARATE_0_75_HZ = 0b000
} hmc5883l_dataRate_t;
typedef enum
{
HMC5883L_RANGE_8_1GA = 0b111,
HMC5883L_RANGE_5_6GA = 0b110,
HMC5883L_RANGE_4_7GA = 0b101,
HMC5883L_RANGE_4GA = 0b100,
HMC5883L_RANGE_2_5GA = 0b011,
HMC5883L_RANGE_1_9GA = 0b010,
HMC5883L_RANGE_1_3GA = 0b001,
HMC5883L_RANGE_0_88GA = 0b000
} hmc5883l_range_t;
typedef enum
{
HMC5883L_IDLE = 0b10,
HMC5883L_SINGLE = 0b01,
HMC5883L_CONTINOUS = 0b00
} hmc5883l_mode_t;
#ifndef VECTOR_STRUCT_H
#define VECTOR_STRUCT_H
struct Vector
{
float XAxis;
float YAxis;
float ZAxis;
};
#endif
class HMC5883L
{
public:
bool begin(void);
Vector readRaw(void);
Vector readNormalize(void);
void setOffset(int xo, int yo);
void setRange(hmc5883l_range_t range);
hmc5883l_range_t getRange(void);
void setMeasurementMode(hmc5883l_mode_t mode);
hmc5883l_mode_t getMeasurementMode(void);
void setDataRate(hmc5883l_dataRate_t dataRate);
hmc5883l_dataRate_t getDataRate(void);
void setSamples(hmc5883l_samples_t samples);
hmc5883l_samples_t getSamples(void);
private:
float mgPerDigit;
Vector v;
int xOffset, yOffset;
void writeRegister8(uint8_t reg, uint8_t value);
uint8_t readRegister8(uint8_t reg);
uint8_t fastRegister8(uint8_t reg);
int16_t readRegister16(uint8_t reg);
};
#endif
QMC code Mecha_QMC5883L/test.ino at master · mechasolution/Mecha_QMC5883L · GitHub
#include <Wire.h> //I2C Arduino Library
#define addr 0x0D //I2C Address for The HMC5883
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(addr); //start talking
Wire.write(0x0B); // Tell the HMC5883 to Continuously Measure
Wire.write(0x01); // Set the Register
Wire.endTransmission();
Wire.beginTransmission(addr); //start talking
Wire.write(0x09); // Tell the HMC5883 to Continuously Measure
Wire.write(0x1D); // Set the Register
Wire.endTransmission();
}
void loop() {
int x, y, z; //triple axis data
//Tell the HMC what regist to begin writing data into
Wire.beginTransmission(addr);
Wire.write(0x00); //start with register 3.
Wire.endTransmission();
//Read the data.. 2 bytes for each axis.. 6 total bytes
Wire.requestFrom(addr, 6);
if (6 <= Wire.available()) {
x = Wire.read(); //MSB x
x |= Wire.read() << 8; //LSB x
z = Wire.read(); //MSB z
z |= Wire.read() << 8; //LSB z
y = Wire.read(); //MSB y
y |= Wire.read() << 8; //LSB y
}
// Show Values
Serial.print("X Value: ");
Serial.println(x);
Serial.print("Y Value: ");
Serial.println(y);
Serial.print("Z Value: ");
Serial.println(z);
Serial.println();
delay(500);
}