ADXL345 Accelerometer using I2C


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2005
    Posts
    24

    Default ADXL345 Accelerometer using I2C

    Hi,
    I am currently very unsuccessful at communicating via I2C to an ADXL345 Accelerometer. The code and circuit appear to both very simple, but the response I get back makes no sense. Here are the details:
    The ADXL345 CS is set high (3.3v) to put the device into I2C mode.
    The ADXL345 Alt Address is set low (0v)
    I have pull-up resistors on the Clock and SDA lines.

    The chip is a PIC18F26K22 running at +3.3v

    Below is the I2C code:
    Code:
    I2Cread ADXL345_SDA,ADXL345_SCL, $A7, $00, [X0]   ' Read Device ID
    serout2 RS232_TX, 6, ["ID: ",bin8 X0,13,10]                ' The result should be 11100101 but is 00010000 like all other read results.
    
    I2CWRITE ADXL345_SDA,ADXL345_SCL, $A6, $2D, [8] ' Put into Run Mode
    pauseus 5
    
    Main_I2C:
       I2Cread ADXL345_SDA,ADXL345_SCL, $A7, $32, [X0,X1,Y0,Y1,Z0,Z1] ' Read accelerometer data
       serout2 RS232_TX, 6, ["X: ",dec X0,",",dec X1]
       serout2 RS232_TX, 6, ["  Y: ",dec Y0,",",dec Y1]
       serout2 RS232_TX, 6, ["  Z: ",dec Z0,",",dec Z1,13,10]
       pause 100
    Goto Main_I2C
    When reading back any byte the answer is always 16 or 00010000, which implies that I have something fundamentally configured wrong.

    Any suggestions is greatly appreciated.

    Terry

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: ADXL345 Accelerometer using I2C

    try $a6 for the i2cread address as well as the write address , pbp will set bit.0 of the address as required

  3. #3
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: ADXL345 Accelerometer using I2C

    It looks good to me. You need to check your connections and scope it and see if you're actually talking to it.
    See post# 8 where the I2CREAD was used same as yours, just different address.
    Louie

  4. #4
    Join Date
    Feb 2005
    Location
    brookfield, ct, usa
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: ADXL345 Accelerometer using I2C

    There was a recent thread that explained that you have to shift the seven bit address left, and let PBP insert bit 0 for the I2Cread or I2C write command. Using both a barometer chip and a humidity chip I made the address a constant, then made a variable that shifted the constant left one bit, and used that variable as the address.

    HTU21D_i2c_address con $40
    HTU21D_i2c_address1 var byte
    HTU21D_i2c_address1 = HTU21D_i2c_address <<1
    i2cwrite SDA, SCL, HTU21D_i2c_address1, [trig_temp]
    pause 10
    i2cread SDA, SCL, HTU21D_i2c_address1, [temp]
    pause 10
    i2cwrite SDA, SCL, HTU21D_i2c_address1, [trig_RH]
    pause 10
    i2cread SDA, SCL, HTU21D_i2c_address1, [RH]

    try something like this using the $1D address as the constant, shift it once, and let PBP add bit 0

  5. #5
    Join Date
    Dec 2005
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: ADXL345 Accelerometer using I2C

    Thanks for everyone's input.
    The main issue was Louie's point, the ADXL345 is a QFN part and I had a bad solder joint.
    From the ADXL345 data sheet, the read/write addresses are given as $A6 and $A7 which do take into account the read/write bit and it is this value used in the I2C PBP code. I added a label to the I2C commands which allows a jump if there is an I2C communication error talking to the device. I added this label for all commands sent whether read or write, this error occurred for every command which directed me to investigating the hardware. Adding the label was a very valuable diagnostic tool.

    Below is my working code for the ADXL345, much more work is still needed to get the device configured for my application and get the raw counts into meaningful values. Overall the PBP I2C commands work great and are simple to use.

    Code:
    X0 var byte
    X1 var byte
    Y0 var byte
    Y1 var byte
    Z0 var byte
    Z1 var byte
    ID       var byte
    Mode     var byte
    
    high ADXL345_CS ' Set high to put unit into I2C mode
    pause 1         ' Added to insure no R/W issue
    low ADXL345_SDO ' Set low to put unit addresses to $A6, $A7
    pause 1         ' Added to insure no R/W issue
    
    serout2 RS232_TX, 6, [13,13,10,"Start of Test Program. "]
    
    'Initialize ADXL345
    I2Cread ADXL345_SDA,ADXL345_SCL, $A7,$00, [ID], I2C_error 'read Device ID
    serout2 RS232_TX, 6, ["Device ID: $", hex2 ID,13,10]   ' Value should be $E5
    
    I2CWRITE ADXL345_SDA,ADXL345_SCL, $A6, $31, [$0B], I2C_error  '+/-16g, 13-bit mode
    pauseus 5
    I2CWRITE ADXL345_SDA,ADXL345_SCL, $A6, $2D, [$08], I2C_error  'Start Measurement or put into Run Mode
    pauseus 5
    
    I2Cread ADXL345_SDA,ADXL345_SCL, $A7, $2D, [Mode],I2C_error  ' Confirm mode setting
    serout2 RS232_TX, 6, ["Mode: ",hex Mode,13,10]     ' value should be 8
    pauseus 5
    
    Main_I2C:  ' Main Loop
       I2Cread ADXL345_SDA,ADXL345_SCL, $A7, $32, [X0,X1,Y0,Y1,Z0,Z1], I2C_error
       serout2 RS232_TX, 6, ["X,Y,Z, ", Sdec 256*X1+X0,", ",Sdec 256*Y1+Y0,", ",Sdec 256*Z1+Z0,13,10]
       pause 100
    Goto Main_I2C
    
    I2C_Error:
        serout2 RS232_TX, 6, ["I2C Comm Error",13,10]
    stop

Similar Threads

  1. PIC16F88 I2C Accelerometer Help
    By dkibel in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 7th May 2015, 13:49
  2. I2C MMA8452 Accelerometer
    By emf985 in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 15th January 2015, 17:28
  3. accelerometer question
    By griffin in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 27th February 2009, 21:00
  4. Accelerometer Sensor
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 27th August 2008, 13:26
  5. Adxl213ae Accelerometer
    By DougB in forum mel PIC BASIC
    Replies: 0
    Last Post: - 25th July 2005, 21:32

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts