ADXL345 Accelerometer


50 posts   Page 2 of 2   1, 2
by garym1957 » Fri Nov 02, 2012 3:10 pm
Hey all.
I’m still trying to get (understand, grasp, comprehend) what I need to do to get the RPi to read data from the ADXL34 3-axis accelerometer. I would be happy if i could call some command line function to get a reading or await interrupts or something.

I’m not smart enough to figure out the few examples I’ve found.

Does someone have a COMPLETE, idiot’s guide to making this happen?

I have no idea if I should use SPI or I2C and there appear to be no wiring schematics in any but one example.

So please, anyone… the world needs a COMPLETE solution for stupid people like me.

0. What am I to expect? is this a command line program or just a library example?? Is this a logging function or will I see something on my screen when I tap the module? People love to post step-by-step instructions for things without mentioning what the end result will be.

1. Wiring diagram – seriously – Its not even funny, how many “examples” are out there where the schematic was considered unnecessary.

2. What (libraries, programs) do I need installed for the way its wired?

3. What do I do if something doesn’t work?

4. Perhaps EXPLAIN a little about why each of the previous steps was performed. Not everyone knows linux that well.

Thank you very much
Posts: 11
Joined: Fri Aug 24, 2012 6:04 am
by garym1957 » Fri Nov 02, 2012 3:24 pm
Hey all.

Anyone got this to work? Anyone have a CANNED, WORKING example they can share? I'm not smart enough to do this myself.

I’m still trying to get (understand, grasp, comprehend) what I need to do to get the RPi to read data from the ADXL34 3-axis accelerometer. I would be happy if i could call some command line function to get a reading or await interrupts or something.

Does someone have a COMPLETE, idiot’s guide to making this happen?

I have no idea if I should use SPI or I2C and there appear to be no wiring schematics in any but one example. I've mentioned a photograph of another person's wiring, but there are no connections for CS or anything... just from the PI to the Accel board.

So please, anyone… the world needs a COMPLETE solution for stupid people like me.

Wish list:
0. What am I to expect? is this a command line program or just a library example?? Is this a logging function or will I see something on my screen when I tap the module?

1. Wiring diagram – seriously – Its not even funny, how many “examples” are out there where the schematic was thought of as unnecessary.

2. What do I need installed for the way its wired?

3. What do I do if something doesn’t work?

4. Perhaps EXPLAIN a little about why each of the previous steps was performed. Not everyone knows linux that well.

Thank you very much
Posts: 11
Joined: Fri Aug 24, 2012 6:04 am
by joan » Fri Nov 02, 2012 3:36 pm
The ADXL345 is a chip manufactured in many forms. It is installed in many different boards with different names. These may all have unique interfacing requirements.

What did you buy?
User avatar
Posts: 5672
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by garym1957 » Fri Nov 02, 2012 9:02 pm
Its a Chinese breakout board from eBay.
All pins come directly out to headers. Among a half-dozen manufacturers, I have seen this to be the same in every case.

There is a LDO regulator chip and jumper for those who want to use either 5V or 3.3V for power.

I can run
i2cdetect -y 0
and it returns hex 53. I can disconnect any of the data wires and it returns nothing, so I know my Pi is seeing the board.
Posts: 11
Joined: Fri Aug 24, 2012 6:04 am
by Thoric » Thu Jun 20, 2013 8:20 pm
Hi all,

I'm new to RPi and C coding, but as a personal project I got hold of a RPi and an ADXL345 on a breakout board. I followed these instructions to set up I2C comms on the RPi (http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c), and when I entered "sudo i2cdetect -y 1" I got a response from the accelerometer on address x53.

Then, after trying to compile the code in this thread (see above code segment from joan), I now no longer get a response to i2cdetect command.

Has this code somehow changed a setting/parameter or something that has stopped me from communicating with the ADXL? I'm new to C so I'm not sure what the code truly does, so could it have set a parameter on the chip that needs undoing? I hope somebody can help me diagnose my problem.

Thanks in advance,

