LSM303C w/ Particle Photon

Hi all,

I’m trying to get the Sparkfun LSM303C module working with a Particle Photon but have been stumped for a couple of days now.

It was easy enough to wire in:

Photon - LSM303C
GND - GND
3V3 - VDD_IN
D0 - SDA (+10k pullup from 3V3)
D1 - SCLK

But when I tried using the LSM303C Arduino Library to start seeing some values I hit some issues. I first commented out the SPI code to save re-writing all the variables / pins and ensure we are communication using I2C. As a result the code froze when Wire.endTransmission() / Wire.requestFrom() is called, causing the Photon to go into SOS mode. I doubt this issue refers to previous problems caused by these functions as they were resolved about 2-3 years ago according this forum. As a result, I found another approach to reading the accelerometer values using this forum post. This code compiles and flashes onto the device without issue but the values returned are static and unaffected by movement.

I’m not sure where i’m going wrong?

I apologise if this is a nooby question but I appreciate any help I can get :slight_smile:

Thanks,

Sam.

@sambeedell, you need a 10K pull-up on D1/SCLK as well, as per the I2C specs. Also, are you using the LSM303C library available on the web IDE?

Ah yes. My bad. Although, it did not fix the issue unfortunately.

I couldn’t see a LSM303C library on Particles’ web IDE, the closest I could find was an Adafruit_LSM303_U library which didn’t seem to work with the LSM303C.

Thanks for your reply :relieved:

@sambeedell, the LSM303C is different from the LSM303DHLC. As such, I’m not sure the Adafruit library (for the LSM303DHLC) will work with the Sparkfun board. I suggest you try using the Sparkfun library even though it is not available on the web IDE.

This is the library I am currently using which results in the problem i stated above, it causes the Photon to go into SOS mode. I tried to debug the issue to see where it gets stuck and it doesn’t get past Wire.endTransmission() / Wire.requestFrom()…

@sambeedell for some reason, I missed that! You should be able to use that library without any modifications. Can you tell me how you wired the breakout to the Photon? A picture would be good as well.

Like this:

Photon - LSM303C
GND - GND (using side rail on breadboard)
3V3 - VDD_IN (using side rail on breadboard)
D0 - SDA (+10k pullup from 3V3)
D1 - SCLK (+10k pullup from 3V3)

Sorry I cant supply a photo as it’s in my breadboard which is currently v messy with other components for a project and don’t really want to unplug them all. This is the last piece of the puzzle! I left it 'till last after testing each module individually and then giving up on this one. I’ve quickly drawn how i have it plugged in minus the other components which is also how i was been testing it.

Cheers.

@sambeedell, you need to power the board via the VDD pin as well. All you have now is power to the VDD_IO which is for the IO pins only. So connect VDD to 3.3v also :wink:

1 Like

With 3.3V you may want to try a 4k7 instead of 10k too.

1 Like

Ha, silly me.

Unfortunately I still get the same issue where the photon goes into SOS mode :confused:

Is it possible this is a result of a broken board due to running 5V through it? I’m not sure I did this but it is a possibility.

I found some alternative code that runs on the Photon without any issues other than the values returned are static.

Maybe but not likely. The "alternative code" is for the LSM303D which has a different register map and slave addresses. Perhaps it would be best to step back and run an I2C bus scanner to see if the device correctly appears on the bus. Looking at the schematics, I see that the board already has 4.7K ohm pull-ups onboard so you can remove the extra 10K ones you added. Also, as @ScruffR pointed out, there is a trace jumper already in place powering VDD and VDD_IO if you apply power to either of these.

The I2C bus scanner code is:

// --------------------------------------
// i2c_scanner
//
// http://playground.arduino.cc/Main/I2cScanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// 
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

void setup()
{

  Wire.setSpeed(CLOCK_SPEED_400KHZ );  // Run scanner with and without this line commented out
  Wire.begin();

  Serial.begin(9600);
  delay(5000);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

When you run the scanner, it should identify devices at addresses 0x1D and 0x1E. The Particle docs for setSpeed() say:

Sets the I2C clock speed. This is an optional call (not from the original Arduino specs.) and must be called once before calling begin().

I did notice in the Sparkfun library that setSpeed() is called after Wire.begin() which may be the problem. :wink:

Thanks for the insight on the onboard pull-ups and connected VDD and VDD_IO.

I do make sure to run the i2c scanner before testing the LSM303C. I tired your code which has the addition of setting the clock speed but it did not make any difference to the results:

I also couldn’t find any Wire.begin() or Wire.setSpeed() in the Sparkfun Library. Therefore I set it in my main class file before calling the Sparkfun library. It now compiles and runs fine :smiley:

Thank you so much for your help. I knew it would be something silly I was missing and just took a step back.

1 Like