GY-271 (HMC5882_L) Error

hey, I had purchased a Gy-271 HMC5883_L magnetometer sensor for my project

I downloaded the library and extracted it to libraries folder inside my Arduino folder

downloaded from here:https://github.com/jarzebski/Arduino-HMC5883L

But whenever I run the code, I get tons of errors saying like

“‘class HMC5883L’ has no member named ‘begin’”
“‘class HMC5883L’ has no member named ‘setRange’”

Arduino: 1.8.4 (Windows 8.1), Board: “Arduino/Genuino Uno”

C:\Program Files (x86)\Arduino\libraries\Arduino-HMC5883L-master\HMC5883L_simple\HMC5883L_simple.ino: In function ‘void setup()’:

HMC5883L_simple:20: error: ‘class HMC5883L’ has no member named ‘begin’

while (!compass.begin())

^

HMC5883L_simple:35: error: ‘class HMC5883L’ has no member named ‘setRange’

compass.setRange(HMC5883L_RANGE_1_3GA);

^

HMC5883L_simple:35: error: ‘HMC5883L_RANGE_1_3GA’ was not declared in this scope

compass.setRange(HMC5883L_RANGE_1_3GA);

^

HMC5883L_simple:41: error: ‘class HMC5883L’ has no member named ‘setMeasurementMode’

compass.setMeasurementMode(HMC5883L_CONTINOUS);

^

HMC5883L_simple:41: error: ‘HMC5883L_CONTINOUS’ was not declared in this scope

compass.setMeasurementMode(HMC5883L_CONTINOUS);

^

HMC5883L_simple:51: error: ‘HMC5883L_DATARATE_15HZ’ was not declared in this scope

compass.setDataRate(HMC5883L_DATARATE_15HZ);

^

HMC5883L_simple:58: error: ‘class HMC5883L’ has no member named ‘setSamples’

compass.setSamples(HMC5883L_SAMPLES_1);

^

HMC5883L_simple:58: error: ‘HMC5883L_SAMPLES_1’ was not declared in this scope

compass.setSamples(HMC5883L_SAMPLES_1);

^

C:\Program Files (x86)\Arduino\libraries\Arduino-HMC5883L-master\HMC5883L_simple\HMC5883L_simple.ino: In function ‘void checkSettings()’:

HMC5883L_simple:68: error: ‘class HMC5883L’ has no member named ‘getRange’

switch (compass.getRange())

^

^

HMC5883L_simple:106: error: ‘HMC5883L_SAMPLES_1’ was not declared in this scope

case HMC5883L_SAMPLES_1: Serial.println(“1”); break;

^

HMC5883L_simple:107: error: ‘HMC5883L_SAMPLES_2’ was not declared in this scope

case HMC5883L_SAMPLES_2: Serial.println(“2”); break;

^

HMC5883L_simple:108: error: ‘HMC5883L_SAMPLES_4’ was not declared in this scope

case HMC5883L_SAMPLES_4: Serial.println(“4”); break;

^

HMC5883L_simple:109: error: ‘HMC5883L_SAMPLES_8’ was not declared in this scope

case HMC5883L_SAMPLES_8: Serial.println(“8”); break;

^

C:\Program Files (x86)\Arduino\libraries\Arduino-HMC5883L-master\HMC5883L_simple\HMC5883L_simple.ino: In function ‘void loop()’:

HMC5883L_simple:117: error: ‘Vector’ was not declared in this scope

Multiple libraries were found for “HMC5883L.h”
Vector raw = compass.readRaw();

Used: C:\Program Files (x86)\Arduino\libraries\HMC5883L
^

Not used: C:\Program Files (x86)\Arduino\libraries\Arduino-HMC5883L-master
HMC5883L_simple:118: error: expected ‘;’ before ‘norm’

Vector norm = compass.readNormalize();

^

HMC5883L_simple:121: error: ‘raw’ was not declared in this scope

Serial.print(raw.XAxis);

^

HMC5883L_simple:127: error: ‘norm’ was not declared in this scope

Serial.print(norm.XAxis);

^

exit status 1
‘class HMC5883L’ has no member named ‘begin’

etc etc

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Here is the code

#include <Wire.h>
#include <HMC5883L.h>

HMC5883L compass;