Thoric
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by joan » Thu Jun 20, 2013 8:59 pm
Thoric wrote:Hi all,

I'm new to RPi and C coding, but as a personal project I got hold of a RPi and an ADXL345 on a breakout board. I followed these instructions to set up I2C comms on the RPi (http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c), and when I entered "sudo i2cdetect -y 1" I got a response from the accelerometer on address x53.

Then, after trying to compile the code in this thread (see above code segment from joan), I now no longer get a response to i2cdetect command.

Has this code somehow changed a setting/parameter or something that has stopped me from communicating with the ADXL? I'm new to C so I'm not sure what the code truly does, so could it have set a parameter on the chip that needs undoing? I hope somebody can help me diagnose my problem.

Thanks in advance,

Thoric

The code was written before the new boards existed. I think i2c buses 0 and 1 have been swapped.

In the code change "/dev/i2c-0" to "/dev/i2c-1".

Doesn't explain your symptoms though.
User avatar
Posts: 5672
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Thoric » Thu Jun 20, 2013 9:19 pm
joan wrote:
Thoric wrote:Hi all,

I'm new to RPi and C coding, but as a personal project I got hold of a RPi and an ADXL345 on a breakout board. I followed these instructions to set up I2C comms on the RPi (http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c), and when I entered "sudo i2cdetect -y 1" I got a response from the accelerometer on address x53.

Then, after trying to compile the code in this thread (see above code segment from joan), I now no longer get a response to i2cdetect command.

Has this code somehow changed a setting/parameter or something that has stopped me from communicating with the ADXL? I'm new to C so I'm not sure what the code truly does, so could it have set a parameter on the chip that needs undoing? I hope somebody can help me diagnose my problem.

Thanks in advance,

Thoric

The code was written before the new boards existed. I think i2c buses 0 and 1 have been swapped.

In the code change "/dev/i2c-0" to "/dev/i2c-1".

Doesn't explain your symptoms though.


Hi joan, thanks for the quick reply. OK, so the change will hopefully get the c code working. But we're none the wiser as to why my ADXL345 has stopped responding. I'm about to reflash my RPi SD card with a new OS, because I'm desperate concerned that I've screwed up some driver/setting or something and as I have no idea what I see this as a guarantee of undoing it :shock:
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by joan » Thu Jun 20, 2013 9:26 pm
Reboot the Pi and see if you can then see the card with i2cdetect. Software rarely damages hardware. If it works try the modified software.
User avatar
Posts: 5672
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Thoric » Thu Jun 20, 2013 9:56 pm
joan wrote:Reboot the Pi and see if you can then see the card with i2cdetect. Software rarely damages hardware. If it works try the modified software.


