LSM303DLH hangs GPS reader sketch

If I use the TinyGPS code rather than nmea.h, both the gps and the LSM303DLH work and work fine.
I have another reason to prefer to use the nmea.h routines which would be more difficult implementing with TinyGPS.

Machine is Arduino Uno, using Arduino 1.0, Skylab SKM53, and DFrobotics LCD shield.

routine in this code prints lat/lon to lcd. It doesn’t work if I add the LSM303 code. It fails as soon as I add
LSM303 compass;

I suspect that i may have run out of the top of the buffer, or created some other problem that has nothing to do with the accelerometer and if you think that’s the case, just say so, and I’ll try to puzzle out my code problems on my own.

As i said, the 303DLH works fine with TinyGPS in the same hardware setup.

// works!! with $GPGGA sentences directly connected to
// Skylab gps, but not if "LSM303 Compass" entry is added jaf 1/16/12

#include <nmea.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <LSM303.h>

SoftwareSerial gpsin(2, 3);

NMEA gps(ALL);    // GPS data connection to all sentence types

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

LSM303 compass; //this single entry kills output 1/16/12 jaf

void setup() {
  lcd.print(F("GPS & HEADING"));

String boola;
String _latdeg;
String _latmin;
String _londeg;
String _lonmin;

void loop() {
  if (gpsin.available() > 0 ) {
    // read incoming character from GPS and feed it to NMEA type object
    if (gps.decode( {
      // full sentence received
      if (strncmp(gps.sentence(),"$GPGGA", 6) == 0) {
      _latdeg = (boola.substring(18,20));
      _latmin = (boola.substring(20,27));
      _londeg = (boola.substring(31,33));
      _lonmin = (boola.substring(33,40)); 
      lcd.setCursor (0,0);
      lcd.print (F("Lat: "));
      lcd.print (_latdeg);
      lcd.print (F(" "));
      lcd.print (_latmin);
      lcd.setCursor (0,1);
      lcd.print (F("Lon: "));
      lcd.print (_londeg);
      lcd.print (F(" "));
      lcd.print (_lonmin);    

If you think I need to learn how to code, it’s ok to tell me. I tend to work from ignorance and prevail by persistence.


Hello, John.

In what way does adding “LSM303 compass;” cause the code to stop working? Does the Arduino just fail to respond or behave properly even after the sketch compiles and uploads successfully? If that is the case, I suspect you might be running out of memory (RAM). Here are some functions you might be able to use to help determine whether that is the problem.

Another debugging approach you could try is to remove or comment out most of the contents of your setup() and loop() functions, then add some code to blink an LED (or something similar) so you can see if the Arduino is running normally. If that works, then you can try adding your original code back a few lines at a time until the problem shows up.

- Kevin

OMG, Sorry Kevin,
what doesn’t happen is the latitude and longitude data don’t show. The “Lat:” and “Lon:” strings show up and at about the right time intervals but the data is absent. If I comment out the LSM303 compass, then it works. It might be that my diagnostic sequence was meaningless. I started downstream removing the Heading routine which printed the heading to the LCD, then the calibration stuff, then the compass.enableDefault and it kept not printing the data for lat and lon, then when i removed LSM303 compass, it worked.

I strongly suspect that the accelerometer and it’s code may simply be pushing some other mistake that I’ve been getting away with over the top. If you’re interested, I can send the tingygps code that does work, but I’m not sure it wouldn’t be a better use of everyone’s time if I tried to tighten up my gps parsing stuff.

thanks for showing me the memory monitors. since it seems unlikely that the problem is specific to the LSM303, it might be more efficient for me to make sure I’m not running out the top of ram before we fool with this anymore.

thanks for looking at it.


problem may be with substring. …more as I work through it.

Other than causing a memory overflow, the only way I can think of that adding “LSM303 compass;” by itself could affect your program is that it might redefine a constant (macro) that your other code depends on. I don’t think this is very likely, since we tried to be pretty specific with the macro names to minimize the risk of colliding with other definitions (they all start with “LSM303”), but you could check the macros defined in LSM303.h just to make sure.

- Kevin

it is a memory overflow problem as I’ve discovered by running MemoryFree and printing output at critical points. no wonder it doesn’t work but it’s not LSM303, it is more likely something that I’m doing badly in my string handling and input. so thanks for the thoughts, and I’ll pound on with it.

the Arduino collaborators have put the string functions in the core of 1.0, if I understand correctly, and it being new, maybe they still have some problems with these functions chewing up RAM. or maybe my gps input needs better filtering.

john :slight_smile:

problem appears to be with nmea.h. It looks like it consumes most of the RAM on loading so as soon as you try to do much more than read the gps, you’ve run out the end. Much better to use TinyGPS,h and just grin and bear, adding the additional functionality needed to read nmea sentences nmot already supported.

I’m sorry my title was unclear, because clearly neither the LSM303DLH, nor the LSM303 code had anything to do with the problem.

best regards, john

Hello, John.

Thanks for the update; I’m glad you were able to figure out what was going wrong.

- Kevin