void setup()
{
  Serial.begin(9600);

  // Initialize HMC5883L
  Serial.println("Initialize HMC5883L");
  while (!compass.begin())
  {
    Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
    delay(500);
  }
  
  // Set measurement range
  // +/- 0.88 Ga: HMC5883L_RANGE_0_88GA
  // +/- 1.30 Ga: HMC5883L_RANGE_1_3GA (default)
  // +/- 1.90 Ga: HMC5883L_RANGE_1_9GA
  // +/- 2.50 Ga: HMC5883L_RANGE_2_5GA
  // +/- 4.00 Ga: HMC5883L_RANGE_4GA
  // +/- 4.70 Ga: HMC5883L_RANGE_4_7GA
  // +/- 5.60 Ga: HMC5883L_RANGE_5_6GA
  // +/- 8.10 Ga: HMC5883L_RANGE_8_1GA
  compass.setRange(HMC5883L_RANGE_1_3GA);

  // Set measurement mode
  // Idle mode:              HMC5883L_IDLE
  // Single-Measurement:     HMC5883L_SINGLE
  // Continuous-Measurement: HMC5883L_CONTINOUS (default)
  compass.setMeasurementMode(HMC5883L_CONTINOUS);
 
  // Set data rate
  //  0.75Hz: HMC5883L_DATARATE_0_75HZ
  //  1.50Hz: HMC5883L_DATARATE_1_5HZ
  //  3.00Hz: HMC5883L_DATARATE_3HZ
  //  7.50Hz: HMC5883L_DATARATE_7_50HZ
  // 15.00Hz: HMC5883L_DATARATE_15HZ (default)
  // 30.00Hz: HMC5883L_DATARATE_30HZ
  // 75.00Hz: HMC5883L_DATARATE_75HZ
  compass.setDataRate(HMC5883L_DATARATE_15HZ);

  // Set number of samples averaged
  // 1 sample:  HMC5883L_SAMPLES_1 (default)
  // 2 samples: HMC5883L_SAMPLES_2
  // 4 samples: HMC5883L_SAMPLES_4
  // 8 samples: HMC5883L_SAMPLES_8
  compass.setSamples(HMC5883L_SAMPLES_1);

  // Check settings
  checkSettings();
}

void checkSettings()
{
  Serial.print("Selected range: ");
  
  switch (compass.getRange())
  {
    case HMC5883L_RANGE_0_88GA: Serial.println("0.88 Ga"); break;
    case HMC5883L_RANGE_1_3GA:  Serial.println("1.3 Ga"); break;
    case HMC5883L_RANGE_1_9GA:  Serial.println("1.9 Ga"); break;
    case HMC5883L_RANGE_2_5GA:  Serial.println("2.5 Ga"); break;
    case HMC5883L_RANGE_4GA:    Serial.println("4 Ga"); break;
    case HMC5883L_RANGE_4_7GA:  Serial.println("4.7 Ga"); break;
    case HMC5883L_RANGE_5_6GA:  Serial.println("5.6 Ga"); break;
    case HMC5883L_RANGE_8_1GA:  Serial.println("8.1 Ga"); break;
    default: Serial.println("Bad range!");
  }
  
  Serial.print("Selected Measurement Mode: ");
  switch (compass.getMeasurementMode())
  {  
    case HMC5883L_IDLE: Serial.println("Idle mode"); break;
    case HMC5883L_SINGLE:  Serial.println("Single-Measurement"); break;
    case HMC5883L_CONTINOUS:  Serial.println("Continuous-Measurement"); break;
    default: Serial.println("Bad mode!");
  }

  Serial.print("Selected Data Rate: ");
  switch (compass.getDataRate())
  {  
    case HMC5883L_DATARATE_0_75_HZ: Serial.println("0.75 Hz"); break;
    case HMC5883L_DATARATE_1_5HZ:  Serial.println("1.5 Hz"); break;
    case HMC5883L_DATARATE_3HZ:  Serial.println("3 Hz"); break;
    case HMC5883L_DATARATE_7_5HZ: Serial.println("7.5 Hz"); break;
    case HMC5883L_DATARATE_15HZ:  Serial.println("15 Hz"); break;
    case HMC5883L_DATARATE_30HZ: Serial.println("30 Hz"); break;
    case HMC5883L_DATARATE_75HZ:  Serial.println("75 Hz"); break;
    default: Serial.println("Bad data rate!");
  }
  
  Serial.print("Selected number of samples: ");
  switch (compass.getSamples())
  {  
    case HMC5883L_SAMPLES_1: Serial.println("1"); break;
    case HMC5883L_SAMPLES_2: Serial.println("2"); break;
    case HMC5883L_SAMPLES_4: Serial.println("4"); break;
    case HMC5883L_SAMPLES_8: Serial.println("8"); break;
    default: Serial.println("Bad number of samples!");
  }

}

