LIS331 problem

Hello!

I am trying to configure my LIS331 accelerometer http://www.sparkfun.com/datasheets/Sensors/Accelerometer/LIS331HH.pdf, using this code

/* Wiring:
UNO LIS331

3.3V VCC
GND GND
10 CS
11 SDA/SDI
12 SA0/SDO
13 SCL/SPC
*/

#include <SPI.h>
#include <stdlib.h>
#include <stdio.h>

#define SS 10 // Serial Select -> CS on LIS331
#define MOSI 11 // MasterOutSlaveIn -> SDI
#define MISO 12 // MasterInSlaveOut -> SDO
#define SCK 13 // Serial Clock -> SPC on LIS331

#define SCALE 0.0007324; // approximate scale factor for full range (+/-24g)
// scale factor: +/-24g = 48G range. 2^16 bits. 48/65536 = 0.0007324

// global acceleration values
double xAcc, yAcc, zAcc;

void setup()
{
Serial.begin(9600);

// Configure SPI
SPI_SETUP();

// Configure accelerometer
Accelerometer_Setup();
}


void loop()
{
readVal(); // get acc values and put into global variables

Serial.print(xAcc, 1);
Serial.print(",");
Serial.print(yAcc, 1);
Serial.print(",");
Serial.println(zAcc, 1);

delay(1000);
}

// Read the accelerometer data and put values into global variables
void readVal()
{
byte xAddressByteL = 0x28; // Low Byte of X value (the first data register)
byte readBit = B10000000; // bit 0 (MSB) HIGH means read register
byte incrementBit = B01000000; // bit 1 HIGH means keep incrementing registers
// this allows us to keep reading the data registers by pushing an empty byte
byte dataByte = xAddressByteL | readBit | incrementBit;
byte b0 = 0x0; // an empty byte, to increment to subsequent registers

digitalWrite(SS, LOW); // SS must be LOW to communicate
delay(1);
SPI.transfer(dataByte); // request a read, starting at X low byte
byte xL = SPI.transfer(b0); // get the low byte of X data
byte xH = SPI.transfer(b0); // get the high byte of X data
byte yL = SPI.transfer(b0); // get the low byte of Y data
byte yH = SPI.transfer(b0); // get the high byte of Y data
byte zL = SPI.transfer(b0); // get the low byte of Z data
byte zH = SPI.transfer(b0); // get the high byte of Z data
delay(1);
digitalWrite(SS, HIGH);

// shift the high byte left 8 bits and merge the high and low
int xVal = (xL | (xH << 8));
int yVal = (yL | (yH << 8));
int zVal = (zL | (zH << 8));

// scale the values into G's
xAcc = xVal * SCALE;
yAcc = yVal * SCALE;
zAcc = zVal * SCALE;
}

void SPI_SETUP()
{
pinMode(SS, OUTPUT);

// wake up the SPI bus
SPI.begin();

// This device reads MSB first:
SPI.setBitOrder(MSBFIRST);

/*
SPI.setDataMode()
Mode Clock Polarity (CPOL) Clock Phase (CPHA)
SPI_MODE0 0 0
SPI_MODE1 0 1
SPI_MODE2 1 0
SPI_MODE3 1 1
*/
SPI.setDataMode(SPI_MODE0);

/*
SPI.setClockDivider()
sets SPI clock to a fraction of the system clock
Arduino UNO system clock = 16 MHz
Mode SPI Clock
SPI_CLOCK_DIV2 8 MHz
SPI_CLOCK_DIV4 4 MHz
SPI_CLOCK_DIV8 2 MHz
SPI_CLOCK_DIV16 1 MHz
SPI_CLOCK_DIV32 500 Hz
SPI_CLOCK_DIV64 250 Hz
SPI_CLOCK_DIV128 125 Hz
*/

SPI.setClockDivider(SPI_CLOCK_DIV16); // SPI clock 1000Hz
}

void Accelerometer_Setup()
{
// Set up the accelerometer
// write to Control register 1: address 20h
byte addressByte = 0x20;
/* Bits:
PM2 PM1 PM0 DR1 DR0 Zen Yen Xen
PM2PM1PM0: Power mode (001 = Normal Mode)
DR1DR0: Data rate (00=50Hz, 01=100Hz, 10=400Hz, 11=1000Hz)
Zen, Yen, Xen: Z enable, Y enable, X enable
*/
byte ctrlRegByte = 0x37; // 00111111 : normal mode, 1000Hz, xyz enabled

// Send the data for Control Register 1
digitalWrite(SS, LOW);
delay(1);
SPI.transfer(addressByte);
SPI.transfer(ctrlRegByte);
delay(1);
digitalWrite(SS, HIGH);

delay(100);

// write to Control Register 2: address 21h
addressByte = 0x21;
// This register configures high pass filter
ctrlRegByte = 0x00; // High pass filter off

// Send the data for Control Register 2
digitalWrite(SS, LOW);
delay(1);
SPI.transfer(addressByte);
SPI.transfer(ctrlRegByte);
delay(1);
digitalWrite(SS, HIGH);

delay(100);

// Control Register 3 configures Interrupts
// Since I'm not using Interrupts, I'll leave it alone

// write to Control Register 4: address 23h
addressByte = 0x23;
/* Bits:
BDU BLE FS1 FS0 STsign 0 ST SIM
BDU: Block data update (0=continuous update)
BLE: Big/little endian data (0=accel data LSB at LOW address)
FS1FS0: Full-scale selection (00 = +/-6G, 01 = +/-12G, 11 = +/-24G)
STsign: selft-test sign (default 0=plus)
ST: self-test enable (default 0=disabled)
SIM: SPI mode selection(default 0=4 wire interface, 1=3 wire interface)
*/
ctrlRegByte = 0x30; // 00110000 : 24G (full scale)

// Send the data for Control Register 4
digitalWrite(SS, LOW);
delay(1);
SPI.transfer(addressByte);
SPI.transfer(ctrlRegByte);
delay(1);
digitalWrite(SS, HIGH);
}

