hmc588l programming help needed
psyoptica
Posts: 1
I have a small problem I hope someone having experience with sensors can resolve. I'm using a digital compass sensor hmc5883l. The code to read the compass heading is for a different compass model hmc6352. For some reason I need to use this code for my sensor. So I changed the slave read/write address in the code but it still doesn't work. What else do I need to change. I have very little knowledge about i2c devices.
slave read address for hmc5883l= 0x3D
slave write address for hmc5883l= 0x3C
Code for hmc6352 is posted below
slave read address for hmc5883l= 0x3D
slave write address for hmc5883l= 0x3C
Code for hmc6352 is posted below
#include <Wire.h> int HMC6352Address = 0x42; int slaveAddress; byte headingData[2]; int i, headingValue; int headingcompass; void setup(){ slaveAddress = HMC6352Address >> 1; Wire.begin(); Serial.begin(115200); } void loop(){ Wire.beginTransmission(slaveAddress); //the wire stuff is for the compass module Wire.send("A"); Wire.endTransmission(); delay(10); Wire.requestFrom(slaveAddress, 2); i = 0; while(Wire.available() && i < 2) { headingData[i] = Wire.receive(); i++; } headingValue = headingData[0]*256 + headingData[1]; int pracheading = headingValue / 10; // this is the heading of the compass if(pracheading>0){ headingcompass=pracheading; } Serial.println("current heading:"); Serial.println(headingcompass); }
Comments
The HMC6352 will output a heading, the HMC5883L does not. You can not use the same code for both devices.
The HMC5883L outputs the three axis vector of the magnetic field. You need to use some trig (arctangent) function to convert the vector to a heading.