[SOLVED]AdaFruit_LSM303 errors

Hi All,

I am trying to get my spark to talk to the Adafruit LSM303 Accelerometer over I2C, I have them wired with 4K7 pullups and I have gone here

for the files for the unit from where I copied the .cpp and .h into tabs on a new Spark Build and tried to Verify and got errors about Wire.h so as this is included with the Spark I commented that out and then got lots of other errors like:

In file included from LSM303.cpp:1:0:
LSM303.h:163:12: error: 'int16_t' was not declared in this scope
vector<int16_t> a; // accelerometer readings 
LSM303.h:163:19: error: template argument 1 is invalid
vector<int16_t> a; // accelerometer readings 
LSM303.h:164:12: error: 'int16_t' was not declared in this scope
vector<int16_t> m; // magnetometer readings 
LSM303.h:164:19: error: template argument 1 is invalid
vector<int16_t> m; // magnetometer readings 
LSM303.h:165:12: error: 'int16_t' was not declared in this scope
vector<int16_t> m_max; // maximum magnetometer values, used for calibration 
LSM303.h:165:19: error: template argument 1 is invalid
vector<int16_t> m_max; // maximum magnetometer values, used for calibration 

I am tried looking around on this site and googling but to now avail, I am on the verge of writing the library’s from scratch in my own way but was hoping that someone could point me in the right direction of using these and getting it to work. I purchased the units from Adafruit and will hopefully need lots of them is the system works as I need but first I need to get it working.



@Cliff, I ported the LSM303 library some time ago. It uses Adafruit’s unified sensor library. You can find the library files here. You will need to copy the LSM303 .cpp and .h files, as well as, the Sensor_Master .cpp and .h files to get everything to compile. You can use either of the LSM303 example .ino files are your “app”.

You may want to explore Spark DEV to compile your code. If you do, then all you have to do is copy all the .cpp and .h files mentioned above, including the example .ino into a single directory and then compile the directory. :smile:

1 Like

Thank you very much that is perfect I have also installed the Spark Dev and compiled from there as well so thanks.

But (There had to be one :wink: )

I have the spark connected to my laptop via Com7 and breathing with the code flashed but I am seeing nothing on the Serial Monitor in Spark Dev or with PuTTY. So the next question is what am I doing wrong here I loaded the accelsensor.ino as a start and want to prove that before I edit to suit my needs.

Again thanks for the help so far it much better than my first attempts.


I add

while(!Serial.available()) SPARK_WLAN_Loop();

to the code like so:

void setup(void) 
  while(!Serial.available()) SPARK_WLAN_Loop();
  Serial.println("Accelerometer Test"); Serial.println("");
  /* Initialise the sensor */
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
  /* Display some basic information on this sensor */

So the core now connects and my laptop see’s it but pressing a key does nothing…

As you can no guess tell I am new to Spark and trying to learn how to use it before I dive in with my ideas.

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

How do you confirm that your OS can see the Core?
What OS are you running?
If Windows, have you installed the driver mentioned here?
If you use the Spark Dev Serial Monitor, you have to type in a letter and send it with Enter

To see if your keypress reaches the Core try to add this after your first while() loop

  pinMode(D7, OUTPUT);
  digitalWrite(D7, HIGH);

and at the end of setup() add

  digitalWrite(D7, LOW);

So if the blue LED lights up the keypress reaches the Core, if it goes off again your setup() finishes and the Core will execute loop() regularly.
If it never goes on you are not really seeing the Core from your serial monitor.

I’d also add SPARK_WLAN_Loop(); inside your while(1) to allow a reflash without reset.

For testing you should always add some additional means to distinguish how far your code works and where it fails.

To format your code and posts in this forum please have a look here

@Cliff, besides @ScruffR advice, you may also want to run this I2C scanner app to make sure your core sees the LSM303:

@peekay123 thanks for the link I have downloaded this into Spark Dev and again the verify says that it can’t find <Wire.H> line 28 which is again an Arduino file.


I’ve copy pasted the code above and it compiled just fine.

What is your code line 28?
If you copied it all line 28 is a comment line.

If you happened to uncomment #include "Wire.h" then don’t - the Wire object is already part of the Core standard firmware (included via application.h).

@ScruffR thanks for the quick reply I realised from the DOC’s that Wire.h is part of the spark build so I commented out and it’s building now and installed. Just don’t seem to be able to connect to my core at the moment so still playing…

@Cliff, have you read my post about driver install and confirmation if your Core is visible to the OS?

@ScruffR Yes I have intermittent connection with my core I would use the app to check things but I have a windows Phone so thats not going to work for me.

What’s not going to work with the Win Phone?

Or the other way round, what have I suggested that seems to require the use of a phone?

@ScruffR I can’t use the Tinker app on WinPhone but I got it working again by Factory Reset so found the I2C addresses for the two parts just need to get the code working now to use the properly.

@peekay123 I have run the scanner app and found that it returns two addresses for the LSM303 so I guessed that one is the Accel and one the Mag and I put these addresses (1D & 6B) into the following replacing what was there…

    #define LSM303_ADDRESS_ACCEL          (0x6B >> 1)         //0x32 - 0011001x
    #define LSM303_ADDRESS_MAG            (0x1D >> 1)         //0x3C - 0011110x

I then tried many times to get this working and swapped the addresses around and put Print statements into the code to try and debug what was happening. SO far I have worked out that it is getting stuck here…

    @brief  Reads the raw data from the sensor
void Adafruit_LSM303_Mag_Unified::read()
  // Read the magnetometer
  Wire.requestFrom((byte)LSM303_ADDRESS_MAG, (byte)6);

  // Wait around until enough data is available
  while (Wire.available() < 6);    //<---------------------------HERE!!!!!!!!!!

SO I am wondering if anyone can spot what I have missed or doing wrong…



@Cliff, whatever you get from the scanner you use as the address - no shifting (>> 1). If the scanner found 0x1D and 0x6B then use those unmodified. I suspect 0x1D is ACCEL and 0x6B is the MAG. If that doesn’t work, swap them :smile:

@peekay123 thanks that worked great I was only shifting as the original code from Github was shifting the address so I assumed there was a reason rather than actually looking deep into the code.

Anyway works great just need to play with getting the Mag working now then I can design the code for my application.

Again thanks.


1 Like

4 posts were merged into an existing topic: Adafruit LSM303 pushing Variable into the cloud