, but unfortunately I am only getting zeroes. Any help will be greatly appreciated :slight_smile:

The code looks fine to me. How is the chip wired? I assume you have a break-out board of some kind (those Land Grid Array chips are almost impossible to solder wires to).

Amirra:

A few points:

  1. This question should be in the sensors section.

  2. You think wayyyyyy toooo hard. A quick google finds stuff like this...
    Google Code Archive - Long-term storage for Google Code Project Hosting.

  3. NEVER-ever-ever-NEVER re-invent the wheel -- or similar -- you get tired and your brain hurts. DAMHIKT

  4. Hooking up I2C / TWI sensor can be a miserable experience....

  5. I used the BOSCH BMA180 because a very smart feller did a library -- which I modified and updated -- but still he save me hours (perhaps days) of work.

For example...

I used the BMP 085 pressure sensor for the same reason.

Now I get the feeling that you are smarter than me too so I will probably be stealing your library code some day and maybe even asking for changes -- so take this with good humor. :grin:

Another point:

Don't make us go back and find your old posts....

Tell us what Machine (Mega 2560?) you are using and what compiler version... and any other relevant info.

Are you an engineering student?

If you need to change logic levels -- get these... The LIS331 is a 3.3 V device.

I had to use them for some of my I2C sensors.

johnwasser:
The code looks fine to me. How is the chip wired? I assume you have a break-out board of some kind (those Land Grid Array chips are almost impossible to solder wires to).

I am using a 6 pin connector to connect the chip with my Arduino UNO.

WillR:
Tell us what Machine (Mega 2560?) you are using and what compiler version... and any other relevant info.

I am using an Arduino UNO and using Arduino 1.0.1 for programming.

WillR:
Are you an engineering student?

Yes, I am a third year engineering student.

I will try using the library that you showed me.

I think he meant a "circuit diagram" -- the actual wiring connections that you made -- a drawing. .... if you search for BMA180 -- you will find a sketch that someone else made.

Frustrating getting started -- isn't it? Especially when you don't know what it is that you don't know... At least that's how it works for the rest of us... :smiley:

I really do suggest you go through the Sensors Forum...

I really do suggest you go through the Sensors Forum...

Why? This is Project Guidance. I think that covers this thread.

Amirra:

WillR:
Tell us what Machine (Mega 2560?) you are using and what compiler version... and any other relevant info.

I am using an Arduino UNO and using Arduino 1.0.1 for programming.

WillR:
Are you an engineering student?

Yes, I am a third year engineering student.

I will try using the library that you showed me.

OK -- Now which version? :stuck_out_tongue:

All these things are important -- they save time.

I assume it is a latest version... and you used 3.3V to power the sensor... Just like you should.

Engineers NEVER make mistakes right? ...and I have a bridge I could sell you.

I have been doing this since 1968 -- I designed math processors and communications protocols and electronics when I started... whenever I start something new I usually feel helpless, ignorant and out of control. If you feel the same -- get used to it -- it's "condition normal" for good engineers who explore new things. Relax.... :fearful:

dxw00d:

I really do suggest you go through the Sensors Forum…

Why? This is Project Guidance. I think that covers this thread.

Some reasons:

The questions are particular to a specific sensor.

There is a sensors forum

Many of the questions are likely already answered there.

The people who hang out there can probably help FASTER!

Clear?

If she has a project – I have no idea what it might be. Right now the questions are all specific to a sensor and the circuitry.

WillR:
OK -- Now which version? :stuck_out_tongue:

I am using an Arduino UNO R3.

Amirra:

WillR:
OK -- Now which version? :stuck_out_tongue:

I am using an Arduino UNO R3.

Ok -- that makes it easy. The sensor uses 3.3V the Arduuino UNO R3 can supply 3.3 V -- it should work. Assuming that you use a program that works...

From the SPEC -- pg 17 of 37:

  • The registers embedded inside the LIS331HH may be accessed through both the I2C and
    SPI serial interfaces. The latter may be SW configured to operate either in 3-wire or 4-wire
    interface mode.

The serial interfaces are mapped onto the same pads. To select/exploit the I2C interface, CS
line must be tied high (i.e. connected to Vdd_IO).

Now -- in your circuit ( the diagram you never made... :cold_sweat:) did you make that connection?

This is very common in I2C sensors with selectable interfaces.

One last thing...

I hope it is actually the Break Out Board (BOB) you bought.

Soldering those components is another miserable job...