HMC5883 COMPASS NOT WORKING

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ELECT
 
Posts: 8
Joined: Sun Jun 08, 2014 9:55 am

HMC5883 COMPASS NOT WORKING

Post by ELECT »

When I run the sample test sketch "magsensor" for the HMC5883 compass, the serial monitor returns the initial information about the sensor, but nothing after that. It does not return any heading information at all. Am I doing something wrong? Your help is greatly appreciated.

User avatar
DigitalCowboy
 
Posts: 22
Joined: Sat Apr 05, 2014 9:05 pm

Re: HMC5883 COMPASS NOT WORKING

Post by DigitalCowboy »

Post any changes to the sample code and a pic of your hardware to get anything more than guesses

User avatar
ELECT
 
Posts: 8
Joined: Sun Jun 08, 2014 9:55 am

Re: HMC5883 COMPASS NOT WORKING

Post by ELECT »

I made no changes to "magsensor", the sample sketch provided by the Adafruit HMC5883 library. I followed the tutorial to the letter.

You can see everything I did by referring to the Adafruit tutorial for the HMC5883l Compass breakout. There are four wires GND to GND, VIN to 5V, SDA to A4 (Arduino UNO R3), ACL to A5.

The sketch compiles and uploads without any errors, so I know the libraries were properly unzipped and placed in the proper folder. I mentioned in my original post that the Serial Monitor displays the initial sensor particulars, so I know the sensor is also properly connected. It just doesn't generate any other data, like headings of any kind, as the tutorial says it should.

Here's the sketch as downloaded and run:

Code: Select all

/***************************************************************************
  This is a library example for the HMC5883 magnentometer/compass

  Designed specifically to work with the Adafruit HMC5883 Breakout
  http://www.adafruit.com/products/1746
 
  *** You will also need to install the Adafruit_Sensor library! ***

  These displays use I2C to communicate, 2 pins are required to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Kevin Townsend for Adafruit Industries with some heading example from
  Love Electronics (loveelectronics.co.uk)
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the version 3 GNU General Public License as
 published by the Free Software Foundation.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or ------- FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 ***************************************************************************/

#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);
}
Last edited by Franklin97355 on Sat Sep 06, 2014 11:36 pm, edited 1 time in total.
Reason: Added [code] tags to properly format code.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: HMC5883 COMPASS NOT WORKING

Post by adafruit_support_mike »

Post a photo of your hardware and we'll see if that gives us any clues.

A large part of hardware debugging involves finding the places where "what I think it should be doing" and "what it's actually doing" aren't in sync.

User avatar
ELECT
 
Posts: 8
Joined: Sun Jun 08, 2014 9:55 am

Re: HMC5883 COMPASS NOT WORKING

Post by ELECT »

The HMC5883 is working perfectly, now that I set it up on a separate small breadboard..

Originally, I had it set up on another vendor's "prototyping shield". And even though I have successfully used that proto-shield to set up other sensors and devices, it will not work with the HMC5883l. Why not is a mystery to me at this time.

I have pictures of both set ups. However, I can't post them because I am obviously an idiot and I do not know how to get the pictures inserted. Maybe I can e-mail them to you if you want to see them, but I do not have an e-mail address to send to.

Thank you very much for your patience and help Mike.

User avatar
DigitalCowboy
 
Posts: 22
Joined: Sat Apr 05, 2014 9:05 pm

Re: HMC5883 COMPASS NOT WORKING

Post by DigitalCowboy »

ELECT wrote: I have pictures of both set ups. However, I can't post them because I am obviously an idiot and I do not know how to get the pictures inserted..
Here are some screen shots that might help you post your pix if you want.
I suspect since you re-assembled there might have been a loose connection or something. Always a good idea to have a nice reliable breadboard handy...glad you got past the issue.
AdafruitPostFile.png
AdafruitPostFile.png (21.59 KiB) Viewed 2147 times
The attachment AdafruitPlaceFile.png is no longer available
Attachments
AdafruitPlaceFile.png
AdafruitPlaceFile.png (27.73 KiB) Viewed 2147 times

User avatar
KevinJr
 
Posts: 11
Joined: Sun Jan 21, 2018 6:02 pm

Re: HMC5883 COMPASS NOT WORKING

Post by KevinJr »

I'm having a similar issue. My HMC5883 is not printing any data, when using adafruits "adafruit HMC5883 Unified > magsensor" example sketch and uploading to arduino uno without changes to the code the serial monitor prints out this

HMC5883 Magnetometer Test

------------------------------------
Sensor: HMC5883
Driver Ver: 1
Unique ID: 12345
Max Value: 800.00 uT
Min Value: -800.00 uT
Resolution: 0.20 uT
------------------------------------

with no additional code.

I've used other library's examples and ran their codes to which I will get a steady value that will not change with movement to the chip.
I then thought I may have a faulty chip and purchased another type from ebay dealer but have had the same outcome.
I've tried everything I could but cannot figure out what is happening.
Suggestions?
Kevin

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: HMC5883 COMPASS NOT WORKING

Post by Franklin97355 »

please post pictures of your connections and soldering.

User avatar
KevinJr
 
Posts: 11
Joined: Sun Jan 21, 2018 6:02 pm

Re: HMC5883 COMPASS NOT WORKING

Post by KevinJr »

This is one of the chips I was using.
It's the "HCM5883L" magnetometer
Attachments
Optimized-20180121_205956.jpg
Optimized-20180121_205956.jpg (231.01 KiB) Viewed 1475 times

User avatar
KevinJr
 
Posts: 11
Joined: Sun Jan 21, 2018 6:02 pm

Re: HMC5883 COMPASS NOT WORKING

Post by KevinJr »

pic 2
Attachments
Optimized-20180121_211718.jpg
Optimized-20180121_211718.jpg (275.59 KiB) Viewed 1471 times

User avatar
KevinJr
 
Posts: 11
Joined: Sun Jan 21, 2018 6:02 pm

Re: HMC5883 COMPASS NOT WORKING

Post by KevinJr »

pic 3
Attachments
Optimized-20180121_211736.jpg
Optimized-20180121_211736.jpg (299.1 KiB) Viewed 1470 times

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: HMC5883 COMPASS NOT WORKING

Post by Franklin97355 »

the SDA pin looks like it could use a reflow on the solder

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”