![]() |
![]() |
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Jan 1970
Location: Bristol
Posts: 30
|
Hello all,
I want to share what I have learned on how to use the LIS302DL 3-axis accelerometer breakout board. It’s a pretty cool sensor with I2C or SPI interfacing. You can buy it from SparkFun who make the breakout board or from cool components who stock it in the UK. Cool components deliver like lightening if you are in the UK, you could have one of these sensors in your hands and working within 1 working day of ordering and for under Ł25. For anyone who wants to add “tilt sensing” to your projects the LIS302DL is a good choice. I make it sound easy to use, and it is, but for me it was not the case. I must have spent two days reading the datasheet over and over trying to extract useful information, and trawling the web trying to find any information on how to use it. I did not find any PICAXE related information on the unit but I found some AVR and PIC code written in C which I read and reworked into PICAXE code. So hopefully someone will find this information useful and save them some time getting up and running with this little unit. Here is a list of all the documentation that I found useful: Datasheet Breakout Schematic Application notes I2C datasheet
Here is my example code that simply outputs the data to a debug screen. Code:
'************************************************* '**LIS302DL Example Program uisng I2C for PICAXE** '**By Hussam EL-Sheikh (c) 6th July 2009********** '************************************************* symbol who = b0 'who am I symbol dat = b1 'data ready symbol X= w1 symbol Y= w2 symbol Z= w3 symbol TEMP = w4 'for use in calculations symbol sign = b10 'sign byte symbol ii = b11 'counter let ii = 0 let X = 0 let Y = 0 let Z = 0 let sign = %00000000 'This is the sign byte, this is how it works: Bit0 is for the X axis 'Bit1 is for the Y axis 'Bit2 is for the Z axis 'When the respective Bit is = 1 then the reading is negative init: hi2csetup i2cmaster, $39, i2cslow, i2cbyte 'set up the I2C protocol for the LIS302DL hi2cout $20, (%01000111) 'Diable power down and enable X,Y,Z axis pause 10 data_check: hi2cin $27,(dat) 'Check if new data is ready dat = dat | %11110111 'isolate the ZYXDA bit with bit operation pause 10 if dat = 255 then goto read_data goto data_check read_data: hi2cin $0F,(who) 'Read who_am_i register sanity check should equal %00111011 or $3B '************************* hi2cin $29,(TEMP) 'Read X if TEMP > 127 then 'if it's negative remove the sign and raise a marker TEMP = 256 - TEMP TEMP = TEMP*18 X = X + TEMP sign = sign | %00000001 'Bit operation to signal it's negative else TEMP = TEMP*18 X = X + TEMP sign = sign &/ %00000001 'Bit operation to clear negative signal endif '************************** hi2cin $2B,(TEMP) 'Read Y if TEMP > 127 then 'if it's negative remove the sign and raise a marker TEMP = 256 - TEMP TEMP = TEMP*18 Y = Y + TEMP sign = sign | %00000010 'Bit operation to signal it's negative else TEMP = TEMP*18 Y = Y + TEMP sign = sign &/ %00000010 'Bit operation to clear negative signal endif '************************* hi2cin $2D,(TEMP) 'Read Z if TEMP > 127 then 'if it's negative remove the sign and raise a marker TEMP = 256 - TEMP TEMP = TEMP*18 Z = Z + TEMP sign = sign | %00000100 'Bit operation to signal it's negative else TEMP = TEMP*18 Z = Z + TEMP sign = sign &/ %00000100 'Bit operation to clear negative signal endif ii = ii+1 '*************************** if ii = 10 then 'Take an average over 10 cycles ii = 0 X = X/10 Y = Y/10 Z = Z/10 debug X = 0 Y = 0 Z = 0 endif goto data_check I hope this helps someone out. It really is very easy to use and with PICAXE's built in I2C procedures it's a perfect match. Huss Last edited by hussy11 : 06-07-2009 at 19:00. Reason: make the title clearer |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Jan 1970
Posts: 574
|
Many thanks for sharing this useful information. There are lots of smart sensor being introduced at the moment but they often require quite a bit of interface software development work.
|
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jan 1970
Location: Uppsala, Sweden
Posts: 378
|
Hello Hussy,
I also am very grateful for the detailed and diverse information you provided. Based on your information I have decided that I will use the particular device and some parts of your code in a water rocket project (acceleration logging for velocity and heigth calculation) Thanks! /Jurjen |
|
|
|
|
|
#4 |
|
Member
Join Date: Jan 1970
Posts: 50
|
Thanks for all the hard work there.
I'm not one for hard work, so I've been using the board that Pololu have built around the MMA7260QT 3-Axis accelerometer (http://www.pololu.com/catalog/product/766). It just plugs straight in to power, ground, and 3 ADCs for X, Y, & Z. I've found the output to be noisy, but good enough for a tilt switch. I've been chucking the readings into a buffer and averaging to get around this. |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Jan 1970
Posts: 574
|
This board looks pretty much the same as the SparkFun offering. Btw, if your only interested in tilt you could add another series resistor (4.7K max) and 0.1uF capacitor on each output to increase low-pass filtering.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|