I2C programming in Accelerometer

594 views
Skip to first unread message

Zain Dar

unread,
Nov 16, 2013, 11:25:11 PM11/16/13
to beagl...@googlegroups.com
Hi, I'm working on two accelerometers, both of them are digital (ADXL345 by sparkfun) and I'm having trouble programming them to display the X, Y and Z axis. I'm trying the code in python first because its alot easier to work with and much faster to learn than C++ but I haven't had much luck in finding tutorials that make sense. Derek Molloys tutorial of the same topic is good but it comes with some problems that I'm unable to solve. He requires to install a number of other packages that are temporarily unavailable online for downloading into my BBB so I have to result to alternate ideas and code examples. Can someone please kindly direct me to a sample code that I can try out?
Thanks 

Pedro A. Melendez

unread,
Nov 17, 2013, 2:39:52 AM11/17/13
to beagl...@googlegroups.com
you can use for i2c with python the adafruit library fro the beaglebone or if you like to use nodejs there is node-i2c. I used node-i2c to make a custom driver for the ds1337 rtc and it worked pretty nice.

-Pedro


On Sun, Nov 17, 2013 at 12:25 AM, Zain Dar <zain...@gmail.com> wrote:
Hi, I'm working on two accelerometers, both of them are digital (ADXL345 by sparkfun) and I'm having trouble programming them to display the X, Y and Z axis. I'm trying the code in python first because its alot easier to work with and much faster to learn than C++ but I haven't had much luck in finding tutorials that make sense. Derek Molloys tutorial of the same topic is good but it comes with some problems that I'm unable to solve. He requires to install a number of other packages that are temporarily unavailable online for downloading into my BBB so I have to result to alternate ideas and code examples. Can someone please kindly direct me to a sample code that I can try out?
Thanks 

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Pedro A. Melendez

unread,
Nov 17, 2013, 2:46:01 AM11/17/13
to beagl...@googlegroups.com

Zain Dar

unread,
Nov 17, 2013, 2:53:43 AM11/17/13
to beagl...@googlegroups.com
Thanks for the advice and the links! So, I noticed that the ADXL345 is made in the node-i2c github as a project. There are a number of files in it, so the one that has the code for the working of the accelerometer is the one that is labeled "index.js"? also since I haven't made a code in the .js format, is this done from the terminal or from the cloud9ide? thanks again!


You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/YctTkwyFc8g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.

Pedro A. Melendez

unread,
Nov 17, 2013, 4:48:22 PM11/17/13
to beagl...@googlegroups.com
Same as python, just write 'node scriptname.js'. I'm not sure if you can use run it from cloud9

-Pedro

Zain Dar

unread,
Nov 17, 2013, 5:56:07 PM11/17/13
to beagl...@googlegroups.com
I made the code for the z axis of the accelerometer but i just need some help on how to adjust it for the X and Y axis. Heres the sample code for the z axis. 

import smbus
import time
def main():
bus = smbus.SMBus(1)
 
# Power-on the accelerometer, enabling only the z-axis
bus.write_byte_data(0x18, 0x20, 0x24)
 
try:
while True:
# get z-axis acceleration
tilt = bus.read_byte_data(0x18, 0x2D)
 
# if it's supposed to be negative
if tilt > 127:
# then convert it to the absolute value of the negative value
tilt = 256 + ~tilt
positive = False
else:
positive = True
 
# format and print it
pos = tilt / 2
print str(tilt).rjust(6),
if positive:
print 65*' ' + '|' + (pos*'#').ljust(65)
else:
print (pos*'#').rjust(65) + '|' + 65*' '
# sleep
time.sleep(0.1)
except KeyboardInterrupt:
# restore default settings
bus.write_byte_data(0x18, 0x20, 0x07)
 
if __name__ == '__main__':
main()

I only need an idea for the X and Y axis. Thanks to anyone who can help.

rod0...@gmail.com

unread,
Jul 17, 2014, 2:26:01 PM7/17/14
to beagl...@googlegroups.com
Im here with the same problem did you ever find a solution?
Reply all
Reply to author
Forward
0 new messages