BBBAndroid I2C app (using ADXL345 3-axis accelerometer)

by 14:20 5 comments

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.

Ankur Yadav

Software & Hardware Hacker

I believe in truth, freedom + following the trail of our curiosities. I'm all for learning into life. And then, learning some more.

5 comments:

  1. Hi, Have some queries, I am also trying same thing on begalbone but i am not able to detect the I2c device,
    i am using latest kernel, can you please help me on this one.

    ReplyDelete
    Replies
    1. Hi Tushar,

      I would be happy to help you.
      Can you specify the error message that you are getting?

      Delete
    2. Hi,
      Sorry for late reply.
      I2C device is not able to detect in linux begalbone black, checked with i2cdetect utility on i2c0 and i2c1.
      I am not sure that it is problem with device or some more setting required.
      please help me.
      Thanks :)

      Delete
  2. Hi Ankur ,

    While compiling the sample app using i2c I am facing below issue.

    i2c.o: In function `i2cSetAddress':
    /jni/i2c.c:45: undefined reference to `i2c_smbus_write_byte'
    obj/local/arm64-v8a/objs/BBBAndroidHAL/i2c.o: In function `i2cWriteBytes':
    /jni/i2c.c:70: undefined reference to `i2c_smbus_write_i2c_block_data'
    /obj/local/arm64-v8a/objs/BBBAndroidHAL/i2c.o: In function `i2cReadByte':
    /jni/i2c.c:84: undefined reference to `i2c_smbus_read_byte'
    /obj/local/arm64-v8a/objs/BBBAndroidHAL/i2c.o: In function `i2cReadBytes':
    /jni/i2c.c:94: undefined reference to `i2c_smbus_read_i2c_block_data'
    collect2: error: ld returned 1 exit status
    make: *** [/obj/local/arm64-v8a/libBBBAndroidHAL.so] Error 1

    Can you please help me on resolving this.

    Thanks

    ReplyDelete
    Replies
    1. Hi Satya,

      Please keep include folder in jni folder when try to compile the code [https://github.com/ankurayadav/bbbandroidHAL/tree/master/jni/include].

      It seems that i2c.c is not able to find i2c-dev.h file which is present in include/linux directory.

      Regards

      Delete