Hi joan. I'd already tried rebooting, but no luck. I've now downloaded and installed Wheezy as my RPi OS, setup i2c tools and again I get no response when I call i2cdetect :(

Can we only conclude that the chip is dead?

Something that probably ought to be mentioned: When I first connected the ADXL345 I hadn't remembered to wire the ALT ADDRESS pin. When grounded, this sets the address to x53. When set High, the address is 1D. When I first connected the address was showing in i2cdetect as x53. Now nothing. Is it possible that powering the chip with this ALT ADDRESS pin unwired has caused some problem? It makes no difference now if I wire it to ground or 3.3V, I don't get a response either way :(
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by Thoric » Thu Jun 20, 2013 10:39 pm
I tried probing the connections, and I have a solid 3.27V on the supply so I don't think the chip is under-powered. I've considered wiring it to the 5V line, which I'm pretty sure I've read someone else do somewhere without detrimental effects, but I don't want to put the chip under unnecessary risk just to try a theory out.

I literally have no idea what's wrong :(
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by CopterRichie » Thu Jun 20, 2013 10:46 pm
Thoric wrote:I tried probing the connections, and I have a solid 3.27V on the supply so I don't think the chip is under-powered. I've considered wiring it to the 5V line, which I'm pretty sure I've read someone else do somewhere without detrimental effects, but I don't want to put the chip under unnecessary risk just to try a theory out.

I literally have no idea what's wrong :(


Is there a voltage regulator on the breakout board? If so, it may be bad. Before connecting the board to five volts, make sure there is a regulator in place and that it is working.
User avatar
Posts: 129
Joined: Tue Mar 26, 2013 3:14 am
Location: Los Angeles CA.
by Thoric » Thu Jun 20, 2013 10:55 pm
CopterRichie wrote:
Thoric wrote:I tried probing the connections, and I have a solid 3.27V on the supply so I don't think the chip is under-powered. I've considered wiring it to the 5V line, which I'm pretty sure I've read someone else do somewhere without detrimental effects, but I don't want to put the chip under unnecessary risk just to try a theory out.

I literally have no idea what's wrong :(


Is there a voltage regulator on the breakout board? If so, it may be bad. Before connecting the board to five volts, make sure there is a regulator in place and that it is working.


I don't believe there's a voltage regulator on the breakout board. It's a Sparkfun (https://www.sparkfun.com/products/9836), which only has a couple of capacitors for noise suppression (or something like that -I'm no expert).

Any other thoughts?
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by CopterRichie » Thu Jun 20, 2013 11:48 pm
Thoric wrote:I don't believe there's a voltage regulator on the breakout board. It's a Sparkfun (https://www.sparkfun.com/products/9836), which only has a couple of capacitors for noise suppression (or something like that -I'm no expert).

Any other thoughts?


Well, according to the information on the page "2.0-3.6VDC Supply Voltage", you CAN NOT use Five volts. I have just started learning about the Pi so I am not sure how the I2C bus work however with the Arduino, Pull-up Resistors or a logic lever are required. maybe someone can ring in on this one.

http://www.robotshop.com/sfe-logic-level-converter.html
User avatar
Posts: 129
Joined: Tue Mar 26, 2013 3:14 am
Location: Los Angeles CA.
by joan » Fri Jun 21, 2013 7:35 am
@Thoric
Having the alt line connected or disconnected wouldn't harm the ADXL345 .
Configuration settings can't harm the ADXL345 either (they don't survive a shut-down).

How are you connecting between the Pi and the breakout board?
User avatar
Posts: 5672
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Thoric » Fri Jun 21, 2013 8:15 am
joan wrote:@Thoric
Having the alt line connected or disconnected wouldn't harm the ADXL345 .
Configuration settings can't harm the ADXL345 either (they don't survive a shut-down).

How are you connecting between the Pi and the breakout board?


Hi joan, my connections are pretty simple:

Rpi GPIO | ADXL345
GND | GND
3V3 | VCC
3V3 | CS
SDA | SDA
SCL | SCL

And originally the ALT ADDRESS line (SDO) was floating when it reponded, and since it stopped responding I've tried floating/GND/3V3 - all without change.

