Showing posts with label I2C. Show all posts
Showing posts with label I2C. Show all posts

ADXL345 3-axis accelerometer interfacing using I2C bus


  • Connecting BeagleBone Black with ADXL345
 


  • How to use I2C for accessing ADXL345 sensor.
    • Api for accessing all the peripherals of BeagleBoneBlack with Android OS can be found here
    • Api for I2C is in i2c.c
  • Brief description about I2C funtions.
    • i2cOpenAdaptor(uint8_t adaptorNumber): This function I2C adapter number as input and returns a file descriptor for accessing that I2C adapter.
    • i2cSetSlave(int i2cFD, uint8_t address): This takes I2C adapter file descriptor and address at which device is attached as input and sets slave for file descriptor using ioctl call.
    • i2cSetAddress(int i2cFD, unsigned char add): This is used by some other functions internally for random access of sensor registers. It takes file descriptor and random address to which file descriptor should point.
    • i2cWriteByte(int i2cFD, unsigned char add, unsigned char byte): This function is used to write byte provided in the argument at the address provided.
    • i2cWriteBytes(int i2cFD, unsigned char add, int length, uint8_t *bytes): This function is used to write bytes provided in argument from the address provided to address+length continuously.
    • i2cReadByte(int i2cFD, unsigned char add): It reads a byte from the address provided and returns the byte.
    • i2cReadBytes(int i2cFD, unsigned char add, int length, uint8_t *buff): It is used to get 'length' number of bytes in 'buff' array from 'add' starting address.
    • i2cClose(int i2cFD): For closing file descriptor.
  • Source code of Android app using this api can be found here.
  • Steps for making Android app for accessing any I2C device on BeagleBoneBlack
    • Step 1: Copy i2c.c from here and put it into jni folder in you Android app directory.
    • Step 2: Make a jni_wrapper.c for your i2c funtions.
    • Step 3: Make suitable Android.mk file.
    • Step 4: Compile your program using ndk_build. (call ndk_build from current jni directory).
    • Step 5: Declare suitable native function prototypes in your java file like this.
    • Step 6: Include static library in java program like this.
    • Step 7: Now you can easily call general purpose I2C functions from your java program and easily access I2C devices.
  • Steps for configuring ADXL345 sensor for using it through I2C bus. Details regarding how to configure this sensor can be found on this website (This site assumes predefined macros like ADXL345_POWER_CTL address of these macros can be found put here).
    • Step 1: Enable measurement mode in POWER_CTL register like this.
    • Step 2: Set the data format of measurements like this.
    • Step 3: Now we can read sensor values like this.
  • Screenshot of Android app reading ADXL345 sensor values when button is clicked.


  • Video demonstration of I2C Android app.