Go Down

Topic: First Time arduino/microcontroller user working with the LSM303C mag sensor (Read 13679 times) previous topic - next topic

GettingAlong

I was hoping to find some re-made short codes online for how to pull the magnetic readings off the sensor for the sensor I bought. However, it appears the LSM303C is too new and there hasn't been anything uploaded for it. I did find a code for a similar sensor, LSM303DLH, but I a too unsure if it can be ported with changes made to register values, etc.

link to the code:
http://www.dfrobot.com/wiki/index.php/LSM303_Tilt_Compensated_Compass(SEN0079)

link to LSM303C data sheet:
http://www.st.com/web/en/resource/technical/document/datasheet/DM00089896.pdf

link to LSM303DLH data sheet:
http://www.st.com/web/en/resource/technical/document/datasheet/CD00260288.pdf

Being very inexperienced I always question my judgement for this. But looking at the code and comparing data sheets does this code seem viable for what I am looking for?

Peter_n

The LSM303C is the newer version of LSM303DLH. I can't find how they differ or what has changed  =(
I guess they are the same for the software library.
Even ST doesn't have a document for the differences. The accelerator range is lower, and the magnetic range is higher.
http://www.st.com/web/catalog/sense_power/FM89/SC1449
You can ask ST if the software interface is still the same.

Adafruit has a library and tutorial for the LSM303DLH.
http://www.adafruit.com/products/1120
https://learn.adafruit.com/lsm303-accelerometer-slash-compass-breakout/coding

GettingAlong

A quick read over the I2C/SPI timing diagrams shows they two sensors appear to have the same comms protocols. And the only major difference I can see is the number of pins on the DLH is larger than 303C. But they do have matching pins with similar descriptions. I am thinking they simplified the pin layout from LSM303DLh to the LSM303C.

Side question though is that sample code really all I would need to get the mag readings, assuming I can make changes to match up with my sensor? The code seems too simple.

Peter_n

Give it a try.
It is a 3.3V sensor, which Arduino board are you using ?


Peter_n

The mega has a 5V I2C-bus, you can not connect those SCL and SDA to the sensor.
Or do you have a LSM303C breakout board with level shifters ? If not, you need a I2C level shifter.

GettingAlong

I am rather lost on what a level shifter is. My sensor is soldered to a small board with pins that I have connected to a bread board. I built the remaining circuit on the breadboard.

GettingAlong

http://www.adafruit.com/products/757

Is that what you are talking about?

Peter_n

Yes, but there are easier modules.
I use these small level shifters:
http://www.ebay.com/sch/i.html?LH_BIN=1&_sop=15&_osacat=0&_from=R40&LH_PrefLoc=2&_nkw=i2c+level+-channel+-m32&_sacat=0

When the SDA and SCL have pullup resistors to 5V, the high level is 5V and I call that a 5V I2C-bus. That is too much for a 3.3V chip.
A level shifter connects a 5V I2C bus to a 3.3V I2C bus in both ways, completely transparent.
The Arduino Mega2560 is one of the few (or the only) board that has pullup resistors of 10k from SDA and SCL to 5V.

GettingAlong

What do you mean easier modules? Because those are all from china and I can't waste a month waiting for them to ship.....

GettingAlong

Hmmm I assume you are aware of the arduino Due. Having never heard of this I am interested because of the fact it is based on an arm processor. The fact it operates at 3.3V is a side plus. I am eventually going to have to port my code to an arm processor, unrelated to arduino. Is it possible the Due could make the overall process easier?

Also for the level shifters to work do I just assemble the circuit with all lines from the sensor pass through the shifter which is then connected to the corresponding arduino pins?

michinyon


GettingAlong

Fair enough. Well I ordered a level shifter should e here tomorrow. Once it is placed in the circuit anything special need to be done to the code or is it a seamless integration?

Peter_n

An I2C level shifter is fully transparent. Everythin on the 3.3V bus and everything on the 5V bus is connected, like there is no barrier at all.

It also means that a 3.3V module must pull down the 3.3V and the 5V voltage when communicating. So if you have many 5V modules and many 3.3V modules, and each module with pullup resistors for the I2C bus,  you must calculate the total pullup resistor to see if it is not too low impedance.

The Due is the first and yet only Arduino with ARM processor (Arduino Tre coming soon)
http://arduino.cc/en/Main/arduinoBoardDue
Not all user made libraries do work well with the Due yet.

I don't know how to connect, I only use those simple level shifters from Ebay ;)

GettingAlong

The one I ordered was from Sparkfun because I needed it here in a timely manner but it does not appear to be any different. Any chance you could explain the purpose of a snippet of code from the library I found? I cannot figure out the reason for the hex values used because I cannot find them in the data sheet. The #define values used do not correlated with anything as far as I can tell.

#define LSM303_ACC 0x18
#define LSM303_MAG 0x1E


byte LSM303_read(byte address)
{
  byte temp;
   
  if (address >= 0x20)
    Wire.beginTransmission(LSM303_ACC);
  else
    Wire.beginTransmission(LSM303_MAG);
     
  Wire.write(address);
   
  if (address >= 0x20)
    Wire.requestFrom(LSM303_ACC, 1);
  else
    Wire.requestFrom(LSM303_MAG, 1);
  while(!Wire.available())
    ;
  temp = Wire.read();
  Wire.endTransmission();
   
  return temp;
}

void LSM303_write(byte data, byte address)
{
  if (address >= 0x20)
    Wire.beginTransmission(LSM303_ACC);
  else
    Wire.beginTransmission(LSM303_MAG);
     
  Wire.write(address);
  Wire.write(data);
  Wire.endTransmission();
}

Go Up