Go Down

Topic: HMC6352 not accurate (Read 2179 times) previous topic - next topic


Hello everyone,

I have been battling with the programing of the compass HMC6352 to get accurate reading but so far all what I have done has not work...
Well I am using arduino UNO atmega328P. see below the code I used.
while I upload the code, I have readings but the problem comes when I rotate the compass 90degrees. the resulting indication on the serial monitor does not reflect the same 90degrees. I would like to calibrate and I wrote the code for calibration with which I tested the compass once more. but no change.

can anyone lead me or provide me with the calibration code... or anything that will help me move forward...

thank you

#include <Wire.h>
#include <LiquidCrystal.h>

//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);       // initialize the numbers of the interface pins

int compass_address = 0x42;                  // initialization of the address of the compass
int slave_address, i, current_value, heading_value; // declaration of variables
byte heading_data[2];                        // declaration of heading_data as array

void setup()

slave_address = compass_address >> 1;        // this shift the bit right one position results in 0x21 as the address to pass to TWI
Serial.begin(9600);   // determine the speed of data transmission
// set up
 CLKPR = 0;
// lcd.begin(16, 4);                           // set the LCD's number of columns and rows
// lcd.setCursor(0, 0);                        // set the cursor to column 0 and line 1
// lcd.print("Robot heading");                 // Print a message to the LCD

void loop()
 Wire.beginTransmission(slave_address);     // start of data transmission
 Wire.send("A");                            // get current data
 Wire.endTransmission();                    // end of data transmission
 delay(100);                               // delay of 1s before refreshing the data

 Wire.requestFrom(slave_address, 2);        // request the 2 bytes heading of 8 bits each
 i = 0;
 while(Wire.available() && i < 2)
   heading_data = Wire.receive();
 heading_value = (heading_data[0]*256 + heading_data[1]);  // add the first byte (MSB) and second byte (LSB)

 current_value = heading_value/10;
//  lcd.setCursor(0, 1);
 Serial.print(heading_value %10);
// Serial.setCursor(7, 1);
 delay(100);                // only 70 microseconds are necessary


What's up with the poll? I wanted to vote s (for stupid) but that's not a choice.
The art of getting good answers lies in asking good questions.

Go Up