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
Re: ADXL345 Accelerometer
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
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
Re: ADXL345 Accelerometer
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?
What did you buy?
Re: ADXL345 Accelerometer
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.
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.
Re: ADXL345 Accelerometer
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-ras ... guring-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
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-ras ... guring-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
Re: ADXL345 Accelerometer
The code was written before the new boards existed. I think i2c buses 0 and 1 have been swapped.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-ras ... guring-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
In the code change "/dev/i2c-0" to "/dev/i2c-1".
Doesn't explain your symptoms though.
Re: ADXL345 Accelerometer
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 itjoan wrote:The code was written before the new boards existed. I think i2c buses 0 and 1 have been swapped.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-ras ... guring-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
In the code change "/dev/i2c-0" to "/dev/i2c-1".
Doesn't explain your symptoms though.

Re: ADXL345 Accelerometer
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.
Re: ADXL345 Accelerometer
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 i2cdetectjoan 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.

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

Re: ADXL345 Accelerometer
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
I literally have no idea what's wrong

- CopterRichie
- Posts: 131
- Joined: Tue Mar 26, 2013 3:14 am
- Location: Los Angeles CA.
Re: ADXL345 Accelerometer
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.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
Re: ADXL345 Accelerometer
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).CopterRichie wrote: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.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
Any other thoughts?
- CopterRichie
- Posts: 131
- Joined: Tue Mar 26, 2013 3:14 am
- Location: Los Angeles CA.
Re: ADXL345 Accelerometer
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.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?
http://www.robotshop.com/sfe-logic-level-converter.html
Re: ADXL345 Accelerometer
@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?
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?
Re: ADXL345 Accelerometer
Hi joan, my connections are pretty simple: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?
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 (http://www.raspberrypi.org/phpBB3/viewt ... rs#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.
Re: ADXL345 Accelerometer
The connections appear fine. Have you soldered header pins on to the breakout board? Perhaps there is a short or a bad joint.
Re: ADXL345 Accelerometer
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!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.
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?
Re: ADXL345 Accelerometer
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)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.
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?

Re: ADXL345 Accelerometer
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).
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).
Re: ADXL345 Accelerometer
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 executablejoan 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).

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

So.... how do I stop the code from running then??

Re: ADXL345 Accelerometer
Nevermind - CTRL+C stopped it (I could have sworn I tried that!)
Re: ADXL345 Accelerometer
Have a look at the properties of the i2c devices.
ls -l /dev/i2c*
Mine shows
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.
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
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.
Re: ADXL345 Accelerometer
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!
You're a credit to this forum!

Re: ADXL345 Accelerometer
Hi again Joan, this worked great, thanks!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.
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?
Re: ADXL345 Accelerometer
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
============
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)
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)
=====
(-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)