Arduino & LIS302DL

February 13, 2009

in Sensors, Tinkering

Ages ago I bought an LIS302DL from Sparkfun. One of the old (little bit) messed up ones, but still 3 axis accelerometer with I2C output. No external parts. No pain. Fine!

Before I go in any further investigation. I need to get it up & running with my arduino (actually an boarduino – but who cares?). Read all the nifty details after the break.

The hookup is very easy (but a little bit off standard) – remember we use I2C:

  • VCC is connected to pin 13 – so you can power this from the arduino. But you should also be able to power it directly from VCC. Remember it was 3.3V
  • GND is connected to GND – this was a bit obvious.
  • SCL is connected to analog pin 5 (arduino’s I2C SCL)
  • SCA is connect to analog pin 4 (arduino’s I2C SCA)
  • MISO was tied to GND – in order to select the I2C address 28 – tie this to VCC and you got address 29 – if you want to use two LIS302DL – there is perhaps some reason for this – who knows?
  • INT2 is connected to pin 2 – in order to be able to use the interrupt. I did not – who knows? Perhaps I will do it later

The basic setup was the same as with my ADJD experiments. A 3.3V breadboard and the same I2C-routines for reading and setting registers:

void writeRegister(unsigned char r, unsigned char v)
{
  Wire.beginTransmission(I2C_ADDRESS);
  Wire.send(r);
  Wire.send(v);
  Wire.endTransmission();
}
 
unsigned char readRegister(unsigned char r)
{
  unsigned char v;
  Wire.beginTransmission(I2C_ADDRESS);
  Wire.send(r);  // register to read
  Wire.endTransmission();
 
  Wire.requestFrom(I2C_ADDRESS, 1); // read a byte
  while(!Wire.available()) {
    // waiting
  }
  v = Wire.receive();
  return v;
}

First of all we power up the LIS302DL and initialize it:

void setupLIS() {
 
  Serial.println("Setting up LIS302DL");
 
  pinMode(LIS302_POWER_PIN, OUTPUT); //the power pin
  pinMode(LIS302_INT2_PIN, INPUT); //the int2 pin
 
  digitalWrite(LIS302_POWER_PIN,LOW); //switch off the LIS302
 
  Wire.begin();        // join i2c bus (address optional for master)
 
}
 
void startLIS() {
 
  Serial.println("Powering up LIS302DL");
  digitalWrite(LIS302_POWER_PIN,HIGH); //switch off the LIS302
 
  delay(POWER_UP_DELAY);
  writeRegister(CTRL_REG1 , _BV(CTRL_PD) | _BV(CTRL_XEN) | _BV(CTRL_YEN) | _BV(CTRL_ZEN));
 
  //read who am i
  char myself = readRegister(REGISTER_WHO_AM_I);
  if (myself == WHO_AM_I_RESULT) {
    Serial.println("yippie got an lis302DL!");
  } else {
    Serial.print("found an ");
    Serial.print(myself, HEX);
    Serial.println("! very strange!");
  }
}

The routine setupLIS() does the basic initialization while startLIS() does everything to power it up (I put it in two routines since I thought I would write an power down routine – but this did not happen until now).
Most of the magic is in the routine

  writeRegister(CTRL_REG1 , _BV(CTRL_PD) | _BV(CTRL_XEN) | _BV(CTRL_YEN) | _BV(CTRL_ZEN));

It stops internal power down (CTRL_PD), and enables all three axis for detection. After this is done I check if the LIS302DL does correctly report to ‘who am I’ – register 0×0f. This is more or less an sanity check (as you can see by the diagnostic output).
After you have down the right initialization magic the readout is an charm:

  char x = readRegister(X_OUT);
  char y = readRegister(Y_OUT);
  char z = readRegister(Z_OUT);

Right now I just read it out every 100ms – you could of courser do it in a more sophisticated way by waiting until new data is ready by watching the status registers or using interrupts. But there is always something better to do. Perhaps I will add it later.

And before you ask how to do it yourself: Check the source code!

{ 2 trackbacks }

Properly decoupling LIS302DL acceleration sensor | Interactive Matter
December 15, 2009 at 21:21
Filtering Sensor Data with a Kalman Filter | Interactive Matter
January 22, 2010 at 18:40

{ 13 comments… read them below or add one }

1 Leo Kacenjar October 2, 2009 at 04:44

Thank you very much! This worked really well on the first try. That doesn’t happen often. Now to try the ADJD tutorial… I’ve had one for a few months and havn’t had a chance to try it out yet.

Reply

2 JJ October 12, 2009 at 11:07

I about to interface an atmega168 with a LIS302. I’ve checked both datasheets and even though the former Vcc is 5V and the later is 2.5, they look electrically compatible which it comes to level interpretation.

I wonder though is the atmega168 5V outputs in the SCL and SDA lines will blow the LIS302 in the long run. Page14 of the LIS302 makes me think that if I power it at 5V I will not exceed maximum ratigs… but I wonder if you are of a different opinion.

Reply

3 Marcus October 12, 2009 at 13:45

