HMC5883L Magnetometer sample code problems

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
cyanpotatoe
 
Posts: 3
Joined: Sat Jan 24, 2015 6:52 pm

HMC5883L Magnetometer sample code problems

Post by cyanpotatoe »

So I recently received the HMC5883L Magnetometer HMC5883L Magnetometer, soldered it up, and plugged it in. I've used example code for other Adafruit products, and they have worked flawlessly, but I'm having some trouble with this example sketch. Following the instructions on the learn page, I downloaded and installed the two libraries that are required for the sample code to operate. (They are the "Adafruit_Sensor" and "Adafruit_HMC5883_U" libraries.) When I open the example code "magsensor", I get pummeled with errors:

Code: Select all

In file included from magsensor.ino:34:
C:\Program Files (x86)\Arduino\libraries\Adafruit_HMC5883_Unified/Adafruit_HMC5883_U.h:93: error: expected class-name before '{' token
C:\Program Files (x86)\Arduino\libraries\Adafruit_HMC5883_Unified/Adafruit_HMC5883_U.h:99: error: 'sensors_event_t' has not been declared
C:\Program Files (x86)\Arduino\libraries\Adafruit_HMC5883_Unified/Adafruit_HMC5883_U.h:100: error: 'sensor_t' has not been declared
magsensor.ino: In function 'void displaySensorDetails()':
magsensor:41: error: 'sensor_t' was not declared in this scope
magsensor:41: error: expected `;' before 'sensor'
magsensor:42: error: 'sensor' was not declared in this scope
magsensor.ino: In function 'void loop()':
magsensor:75: error: 'sensors_event_t' was not declared in this scope
magsensor:75: error: expected `;' before 'event'
magsensor:76: error: 'event' was not declared in this scope
That's from the debugger in the Arduino IDE. It looks like the first errors are in the library itself, and so I will include the library file it's talking about. Here's Adafruit_HMC5883_Unified:

Code: Select all

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

  Designed specifically to work with the Adafruit HMC5883 Breakout
  http://www.adafruit.com/products/1746

  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.  
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/
#ifndef __HMC5883_H__
#define __HMC5883_H__

#if (ARDUINO >= 100)
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif

#include <Adafruit_Sensor.h>
#include <Wire.h>

/*=========================================================================
    I2C ADDRESS/BITS
    -----------------------------------------------------------------------*/
    #define HMC5883_ADDRESS_MAG            (0x3C >> 1)         // 0011110x
/*=========================================================================*/

/*=========================================================================
    REGISTERS
    -----------------------------------------------------------------------*/
    typedef enum
    {
      HMC5883_REGISTER_MAG_CRA_REG_M             = 0x00,
      HMC5883_REGISTER_MAG_CRB_REG_M             = 0x01,
      HMC5883_REGISTER_MAG_MR_REG_M              = 0x02,
      HMC5883_REGISTER_MAG_OUT_X_H_M             = 0x03,
      HMC5883_REGISTER_MAG_OUT_X_L_M             = 0x04,
      HMC5883_REGISTER_MAG_OUT_Z_H_M             = 0x05,
      HMC5883_REGISTER_MAG_OUT_Z_L_M             = 0x06,
      HMC5883_REGISTER_MAG_OUT_Y_H_M             = 0x07,
      HMC5883_REGISTER_MAG_OUT_Y_L_M             = 0x08,
      HMC5883_REGISTER_MAG_SR_REG_Mg             = 0x09,
      HMC5883_REGISTER_MAG_IRA_REG_M             = 0x0A,
      HMC5883_REGISTER_MAG_IRB_REG_M             = 0x0B,
      HMC5883_REGISTER_MAG_IRC_REG_M             = 0x0C,
      HMC5883_REGISTER_MAG_TEMP_OUT_H_M          = 0x31,
      HMC5883_REGISTER_MAG_TEMP_OUT_L_M          = 0x32
    } hmc5883MagRegisters_t;
/*=========================================================================*/

/*=========================================================================
    MAGNETOMETER GAIN SETTINGS
    -----------------------------------------------------------------------*/
    typedef enum
    {
      HMC5883_MAGGAIN_1_3                        = 0x20,  // +/- 1.3
      HMC5883_MAGGAIN_1_9                        = 0x40,  // +/- 1.9
      HMC5883_MAGGAIN_2_5                        = 0x60,  // +/- 2.5
      HMC5883_MAGGAIN_4_0                        = 0x80,  // +/- 4.0
      HMC5883_MAGGAIN_4_7                        = 0xA0,  // +/- 4.7
      HMC5883_MAGGAIN_5_6                        = 0xC0,  // +/- 5.6
      HMC5883_MAGGAIN_8_1                        = 0xE0   // +/- 8.1
    } hmc5883MagGain;	
/*=========================================================================*/

/*=========================================================================
    INTERNAL MAGNETOMETER DATA TYPE
    -----------------------------------------------------------------------*/
    typedef struct hmc5883MagData_s
    {
        float x;
        float y;
        float z;
      float orientation;
    } hmc5883MagData;
/*=========================================================================*/

/*=========================================================================
    CHIP ID
    -----------------------------------------------------------------------*/
    #define HMC5883_ID                     (0b11010100)
/*=========================================================================*/


/* Unified sensor driver for the magnetometer */
class Adafruit_HMC5883_Unified : public Adafruit_Sensor
{
  public:
    Adafruit_HMC5883_Unified(int32_t sensorID = -1);
  
    bool begin(void);
    void setMagGain(hmc5883MagGain gain);
    void getEvent(sensors_event_t*);
    void getSensor(sensor_t*);

  private:
    hmc5883MagGain   _magGain;
    hmc5883MagData   _magData;     // Last read magnetometer data will be available here
    int32_t         _sensorID;
    
