Arduino + HMC5883L read all 0

Hi,
I am testing the Arduino + HMC5883L, and Serial Monitor shown all 0, why?
Thanks for help.
Adam

#include "Wire.h"

// I2Cdev and HMC5883L must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "HMC5883L.h"

// class default I2C address is 0x1E
// specific I2C addresses may be passed as a parameter here
// this device only supports one I2C address (0x1E)
HMC5883L mag;

int16_t mx, my, mz;

#define LED_PIN 13
bool blinkState = false;

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(38400);

    // initialize device
    Serial.println("Initializing I2C devices...");
    mag.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");

    // configure Arduino LED for
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // read raw heading measurements from device
    mag.getHeading(&mx, &my, &mz);

    // display tab-separated gyro x/y/z values
    Serial.print("mag:\t");
    Serial.print(mx); Serial.print("\t");
    Serial.print(my); Serial.print("\t");
    Serial.print(mz); Serial.print("\t");
    
// To calculate heading in degrees. 0 degree indicates North
    float heading = atan2(my, mx);
    if(heading < 0)
      heading += 2 * M_PI;
    Serial.print("heading:\t");
    Serial.println(heading * 180/M_PI);

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
}

Arduino_HMC5883L.JPG

hmc5883l.JPG

I2C Scanner.1.JPG

Arduino_HMC5883L.JPG

hmc5883l.JPG

I2C Scanner.1.JPG

Are you sure that the HMC5883L is not a fake?

An I2C scanner. What address (if any) show up when you run this?

// I2C scanner by Nick Gammon.  Thanks Nick.

#include <Wire.h>

void setup() {
  Serial.begin (115200); //*****  make sure serial monitor baud matches *****

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

What Arduino board?

A photo of your wiring is often useful.

groundFungus:
Are you sure that the HMC5883L is not a fake?

An I2C scanner. What address (if any) show up when you run this?

// I2C scanner by Nick Gammon.  Thanks Nick.

#include <Wire.h>

void setup() {
 Serial.begin (115200); //*****  make sure serial monitor baud matches *****

// Leonardo: wait for serial port to connect
 while (!Serial)
   {
   }

Serial.println ();
 Serial.println (“I2C scanner. Scanning …”);
 byte count = 0;
 
 Wire.begin();
 for (byte i = 1; i < 120; i++)
 {
   Wire.beginTransmission (i);
   if (Wire.endTransmission () == 0)
     {
     Serial.print (“Found address: “);
     Serial.print (i, DEC);
     Serial.print (” (0x”);
     Serial.print (i, HEX);
     Serial.println (")");
     count++;
     delay (1);  // maybe unneeded?
     } // end of good response
 } // end of for loop
 Serial.println (“Done.”);
 Serial.print (“Found “);
 Serial.print (count, DEC);
 Serial.println (” device(s).”);
}  // end of setup

void loop() {}




What Arduino board?

A photo of your wiring is often useful.

Thanks.

I attached pictures at my first post, cause of I don’t know how to attache them here.
The wiring is just Vcc/GND/SCL(A5)/SDA(A4).
*And the I2C Scanning got address 0x0D, The chip used is DB 5883, maybe I should use HMC5883 Lib instead of HMC5883L? *

That is not an HMC5883L chip. Try the QMC5883 library.

jremington:
That is not an HMC5883L chip. Try the QMC5883 library.

Thank you ! Great Job!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.