Chat freely about anything...
User avatar
By batstru
#71845 Hi all,

I'm having problems with a magnetometer sensor HMC5883 (I2C).
I can detect the sensor with the i2c scanner, but I can't read data: basically it hangs in the getData() method, at mag.getEvent(&event).

I'm using this library: https://github.com/adafruit/Adafruit_HMC5883_Unified

Do you have any suggestion? What can I check?

Let me show you the main loop:
Code: Select allvoid displaySensorDetails(void) {
  mag.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT"); 
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

void setup() {

<cut>

  Wire.begin(0, 2);
 
<cut>

  displaySensorDetails();

<cut>

}


void getData]() {

  Serial.println("debug 03");
   /* Get a new sensor event */
  Wire.beginTransmission(address);
  error = Wire.endTransmission();
 
  mag.getEvent(&event);
 
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
 
<cut>
 
}


void loop() {
Serial.println("debug 01");
<cut>
    Serial.println("debug 02");
    getData();
<cut>
}


and this is the output:

Code: Select all------------------------------------
Sensor:       HMC5883
Driver Ver:   1
Unique ID:    13
Max Value:    800.00 uT
Min Value:    -800.00 uT
Resolution:   0.20 uT
------------------------------------

connected to mqtt
debug 01
debug 02
debug 03