The datasheet states that the LIS302DL should be operated from 2.16V to 3.6V. The digital signals must not be over VDD+0.3V.

So you probably can use 5V signals. But that perhaps destroy your chip or reduce its life span.

Try to use a ‘level shifter’ for the digital signals (there are myriad of options).

4 Thomas November 8, 2009 at 20:53

Hi! I’m new to the Arduino, so I’m just figuring things out.

I have a Duemilanove and what appears to be a newer version of the LIS302DL breakout from Sparkfun. I have a few of questions:

In place of the SCA pin, the version I have has a MOSI pin. According to the schematic, this appears to be the same as the SCA pin. Is this correct? (Link to schematic below.)

http://www.sparkfun.com/datasheets/Sensors/Accelerometer/LIS302DL-Breakout-v13.pdf

I was also confused that you connected VCC to pin 13. My understanding was that pin 13 was 5V, not 3.3V. Is that not the case?

Finally, I noticed that you connected the CS pin to ground. I this in the datasheet, though:
“To select/exploit the I2C interface, CS line must be tied high (i.e connected to Vdd_IO).”
Does this mean I should connect CS to 3.3V?

http://www.st.com/stonline/products/literature/ds/12726.pdf (datasheet)

Thanks so much!

Reply

5 Marcus November 9, 2009 at 11:29

Yes, you are right. The LIS302DL share its pins for the I2C and SPI interface. Only the CS pin decides which protocol is used.
I hooked VCC to pin 15 since I was powering the whole Arduino rig with 3.3V. This is completely out of spec and I wonder why it worked at all. Think of it as an low voltage overclocked variant.

6 Thomas November 14, 2009 at 21:00

Marcus – Thanks for the quick response and sorry for my slow one. :-)

I now have the pins hooked up like this:
VCC – 3V3 (next to RESET on the arduino)
GND – GND
SCL – Analog in pin 5
MOSI – Analog in pin 4
MISO – GND
CS – 3V3

When I run the sketch you included, I see this:

Setting up LIS302DL

Powering up LIS302DL

What am I missing?

Thanks!

Reply

7 Marcus November 15, 2009 at 12:52

Hmm that seems to be right.

After I see the debugging code again I remember that I used one pin of the Arduino to power the LIS302DL. This is not necessarily needed – but at least it guarantees a proper reset. So no problem from here.

On the other hand it fails after the first response from the I2C command. Perhaps it is missing pull up resistors are the reason. Try adding two 4.7K resistor from SCL and SDA to 3.3V. I omitted them – but still does not know why it works without them ;)
So that should be the first test.

SCL pin 5 is OK.
MOSI pin 4? It is meant to be SDA. Probably you just did a mistake writing it up ;)

If you got an oscilloscope you can watch the communication. If you don’t own an oscilloscope you can build an poor mans oscilloscope (as described in http://www.ledametrix.com/oscope/index.html). But be warned. You can destroy your Arduino, sound card, sensor or computer with it if something goes wrong (what most probably will not).

Reply

8 Gary December 2, 2009 at 22:21

Hi, Hope you guys can help me. I am using the LIS302DL via the SPI lines and all is working fine accept the Power down mode. The datasheet specs typical 1uA but when I power down I get 130uA. My application is extremely power sensitive and I did not connect the LIS to a I/O pin of my micro. When I remove the LIS from my pcb then I get 0.78uA and when added and PD is off (Unit On) then I get 300uA which is correct to the datasheet. I have even ordered new chip and was given samples (Not sure if these where old samples :-) ) As any body test the PD mode and actually measured the current consumption? They state that there is a power on time but nothing about a power off time. Any help will be very much appreciated.

Reply

9 Marcus December 3, 2009 at 08:08

Ok, that is very specific. I have not tried the power down mode yet.
And I think I do not have the equipment to test this ;)
But I hope someone else has the solution!

10 Tarinee December 12, 2009 at 11:25

Hi,
I have problem like Thomas(I’m new in Arduino,) and I tried to add two 4.7K resistor from SCL and SDA to 3.3V like you suggested. After run your program I see only this:

Setting up LIS302DL

Powering up LIS302DL

Help me please.

Reply

11 Marcus December 12, 2009 at 16:47

One Idea for a quick check: where did you tie the MISO pin to? With that pin you select the I2C address (check the datasheet). Perhaps you left it floating or tied it to VCC instead?

12 Marcus December 12, 2009 at 17:15

I rechecked the sketch (for other purposes) and it works fine. Perhaps try to power the LIS302DL from the Arduino to get a real reset of the device?
I recently retested it with an C sketch for the ATmega – worked fin too.
Are you powering the system with 3.3V?

Reply

13 tarinee December 13, 2009 at 17:26

Hi Marcus
I’ve set my system like Thomas, and tie the MISO to GND, powered the system
with 3.3V (next to reset).My arduino is ATmega168. But it didn’t work, i don’t know what am i missing?
I have a question ” INT2 is connected to pin 2 ”
Dose pin 2 is digital input of arduino?
tomorrow i’ll try to test the system again.
today….thank you for your suggestion.

Reply

Leave a Comment

Previous post:

Next post: