HMC5883L can't read raw data

Sensor wired to a Mega2560. Compiles with no errors. All I see is two numbers that do not change even when i move the compass.

I am just looking to see some raw (un- calibrated) readings, just to get a feel for the sensor and its quirks.

I took the example code and just extracted what i wanted to see (even if not perfect).

Any help is much appreciated!




// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

//#include "Streaming.h"

// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;



MagnetometerRaw raw; 


// Out setup routine, here we will configure the microcontroller and compass.
void setup()
   {
   compass.initCompass() ;
   compass.getCompass();
  
  
   // Initialize the serial port.
   Serial.begin(115200);
   delay(2000);
   Serial.println("Starting the I2C interface.");
   Wire.begin(); // Start the I2C interface.

   Serial.println("Constructing new HMC5883L");
    
   Serial.println("Setting scale to +/- 1.3 Ga");
   error = compass.setScale(1.3); // Set the scale of the compass.
   if(error != 0) // If there is an error, print it out.
      {
     Serial.println(compass.getErrorText(error));
      } 

     
   Serial.println("Setting measurement mode to continous.");
   error = compass.setMeasurementMode(MEASUREMENT_CONTINUOUS); // Set the measurement mode to Continuous
   if(error != 0) // If there is an error, print it out.
      {
      Serial.println(compass.getErrorText(error));
      }

   }

//**************************************************   L   O   O   P   **********************************************************

// Our main program loop.
void loop()
   {
   // Retrive the raw values from the compass (not scaled).
   MagnetometerRaw  raw = compass.readRawAxis();  
   Serial.print(raw.YAxis); 
   Serial.print( F( "   "  ) ) ;
   Serial.println(raw.YAxis);
   delay(100) ;
   }



The HMC5883L has not been manufactured for several years. You may have a QMC5883 instead (sometimes they are mislabeled as HMC5883).

There is a different library for that, and also, the I2C address is different, which the I2C address scanner program will report.

The underside of the sensor says "GY-273". I will try to find a QMC5883 lib.

Bingo! I downloaded the QMC lib and works just fine! Thank you (everyone) for all the help!!

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