Cannot get an HMC5883L Digital Compass to Output Anything

Hello. I've got an HMC5883L digital compass module that I am trying to use with the Adafruit HMC5883 Unified library. I've tried it with both an UNO R3 and a MEGA 2560 with the same results. I'm using the example sketch magsensor.ino that automatically gets added to the Arduino IDE when the library is installed. I have it connected as follows:

VCC - 3.3V (from the Arduino)

GND - GND (from the Arduino)

SCL - A5

SDA - A4

Here's the example sketch:

/***************************************************************************
  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 FITNESS 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);
}

Here's what I get out of the serial monitor, with no change either to the direction or axis of incline no matter how I move the module:

Heading (degrees): 237.61
X: -0.09  Y: -0.09  Z: -0.10  uT

There's only the power and I2C connections, and I'm assuming the example code is functional, so I'm not sure what could be wrong. I've tried with 2 separate but identical modules with the same results. Here's a picture of the module for context:

Posting schematics and a link to the datasheet would increase the number of helpers taking on Your post significantly. In the mean sit and wait.

If you used the same connection on Mega as on Uno, it is not a great surprise that the module not works.
Did you test the module with I2c scanner IDE example?

Manufacture of the HMC5883 was discontinued many years ago. It is much more likely that you have the Chinese imitation QMC5883, or a counterfeit.

Where did you buy the module?

Which one from where? Does it have level shifting components so it can be used with 5 volt logic?

The cheap one I just found has a 3.3 volt regulator but no level shifting. That would break the deal.

@b707 asked for a scan, see

@jremington mentions counterfeit or otherwise bogus modules. If the scan turns up anything on the bus, it may identify which no good chip they put on it.

a7

SCL and SDA signals of HMC5883L are of 3.3V logic.

Both SCL and SDA lines coming from UNO R3 are of 5V logic.

OP may try using level shifter (Fig-1) for the SCL/SDA lines if the chip has not bricked in the meantime.


Figure-1:

If her moduke is not equipped with them.

If she does need and procures a level shifter, it must be one that can handle the particular challenge which is the I2C bus.

a7

You ave detcetd a 5V/3.3V regulator and no level shifter on the breakout board, and accordingly I am proposing the following connecton (Fig-1) for OP @jools1976 ; where, she needs to buy a level shifter.


Figure-1:

@jools1976
Have you soldered the header pins to the board?
They must be soldered on

Must be 5V not 3.3V

The Arduino Uno R3 provides a 3.3 V output, which could directly power the 3.3 V MC5883L chip. However, many MC5883L breakout board still include an onboard 5 V-to-3.3 V voltage regulator. Is there an advantage or safety reason for including the regulator instead of relying on the Arduino's 3.3 V supply?

Moreover, the breakout board does not contain level shifter. Does it mean that the IO lines of the chip are 5V tolerant and can be used with both 5V and 3.3V Arduinos?

No I used the SCL (21) and SDA (20) with the MEGA. The I2C scanner from the Wire library doesn't pick up any I2C devices. As per some of the other posts, I wonder if it's 3.3V logic. All the examples I saw online used 5V though...

How can I confirm? I bought it from Amazon. I'm not sure if I'm allowed to link to it here or not...

Have you read post #6 as to the need of a level shifter to prevent your chip from being damaged?

If it is from Amazon, it is extremely unlikely to be a genuine HMC5883.

If the I2C Address Scanner can't pick it up, either your wiring is incorrect or the device is nonfunctional.

Almost all sensors are 3.3V and require logic level shifters to work correctly with 5V Arduinos like the Uno R3 and the Mega. If those shifters and a 5V to 3.3V voltage regulator are not on board the sensor module, then you may have damaged the sensor. The module pictured does appear to have a voltage regulator, but a better photo would help make sure.

Adafruit sensors are supported, reliable and most come with an interface that allows them to be used with either 3.3V or 5V Arduinos.

This example (Fig-1) shows the 3.3V bias supply of the chip and the onboard level shifter for 5V bus signals.


Figure-1:

Yes they're soldered on. Vcc should be 5V? Do you mind me asking where you've found that info? Everything I've seen indicated a 3.3V supply...

I saw that schematic and did not see the MOSFETs on either side of the board!

@jools1976 - post a pictures of both sides of you moduke, and yes, you can certainly link to the Amazon product page, just delete everything in the URL that is at the end after a '?'.

a7

Update. I swapped out my breadboard and the wiring just to be prudent, and now it's detecting using the I2C scanner (address is 0x2C), but still the same result in the magsensor.ino sketch... Seeing as it's detecting in the scanner, wouldn't that mean the SCL/SDA are 5V tolerant?

Here's the breakout board:

See Fig-1 of post #15.

Please, follow standard practice and make your chip working.

Get a level shifter, make connection as per Fig-1 of post #8, and see if the module works. If not. proceed for troublesooting.