Program to read from ADXL345 sensor
Connect the ADXL345 module to BeagleBone as shown in the diagram. Type the following program in Cloud9, save it as ADXL345.py
and run it. You should see a three-axis coordinates list printed after every 2 seconds. If you move the sensor, you will see a change in coordinates.
from Adafruit_I2C import Adafruit_I2C import time ADXL345_I2C_ID = 0x53 # I2C bus id ADXL345_REG_POWER_CTL = 0x2D # Power-saving control ADXL345_REG_DATAX0 = 0x32 # X-axis data 0 accel = Adafruit_I2C(ADXL345_I2C_ID,debug=False) accel.write8(ADXL345_REG_POWER_CTL, 0x08) while True: raw = accel.readList(ADXL345_REG_DATAX0, 6) result = [] for i in range(0, 6, 2): g = raw[i] | (raw[i+1] << 8) if g > 32767: g -= 65536 result.append(g) print "result = " + str(result) time.sleep(2)
Explanation
I2C implementation in the Adafruit_BBIO
library is a simple Python file. It has a class named Adafruit_I2C
and a few functions...