Hello,
I'm using an Arduino Uno and a GY-271 sensor board, which uses the HMC5883 magnetometer.
I've tried two libraries with this sensor, one from
Adafruit and one from
Robotpark/Electrodragon. The libraries use Wire.h and communicate with the sensor via I2C.
With either library I'm unable to get the sensor to record actual values. The Adafruit library and example code contains:
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(event.magnetic.y, event.magnetic.x);
// Some adjustments for declination that I'm omitting here
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
delay(500);
}
I've determined that it gets stuck on the line
mag.getEvent(&event);. The console will Serial.print("OK"); before this line but nothing gets printed after.
With the code from RobotPark, I get values printing out but the values never change when I move the sensor.
I've tried this with two sensors so far. I soldered the headers on myself but the solder joints look good to me. I'm not sure of any way to check my soldering other than to make sure the pins are not electrically connected to each other.
Can anyone advise on something I can try to troubleshoot? Maybe I just need to go buy another sensor from another vendor and see if that's the problem...