2

Last week I created some code in which I can run a Ublox GPS NEO-6 and a magnetometer HMC5883L together. Which at the time worked brilliantly. Yesterday I have attempted to run the code again, with the same connections. No modifications to the code.

The GPS NEO-6 works as expected, the magnetometer not so much. After using serial commands as breakpoints, I am able to see that it gets to the line:

mag.getEvent(&event);

Then I bought two new HMC5883L modules and uploaded the example code, to see if the magnetometer will even run correctly. The example code returned the basic stats on the breakout board but once again stopped at that line.

Out of curiosity, I removed the magnetometer and ran the program. It displayed exactly the same. It's not picking up that the HMC5883L is even disconnected.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

void displaySensorDetails(void)
{
  sensor_t sensor;
  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(void) 
{
  Serial.begin(9600);
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");

  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }

  /* Display some basic information on this sensor */
  displaySensorDetails();
}

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);

 // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
 // Find yours here: http://www.magnetic-declination.com/
 // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
 // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
 float declinationAngle = 0.22;
 heading += declinationAngle;

 // Correct for when signs are reversed.
 if(heading < 0)
   heading += 2*PI;

 // Check for wrap due to addition of declination.
 if(heading > 2*PI)
   heading -= 2*PI;

 // Convert radians to degrees for readability.
 float headingDegrees = heading * 180/M_PI; 

 Serial.print("Heading (degrees): "); Serial.println(headingDegrees);

 delay(500);
}

This is the hmc5883l which I use

The wires i used are about 20cm long.

5
  • 1
    Please add more information to your question. Do you use the adafruit hmc5883 modules or other modules? Do those other modules have level shifters for sda and scl? How long are the wires of the i2c bus? Please also add a link to the used library: github.com/adafruit/Adafruit_HMC5883_Unified That library is not very good and contains a bug. It does not test if the hmc5883 is connected and the mag.getEvent function has a while loop that halts everything when the hmc5883 is not connected. That is what you found. I suggest to read the identification registers yourself after the mag.begin
    – Jot
    Apr 12, 2019 at 17:01
  • i use 20 cm long jumper wires Apr 13, 2019 at 6:07
  • and two another libraries i used for clearification are linked as: 1)..link and 2)..arduinolibraries.info/libraries/… Apr 13, 2019 at 6:11
  • Were you able to solve this problem? @Jot would you specify more clearly what the alternative to _HMC5883_Unified is? May 9, 2021 at 21:23
  • Denis G. Labrecque, sorry Dear, it's been 2 years ago and i haven't clearly remember its solution. May 27, 2021 at 12:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.