I read somewhere that the RPi has the pull-up resistors onboard required for the I2C comms (viewtopic.php?f=44&t=46253&p=364976&hilit=raspberry+pi+pullup+resistors#p364976) so maybe these are a problem?

I'm more than willing to create a high-low logic interface board and use the 5V supply, but I'd need some guidance on how to design that correctly.
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by joan » Fri Jun 21, 2013 8:20 am
The connections appear fine. Have you soldered header pins on to the breakout board? Perhaps there is a short or a bad joint.
User avatar
Posts: 5672
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Thoric » Fri Jun 21, 2013 9:01 am
joan wrote:The connections appear fine. Have you soldered header pins on to the breakout board? Perhaps there is a short or a bad joint.


Everything posted yesterday was from home, where I was trying to develop all this. Today I am at the office and out of curiosity I've set it all up, called "i2cdetect -y 1" and the ADXL is responding again on address 0x53!

However, when I run your code segment joan I get "Failed to open i2c bus".
In the code I replaced "/dev/i2c-0" with "/dev/i2c-1", and I've made sure the ADXL345_I2C_ADDR is set to 0x53.
I used "gcc -o test1 test1.c" to compile it under Wheezy.

Any thoughts on what I'm missing?
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by Thoric » Fri Jun 21, 2013 9:06 am
joan wrote:The connections appear fine. Have you soldered header pins on to the breakout board? Perhaps there is a short or a bad joint.


Sorry - I didn't answer your question. I'm using the GPIO breakout from Hobbytronics with a 6 inch ribbon cable (http://www.hobbytronics.co.uk/raspberry ... rry%20gpio)

To connect them I have jumper leads that slot directly onto the pins. To check all is OK I buzzed out the lines yesterday with my multimeter and there were no shorts or bad connections (that I could identify).

Given that it's working today again, I wonder if it is a bad connection somewhere? Or perhaps a supply problem?

Anyway - whilst it's working I'd love to get the C code to talk with it if possible. That would be just great! How can I best go about diagnosing the failure to open the I2C bus? :)
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by joan » Fri Jun 21, 2013 9:10 am
That may be a permissions issue. As a quick check you could try

sudo ./test1

As to the on/off working that suggests a loose connection.

You will need to solder some header pins on for a reliable connection (especially for an ADXL345 which is in effect a shock sensor).
User avatar
Posts: 5672
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Thoric » Fri Jun 21, 2013 9:20 am
joan wrote:That may be a permissions issue. As a quick check you could try

sudo ./test1

As to the on/off working that suggests a loose connection.

You will need to solder some header pins on for a reliable connection (especially for an ADXL345 which is in effect a shock sensor).


Hurray! Thank you!! It's all working! This is great stuff, you're a star! The above shows my inexperience, not thinking to use sudo to launch the executable :oops:

I now have a scrolling list of X Y and Z readings from the ADXL345, this is great :D

So.... how do I stop the code from running then?? :lol:
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by Thoric » Fri Jun 21, 2013 9:35 am
Nevermind - CTRL+C stopped it (I could have sworn I tried that!)
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by joan » Fri Jun 21, 2013 10:22 am
Have a look at the properties of the i2c devices.

ls -l /dev/i2c*

Mine shows
Code: Select all
crw-rw---T 1 root i2c  89, 0 Jun 19 22:17 /dev/i2c-0
crw-rw---T 1 root i2c  89, 1 Jun 19 22:17 /dev/i2c-1


The "proper" usage (rather than using sudo) is to add yourself to the i2c group, e.g. if your user name is pi then

sudo adduser pi i2c

will allow you to use the i2c devices without needing sudo.

Note, changes in user permissions don't take effect until you log out and back in.
User avatar
Posts: 5672
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by Thoric » Fri Jun 21, 2013 10:46 am
Thank you joan - I'll look at this tonight when I get another chance to work on the RPi.

You're a credit to this forum! :D
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by Thoric » Fri Jun 21, 2013 9:09 pm
joan wrote:
The "proper" usage (rather than using sudo) is to add yourself to the i2c group, e.g. if your user name is pi then

sudo adduser pi i2c

will allow you to use the i2c devices without needing sudo.

Note, changes in user permissions don't take effect until you log out and back in.


Hi again Joan, this worked great, thanks!

I'm now looking at grabbing images from my cheap webcam in c (there is a plan, ultimately).
So far I've shown my webcam works using guvcview, so I'm looking for ways to grab images in code now. I came across openCV mentioned in a few places, but there appears to be no "sudo apt-get" method of getting it. Instead I have this tarball and no idea what to do with it. Any clues how I go about making/compiling/installing this openCV?
Posts: 20
Joined: Sun May 12, 2013 8:04 pm
by DrTim » Fri Mar 07, 2014 3:11 pm
I wanted to connect the GY-291 to my Ra Pi and read the xyz gravity data in Python. I searched the internet and could not find a working Python solution so I coded it myself...
Python does not support signed ints so I had to do a bit of math to interpret the output from the GY-291 as the DATAx1 outputs 125, 126, 127, 0, 1, 2, 3 where x represents X,Y,Z
You have to have installed the SPI header and the GPIO too
Ra Pi to GY-291 board connections:
board Ra Pi
--------------------
GND 0V
Vcc 3v3 (on Ra Pi)
CS P24 (on RaPi)
INT1 N/C
INT2 N/C
SDO MISO (on Ra Pi)
SDA MSIO (on Ra Pi)
SCL SCLK (on Ra Pi)
also
47nF Ceramic cap between SDA and GND

Python code
============
Code: Select all
import spidev
import time
import math
import RPi.GPIO as GPIO

spi = spidev.SpiDev()
spi.open(0,1)  #spi port, device (cs)

def setupSPI():
    spi.open(0, 1)
    spi.max_speed_hz = 5000
    spi.mode = 2
    spi.bits_per_word = 8
    spi.threewire = False
    spi.cshigh= False
    spi.lsbfirst = False
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(18, GPIO.OUT)
    #Initialize the device 
    Write(0x2D, 0)
    Write(0x2D, 16)
    Write(0x2D, 8)# write data out to the SPI peripheral start conversations#,Write(0x2D, 15)  #Wakeup

    Write(0x2C, 14)# set datarate to 800hz BW 400 Hz rate code 1101

    Write(0x31, 0)
    Write(0x31, 15)# 13bit += 16g write to set justify msb the data out to the spi peripheral

def CloseSPI():
    spi.close()

def Write(add, char):
    GPIO.output(18,False)
    spi.xfer2([add, char])
    GPIO.output(18,True)

def Read(add):
    GPIO.output(18,False)
    spi.writebytes([add+128])
    f = spi.readbytes(1)
    GPIO.output(18,True)
    return f

def ReadN(add, n):
    GPIO.output(18,False)
    spi.writebytes([add+128+64])
    f= spi.readbytes(n)
    GPIO.output(18,True)
    return f

setupSPI()
GPIO.setup(18, GPIO.OUT)

while(1):
    buf = ReadN(50,6)
    if (int(buf[1]>64)):
        xsum= (-128+buf[1])*256
    else:
        xsum= buf[1]*256
    x= float(xsum + int(buf[0]))/800
       
    if (int(buf[3]>64)):
        ysum= (-128+buf[3])*256
    else:
        ysum= buf[3]*256
    y= float(ysum + int(buf[2]))/800

    if (int(buf[5]>64)):
        zsum= (-128+buf[5])*256
    else:
        zsum= buf[5]*256
    z= float(zsum + int(buf[0]))/800
   
    print (x, y, z , math.sqrt(x*x + y*y +z*z))
    time.sleep(0.5)

output
=====
(-0.03, -0.15, -0.99, 1.00174847142384)
(0.16, -0.18, -1.12, 1.145600279329575)
(0.16, -0.32, -1.12, 1.1757550765359255)
(0.16, -0.3, -1.12, 1.1704699910719625)
(0.16, -0.32, -1.12, 1.1757550765359255)
(0.16, -0.32, -1.12, 1.1757550765359255)
(-0.03, -0.16, -0.99, 1.0032945728947207)
(-0.04, -0.16, -1.0, 1.0135087567455943)
(0.16, -0.17, -1.12, 1.144071676076285)
(0.16, -0.18, -1.12, 1.145600279329575)
(0.16, -0.32, -1.12, 1.1757550765359255)
(0.16, -0.32, -1.12, 1.1757550765359255)
(0.16, -0.29, -1.12, 1.167946916601949)
(-0.01, -0.15, -0.97, 0.981580358401695)
(-0.03, -0.01, -0.99, 0.9905049217444606)
(-0.02, -0.01, -0.98, 0.9802550688468792)
(0.16, -0.31, -1.12, 1.1730728877610292)
(-0.04, -0.15, -1.0, 1.0119782606360672)
(-0.02, -0.16, -0.98, 0.9931767214348108)
(0.16, -0.31, -1.12, 1.1730728877610292)
(-0.03, -0.01, -0.99, 0.9905049217444606)
Posts: 1
Joined: Fri Mar 07, 2014 2:45 pm