LSM303 3axis mag sensor code

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.
jmulalley
 
Posts: 6
Joined: Tue Apr 16, 2013 7:30 pm

LSM303 3axis mag sensor code

Post by jmulalley »

I downloaded from Github, the sample program for the LSM303DHLC compass+accel circuit.
Here is my code, I changed #include <Adafruit_Sensor.h> to read #include "Adafruit_Sensor.h" and #include <Adafruit_LSM303.h> to read #include "Adafruit_LSM303.h"

Getting an "Error compiling"

I have the following three files in this folder:
C:\~\Documents\Arduino\libraries\Adafruit_LSM303DLHC\examples\magsensor2\

Adafruit_LSM303.h
Adafruit_Sensor.h
magsensor2.ino

Code: Select all

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

/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Mag mag = Adafruit_LSM303_Mag(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("Magnetometer Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the LSM303 ... check your connections */
    Serial.println("Ooops, no LSM303 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");
  delay(500);
}
Any help is greatly appreciated.
  • magsensor2.cpp.o: In function `__static_initialization_and_destruction_0':
    C:\Users\Jesse\Documents\Arduino/magsensor2.ino:6: undefined reference to `Adafruit_LSM303_Mag::Adafruit_LSM303_Mag(long)'
    magsensor2.cpp.o: In function `loop':
    C:\Users\Jesse\Documents\Arduino/magsensor2.ino:45: undefined reference to `Adafruit_LSM303_Mag::getEvent(sensors_event_t*)'
    magsensor2.cpp.o: In function `displaySensorDetails()':
    C:\Users\Jesse\Documents\Arduino/magsensor2.ino:11: undefined reference to `Adafruit_LSM303_Mag::getSensor(sensor_t*)'
    magsensor2.cpp.o: In function `setup':
    C:\Users\Jesse\Documents\Arduino/magsensor2.ino:30: undefined reference to `Adafruit_LSM303_Mag::begin()'

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LSM303 3axis mag sensor code

Post by adafruit_support_rick »

You don't have the libraries installed correctly. You don't want the .h files in with the .ino - they should be in the libraries folder, along with the corresponding .cpp files.

Please review our tutorial on installing arduino libraries.

jmulalley
 
Posts: 6
Joined: Tue Apr 16, 2013 7:30 pm

Re: LSM303 3axis mag sensor code

Post by jmulalley »

I have the libraries with the .h and .cpp files for each folder.

C:\~\Documents\Arduino\libraries\Adafruit_LSM303
C:\~\Documents\Arduino\libraries\Adafruit_LSM303DLHC
C:\~\Documents\Arduino\libraries\Adafruit_Sensor

I have gone through the tutorial I have verified that all the libraries are correct. This page is exactly the problem I was getting initially.
http://learn.adafruit.com/adafruit-all- ... y-problems

'xxxx' does not name a type
This is the most common library related error message and it means that the compiler could not find the library. This can be due to:
Library is not Installed (see previous pages in this guide)
Wrong Folder Location
Wrong Folder Name
Wrong Library Name
Library Dependencies
Forgot to Shutdown the IDE

I have tried compling several library examples and getting errors. You can see in these pictures that I have the libraries saved to the correct location.
Attachments
Clipboard02.jpg
Clipboard02.jpg (103.42 KiB) Viewed 3036 times
Clipboard01.jpg
Clipboard01.jpg (106.6 KiB) Viewed 3036 times

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LSM303 3axis mag sensor code

Post by adafruit_support_rick »

I think the problem is that you've got both the LSM303 and the LSM303DLHC libraries installed. You only want the LSM303DLHC.
Delete the LSM303 library, and restart the Arduino IDE.

jmulalley
 
Posts: 6
Joined: Tue Apr 16, 2013 7:30 pm

Re: LSM303 3axis mag sensor code

Post by jmulalley »

Unfortunatly, deleting the other library did not work.

From the LSM303 tutorial I copied the calibration code and tried to compile, still getting errors
http://learn.adafruit.com/lsm303-accele ... alibration

Code: Select all

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

/* Assign a unique ID to these sensors */
Adafruit_LSM303_Accel accel = Adafruit_LSM303_Accel(54321);
Adafruit_LSM303_Mag mag = Adafruit_LSM303_Mag(12345);

float AccelMinX, AccelMaxX;
float AccelMinY, AccelMaxY;
float AccelMinZ, AccelMaxZ;

float MagMinX, MagMaxX;
float MagMinY, MagMaxY;
float MagMinZ, MagMaxZ;

long lastDisplayTime;

void setup(void) 
{
  Serial.begin(9600);
  Serial.println("LSM303 Calibration"); Serial.println("");
  
  /* Initialise the accelerometer */
  if(!accel.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    while(1);
  }
  /* Initialise the magnetometer */
  if(!mag.begin())
  {
    /* There was a problem detecting the LSM303 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    while(1);
  }
  lastDisplayTime = millis();
}

void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t accelEvent; 
  sensors_event_t magEvent; 
  
  accel.getEvent(&accelEvent);
  mag.getEvent(&magEvent);
  
  if (accelEvent.acceleration.x < AccelMinX) AccelMinX = accelEvent.acceleration.x;
  if (accelEvent.acceleration.x > AccelMaxX) AccelMaxX = accelEvent.acceleration.x;
  
  if (accelEvent.acceleration.y < AccelMinY) AccelMinY = accelEvent.acceleration.y;
  if (accelEvent.acceleration.y > AccelMaxY) AccelMaxY = accelEvent.acceleration.y;

  if (accelEvent.acceleration.z < AccelMinZ) AccelMinZ = accelEvent.acceleration.z;
  if (accelEvent.acceleration.z > AccelMaxZ) AccelMaxZ = accelEvent.acceleration.z;

  if (magEvent.magnetic.x < MagMinX) MagMinX = magEvent.magnetic.x;
  if (magEvent.magnetic.x > MagMaxX) MagMaxX = magEvent.magnetic.x;
  
  if (magEvent.magnetic.y < MagMinY) MagMinY = magEvent.magnetic.y;
  if (magEvent.magnetic.y > MagMaxY) MagMaxY = magEvent.magnetic.y;

  if (magEvent.magnetic.z < MagMinZ) MagMinZ = magEvent.magnetic.z;
  if (magEvent.magnetic.z > MagMaxZ) MagMaxZ = magEvent.magnetic.z;

  if ((millis() - lastDisplayTime) > 1000)  // display once/second
  {
    Serial.print("Accel Minimums: "); Serial.print(AccelMinX); Serial.print("  ");Serial.print(AccelMinY); Serial.print("  "); Serial.print(AccelMinZ); Serial.println();
    Serial.print("Accel Maximums: "); Serial.print(AccelMaxX); Serial.print("  ");Serial.print(AccelMaxY); Serial.print("  "); Serial.print(AccelMaxZ); Serial.println();
    Serial.print("Mag Minimums: "); Serial.print(MagMinX); Serial.print("  ");Serial.print(MagMinY); Serial.print("  "); Serial.print(MagMinZ); Serial.println();
    Serial.print("Mag Maximums: "); Serial.print(MagMaxX); Serial.print("  ");Serial.print(MagMaxZ); Serial.print("  "); Serial.print(MagMaxZ); Serial.println(); Serial.println();
    lastDisplayTime = millis();
  }
}
errors

Code: Select all

calibrate_LSM303:6: error: 'Adafruit_LSM303_Accel' does not name a type
calibrate_LSM303:7: error: 'Adafruit_LSM303_Mag' does not name a type
calibrate_LSM303.ino: In function 'void setup()':
calibrate_LSM303:25: error: 'accel' was not declared in this scope
calibrate_LSM303:32: error: 'mag' was not declared in this scope
calibrate_LSM303.ino: In function 'void loop()':
calibrate_LSM303:47: error: 'accel' was not declared in this scope
calibrate_LSM303:48: error: 'mag' was not declared in this scope

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: LSM303 3axis mag sensor code

Post by adafruit_support_bill »

I am able to compile that code without errors. The error output you are getting says that the Arduino IDE did not find the libraries.

Make sure that you have correctly identified your sketchbook folder and have the libraries installed in the correct location : http://learn.adafruit.com/adafruit-all- ... -a-library

Make sure that you delete or remove any duplicate libraries from the Libraries folder. Simply renaming them will not work.

Be sure to close ALL Arduino IDE windows after making any library changes. It will not recognize the changes untilo you close all windows and restart the IDE>

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LSM303 3axis mag sensor code

Post by adafruit_support_rick »

Works for me, too. Here's what my libraries folder looks like:
magsensor.png
magsensor.png (224.35 KiB) Viewed 2972 times

jmulalley
 
Posts: 6
Joined: Tue Apr 16, 2013 7:30 pm

Re: LSM303 3axis mag sensor code

Post by jmulalley »

Thank you for the help. I started all over from new with my libraries folder, then I added the Adafruit_LSM303DHC and Adafruit_Sensors folders back into the clean Arduino 1.0.4, all is good. My question is, where should I keep my sketches at on my filesystem? I don't want to change my library folder with non working code.

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: LSM303 3axis mag sensor code

Post by adafruit_support_bill »

Your sketches should go in the sketchbook folder (the parent of the Libraries folder). http://learn.adafruit.com/adafruit-all- ... -a-library

They are saved there automatically by the IDE when you save from the File menu.

User avatar
Yeoh
 
Posts: 3
Joined: Fri Dec 11, 2015 12:43 pm

Re: LSM303 3axis mag sensor code

Post by Yeoh »

I face the same problem with you, I unable to solve it and I have follow all the instruction. Please guild me the way how to solve the 'Adafruit_LSM303_Accel_Unified does not the name type'. Thank you
Attachments
accelerometer.PNG
accelerometer.PNG (25.15 KiB) Viewed 1468 times

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LSM303 3axis mag sensor code

Post by adafruit_support_rick »

Do you have the library Adafruit_LSM303DHLC in your libraries folder?
You also need the Adafruit_Sensor library in your libraries folder.

User avatar
Yeoh
 
Posts: 3
Joined: Fri Dec 11, 2015 12:43 pm

Re: LSM303 3axis mag sensor code

Post by Yeoh »

thank you very much. It works now =).

User avatar
geocachersteve
 
Posts: 34
Joined: Mon Mar 17, 2014 12:31 am

Re: LSM303 3axis mag sensor code

Post by geocachersteve »

Hmm... same problem. Library is installed. However, I just noticed my library does not have Adafruit_LSM303DHLC the DHLC at the end.
Perhaps it's a more recent library. Mine just says Adafruit_LSM303.

User avatar
geocachersteve
 
Posts: 34
Joined: Mon Mar 17, 2014 12:31 am

Re: LSM303 3axis mag sensor code

Post by geocachersteve »

nope. just changed the problem.

User avatar
geocachersteve
 
Posts: 34
Joined: Mon Mar 17, 2014 12:31 am

Re: LSM303 3axis mag sensor code

Post by geocachersteve »

Oh my. I have a flora v2 with a lsm303 accel board soldered up. I can't get my arduino 1.0.5 to recognize the boards....not even for strand test (cannot find leonardo...even though flora is selected for the board.) I don't see the serial connection light up, but the border shows flora on port 5. So many clues, and so little success.

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

Return to “Arduino”