    void write8(byte address, byte reg, byte value);
    byte read8(byte address, byte reg);
    void read(void);
};

#endif
The code won't verify (because of the errors) so I haven't even uploaded it to an Arduino yet. There is a very, very substantial chance that I have made another "stupid mistake" but I really can't solve this one. Thanks in advance for any help!!
Last edited by cyanpotatoe on Sat Jan 24, 2015 11:59 pm, edited 1 time in total.

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

Re: HMC5883L Magnetometer sample code problems

Post by Franklin97355 »

Your library files should be installed in ...\Documents\Arduino\libraries and it looks like the program is looking in C:\Program Files (x86) .... instead.

User avatar
cyanpotatoe
 
Posts: 3
Joined: Sat Jan 24, 2015 6:52 pm

Re: HMC5883L Magnetometer sample code problems

Post by cyanpotatoe »

Thanks for the quick reply!
franklin97355 wrote:Your library files should be installed in ...\Documents\Arduino\libraries and it looks like the program is looking in C:\Program Files (x86) .... instead.
I changed the location to the Arduino folder in Documents, but the problem persists.

Code: Select all

In file included from magsensor.ino:34:
C:\Users\Evan\Documents\Arduino\libraries\Adafruit_HMC5883_Unified/Adafruit_HMC5883_U.h:93: error: expected class-name before '{' token
C:\Users\Evan\Documents\Arduino\libraries\Adafruit_HMC5883_Unified/Adafruit_HMC5883_U.h:99: error: 'sensors_event_t' has not been declared
C:\Users\Evan\Documents\Arduino\libraries\Adafruit_HMC5883_Unified/Adafruit_HMC5883_U.h:100: error: 'sensor_t' has not been declared
magsensor.ino: In function 'void displaySensorDetails()':
magsensor:41: error: 'sensor_t' was not declared in this scope
magsensor:41: error: expected `;' before 'sensor'
magsensor:42: error: 'sensor' was not declared in this scope
magsensor.ino: In function 'void loop()':
magsensor:75: error: 'sensors_event_t' was not declared in this scope
magsensor:75: error: expected `;' before 'event'
magsensor:76: error: 'event' was not declared in this scope

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

Re: HMC5883L Magnetometer sample code problems

Post by Franklin97355 »

Try re downloading the sensor and HMC5883 libraries and reinstalling them. I just downloaded the HMC library and I already had the sensor library and the program compiled just fine in 1.0.6.

User avatar
cyanpotatoe
 
Posts: 3
Joined: Sat Jan 24, 2015 6:52 pm

Re: HMC5883L Magnetometer sample code problems

Post by cyanpotatoe »

Thank you!! It works perfectly!

User avatar
sologar
 
Posts: 3
Joined: Wed Mar 11, 2015 1:30 am

Re: HMC5883L Magnetometer sample code problems

Post by sologar »

Hi, sorrry for posting a reply in this thread.
I wanted yo ask here first before making another thread.
I have problems with the HMC5883L sample code:

The error code I get is:

Code: Select all

...
In file included from magsensor.ino:34:0:
C:\Users\Orlando\Documents\Arduino\libraries\Adafruit_HMC5883_U/Adafruit_HMC5883_U.h:99:10: error: conflicting return type specified for 'virtual void Adafruit_HMC5883_Unified::getEvent(sensors_event_t*)'
     void getEvent(sensors_event_t*);
It seems to be a problem with the library, I've uninstalled Arduino, and installed the last versión, I've re-downloaded the libraries, but it's the same problem.
I'm following the instructions here: https://learn.adafruit.com/adafruit-hmc ... r?view=all

Thanks

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

Re: HMC5883L Magnetometer sample code problems

Post by Franklin97355 »

I just tried with 1.6.0 and there were no errors. Did you modify the code at all and what board do you have set in the compiler?

User avatar
sologar
 
Posts: 3
Joined: Wed Mar 11, 2015 1:30 am

Re: HMC5883L Magnetometer sample code problems

Post by sologar »

franklin97355 wrote:I just tried with 1.6.0 and there were no errors. Did you modify the code at all and what board do you have set in the compiler?
I just renamed the folders (the libraries) as the instructions said.
And the board is set to arduino nano, I didn't tried with arduino UNO.
I guess i'll try that.
but arduino nano should work, right?


EDIT:
I just tried with arduino UNO board selected, and the problem is the same.
I tried downloading the library: Adafruit_LSM303DLHC-master
and that works fine, the problem is with the Adafruit_HMC5883_Unified-master library
What should I do?, I've tried changing the folder name for: Adafruit_HMC5883_U, as the instructions said but, nothing change.
I do have the Adafruit_Sensor-master folder; I also tried changing the folder name to: Adafruit_Sensor

Need help

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

Re: HMC5883L Magnetometer sample code problems

Post by Franklin97355 »

Not sure what is happening. I find that I end up naming the folder the same as the .h file inside and that has worked for me. Arduino does not like dashes in it's library names so if you have any of those remove them or change them to underscores.

User avatar
sologar
 
Posts: 3
Joined: Wed Mar 11, 2015 1:30 am

Re: HMC5883L Magnetometer sample code problems

Post by sologar »

franklin97355 wrote:Not sure what is happening. I find that I end up naming the folder the same as the .h file inside and that has worked for me. Arduino does not like dashes in it's library names so if you have any of those remove them or change them to underscores.
Thanks for your help, I'll try that.
But for now, this metod worked for me:

i downloaded this libraries and sample code:
https://github.com/pkourany/Adafruit_HMC5883_U

And it worked with and arduino UNO, i'll try it later with arduino nano.
Thanks again for your help

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

Return to “Arduino”