void loop()
{
  Vector raw = compass.readRaw();
  Vector norm = compass.readNormalize();

  Serial.print(" Xraw = ");
  Serial.print(raw.XAxis);
  Serial.print(" Yraw = ");
  Serial.print(raw.YAxis);
  Serial.print(" Zraw = ");
  Serial.print(raw.ZAxis);
  Serial.print(" Xnorm = ");
  Serial.print(norm.XAxis);
  Serial.print(" Ynorm = ");
  Serial.print(norm.YAxis);
  Serial.print(" ZNorm = ");
  Serial.print(norm.ZAxis);
  Serial.println();  

  delay(100);

How did you call the folder in the library? Try with HMC5883L

I'm using HMC5883L sensor only

is that the answer to my question?

yes ! I think I misunderstood your answer. Can you please elaborate a little so that I can understand what you're suggesting?

you have library implementation problem. first look inside <HMC5883L.h> and see if there is class name vector . if it has it then you should reupload library. There is a comminication error .

if there is not a class by that name then you need a different file to make it work .

Hitesh_KHD:
yes ! I think I misunderstood your answer. Can you please elaborate a little so that I can understand what you’re suggesting?

Not sure how to say that differently :slight_smile:

Let me try: when you unzipped the github file and stored the resulting folder into your library, what folder name did you use? if this is not HMC5883L (with nothing else before or after) try changing the folder’s name to HMC5883L. Quit the IDE and relaunch And try compiling again.(my guess is that the structure of the folder is dated and is not matching what the IDE expects - should have src, examples as modern libraries maybe). Also ensure you have nothing else called HMC5883L.h in your computer.

Alternatively don’t store the folder in library and try duplicating the folder HMC5883L_compass on your desktop, in which you copy as well the HMC5883L.cpp and HMC5883L.h. That will make the thing self contained.
Open the HMC5883L_compass.ino in the IDE (you should also see two other tabs with the .h and .cpp) and modify the #include <HMC5883L.h> into #include “HMC5883L.h”. Do the same in the HMC5883L.cpp file. Then try compiling

Hey, Thank you so much for replying!

I had a couple of HMC5883L files in my library which I deleted and did exactly as you told and now I have successfully compiled and uploaded the code!

But, when I run the code, I don't get the values, instead on serial monitor it says

"Could not find a valid HMC5883L sensor, check wiring!"

I cross checked my connections and verified it, there is nothing wrong with the connection!

Any idea why is this happening?

How did you wire it? what arduino? How is the whole thing powered?
Where did you buy it from?
You could check adafruit’s tutorial and code

You could also double check that your unit is on the right I2C address (0x3C ?)with an I2C scan

Post an image if you can (using the image tags)

Hey. Thank you so much for replying!

So here are the details :

1 - I am using a basic Arduino UNO board

2 - Here are the connections (VCC -(3.3/5 V), GND-GND,SCL-A5,SDA-A4)

3- the Arduino Uno board was available at my office(not sure from where it was purchased)

4- The sensor was purchased offline ( from an electrical shop)

5- I am using I2Cdev library developed by Jeff rowberg

I have added few images to attachments! Sorry for the bad quality of images!

p.s - I am someone from software background and very much new to this hardware world

Looks similar to the Adafruit one, try their tutorial I pointed above

Hey ! I tried following the tutorial and did exactly as it was told

I successfully compiled and uploaded the code, But it outputs only the sensor details and not the sensor values!

I tried debugging and found that the function getEvent is running infinite and doesn't return from there.
on my serial monitor, "inside for loop gets printed" and then nothing is printing.

Here is the code snippet:-

void loop() 
{
  Serial.println("inside for loop");
  /* Get a new sensor event */ 
  sensors_event_t event; 
 
   mag.getEvent(&event);

 Serial.println("Outputiing values....");
 Serial.print(event.magnetic.x);
 Serial.println("outputted X values");