Magnetometer

Ask - Discuss - Share everything about your code in embedded for your projects. Processing, Embedded C, Fritzing...
rupeshkhatpe
Posts: 2
Joined: Wed May 15, 2013 7:33 am

Magnetometer

Postby rupeshkhatpe » Wed May 15, 2013 7:47 am

Dear sir,
Plz give the test Code for the Magnetometer(HMC5883).

thanks
Regard,
Rupesh G Khatpe.
abhimanyu
Posts: 94
Joined: Tue Oct 18, 2011 9:49 pm

Re: Magnetometer

Postby abhimanyu » Wed May 15, 2013 7:51 am

Test code for HMC5883L for Arduino may be found at:
Jeff Rowberg's GitHub

Note that you need the Libraries (HMC5883L.h and HMC5883L.cpp) to run the code (.ino).
rupeshkhatpe
Posts: 2
Joined: Wed May 15, 2013 7:33 am

Re: Magnetometer

Postby rupeshkhatpe » Sat May 18, 2013 2:33 pm

Hi friends i m using magnetometer (HMC5883L), i interface it to PIC 18F4550 uc
i get the data of X,Y,Z axes in the for of hex values..
But i can't convert this in to the Degree format
for e.g
x_value=0x21A;
y_value=0x021;
Z_value=0x363;
consider these values are for the 53°
so plz tell me the calculation part of x,y,z axes value which convert in degrees.
and i want to show this result on 16x2 lcd.
abhimanyu
Posts: 94
Joined: Tue Oct 18, 2011 9:49 pm

Re: Magnetometer

Postby abhimanyu » Mon May 20, 2013 6:54 am

See this link: HMC5883L.cpp from Jeff's GitHub

Line 257:

Code: Select all

// DATA* registers

/** Get 3-axis heading measurements.
 * In the event the ADC reading overflows or underflows for the given channel,
 * or if there is a math overflow during the bias measurement, this data
 * register will contain the value -4096. This register value will clear when
 * after the next valid measurement is made. Note that this method automatically
 * clears the appropriate bit in the MODE register if Single mode is active.
 * @param x 16-bit signed integer container for X-axis heading
 * @param y 16-bit signed integer container for Y-axis heading
 * @param z 16-bit signed integer container for Z-axis heading
 * @see HMC5883L_RA_DATAX_H
 */
void HMC5883L::getHeading(int16_t *x, int16_t *y, int16_t *z) {
    I2Cdev::readBytes(devAddr, HMC5883L_RA_DATAX_H, 6, buffer);
    if (mode == HMC5883L_MODE_SINGLE) I2Cdev::writeByte(devAddr, HMC5883L_RA_MODE, HMC5883L_MODE_SINGLE << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1));
    *x = (((int16_t)buffer[0]) << 8) | buffer[1];
    *y = (((int16_t)buffer[4]) << 8) | buffer[5];
    *z = (((int16_t)buffer[2]) << 8) | buffer[3];
}


After getting the Hex values from x-, y-, z- Data registers, you need to call the getHeading function giving references to your variables.
See this link: HMC5883L.ino

Code: Select all

void loop() {
    // read raw heading measurements from device
    mag.getHeading(&mx, &my, &mz);

    // display tab-separated gyro x/y/z values
    Serial.print("mag:\t");
    Serial.print(mx); Serial.print("\t");
    Serial.print(my); Serial.print("\t");
    Serial.print(mz); Serial.print("\t");
   
// To calculate heading in degrees. 0 degree indicates North
    float heading = atan2(my, mx);
    if(heading < 0)
      heading += 2 * M_PI;
    Serial.print("heading:\t");
    Serial.println(heading * 180/M_PI);

Return to “Code”

Who is online

Users browsing this forum: No registered users and 1 guest