+ Post New Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 38
  1. #1
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Am totally new to electronics and the datasheet is very confusing to me, so can anyone please guide me through the specification of the accelerometer? So that i can have a better understanding on the accelerometer and to be able to convert the accelerometer data into G.

    X-axis Y-axis Z-axis -28 -26 218 These are the readings i get from the accelerometer with a +-2g range

    This is the datasheet for the ADXL345 digital accelerometer [1]: https://www.analog.com/media/en/tech...ts/ADXL345.pdf

    HTML Code:
    import smbus
    import time
    
    # Get I2C bus
    bus = smbus.SMBus(1)
    
    # ADXL345 address, 0x53(83)
    # Select bandwidth rate register, 0x2C(44)
    #       0x0A(10)    Normal mode, Output data rate = 100 Hz
    bus.write_byte_data(0x53, 0x2C, 0x0A)
    # ADXL345 address, 0x53(83)
    # Select power control register, 0x2D(45)
    #       0x08(08)    Auto Sleep disable
    bus.write_byte_data(0x53, 0x2D, 0x08)
    # ADXL345 address, 0x53(83)
    # Select data format register, 0x31(49)
    #       0x08(08)    Self test disabled, 4-wire interface
    #                   Full resolution, Range = +/-2g
    bus.write_byte_data(0x53, 0x31, 0x08)
    
    time.sleep(0.5)
    
    # ADXL345 address, 0x53(83)
    # Read data back from 0x32(50), 2 bytes
    # X-Axis LSB, X-Axis MSB
    data0 = bus.read_byte_data(0x53, 0x32)
    data1 = bus.read_byte_data(0x53, 0x33)
    
    # Convert the data to 10-bits
    xAccl = ((data1 & 0x03) * 256) + data0
    if xAccl > 511 :
        xAccl -= 1024
    
    # ADXL345 address, 0x53(83)
    # Read data back from 0x34(52), 2 bytes
    # Y-Axis LSB, Y-Axis MSB
    data0 = bus.read_byte_data(0x53, 0x34)
    data1 = bus.read_byte_data(0x53, 0x35)
    
    # Convert the data to 10-bits
    yAccl = ((data1 & 0x03) * 256) + data0
    if yAccl > 511 :
        yAccl -= 1024
    
    # ADXL345 address, 0x53(83)
    # Read data back from 0x36(54), 2 bytes
    # Z-Axis LSB, Z-Axis MSB
    data0 = bus.read_byte_data(0x53, 0x36)
    data1 = bus.read_byte_data(0x53, 0x37)
    
    # Convert the data to 10-bits
    zAccl = ((data1 & 0x03) * 256) + data0
    if zAccl > 511 :
        zAccl -= 1024
    
    # Output data to screen
    print ("Acceleration in X-Axis : %d" %xAccl)
    print ("Acceleration in Y-Axis : %d" %yAccl)
    print ("Acceleration in Z-Axis : %d" %zAccl)
    These are the code.

  2. #2
    Advanced Member level 5
    Points: 12,423, Level: 26
    Achievements:
    7 years registered

    Join Date
    Apr 2010
    Posts
    1,889
    Helped
    405 / 405
    Points
    12,423
    Level
    26

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    in the +/- 2g range , the resolution is 10bits.
    The value per bit is 3.9mg.(3.90625mg exactly).

    multiply your axis reading with this constant to get g value.



    •   AltAdvertisment

        
       

  3. #3
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Quote Originally Posted by srizbf View Post
    in the +/- 2g range , the resolution is 10bits.
    The value per bit is 3.9mg.(3.90625mg exactly).

    multiply your axis reading with this constant to get g value.
    Hi, srizbf,
    According to the method you provide here, so i just simply use 218 * 0.0039g which = 0.8502g. But i thought when the accelerometer is stationary the z-axis should be 1g? Am i doing something wrong here?



  4. #4
    Advanced Member level 5
    Points: 12,423, Level: 26
    Achievements:
    7 years registered

    Join Date
    Apr 2010
    Posts
    1,889
    Helped
    405 / 405
    Points
    12,423
    Level
    26

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Nothing wrong.
    it indicates that the accelerometer may be stationary but is not perfectly vertical.



  5. #5
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Quote Originally Posted by srizbf View Post
    Nothing wrong.
    it indicates that the accelerometer may be stationary but is not perfectly vertical.
    I did some research and some said to acquire 1g offset needs to be either subtract or add to the data is this the case when i need to find the offset too?



  6. #6
    Super Moderator
    Points: 28,982, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,630
    Helped
    1096 / 1096
    Points
    28,982
    Level
    41
    Blog Entries
    8

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    so i just simply use 218 * 0.0039g which = 0.8502g
    You cannot simply perform a scalar multiplication with only one axis, but vectorial along 3 axis, as below:

    Click image for larger version. 

Name:	CodeCogsEqn1.gif 
Views:	2 
Size:	1.6 KB 
ID:	152468

    Anyway, something seems to be wrong, whether in the conversion factor or in the need to calibrate the measured value. or even the need to subtract the base line value due to gravity acceleration.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  7. #7
    Super Moderator
    Points: 73,784, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,972
    Helped
    3409 / 3409
    Points
    73,784
    Level
    66

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Hi,

    Do some"calibration tests":
    Put the sensor on the table and fix it with a tape.
    (Maybe mark it's position and direction on the table with a pen)
    * do the measurement an write down the values (best if you do a couple of measurements to get a clue about fluctuation and noise. You may also cancel out some noise by taking the average)
    * the move the sensor upside down and fix it with identical direction on the table.
    (Z should be negative now)
    Now calculate:
    Z_offset = (z_before + z_now)/2
    Z_sensitivity = (z_before - z_now)/2
    Z_multiplier = 1G / z_sensitivity

    For all further Z calculations use: Z = (z_measured - z_offset) * z_multiplier

    Do the same with the other two axis.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   AltAdvertisment

        
       

  8. #8
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Quote Originally Posted by andre_teprom View Post
    You cannot simply perform a scalar multiplication with only one axis, but vectorial along 3 axis, as below:

    Click image for larger version. 

Name:	CodeCogsEqn1.gif 
Views:	2 
Size:	1.6 KB 
ID:	152468

    Anyway, something seems to be wrong, whether in the conversion factor or in the need to calibrate the measured value. or even the need to subtract the base line value due to gravity acceleration.
    Hi andre_teprom,
    Thanks for providing the correct method on perfoming a scalar multiplication.



    •   AltAdvertisment

        
       

  9. #9
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Quote Originally Posted by KlausST View Post
    Hi,

    Do some"calibration tests":
    Put the sensor on the table and fix it with a tape.
    (Maybe mark it's position and direction on the table with a pen)
    * do the measurement an write down the values (best if you do a couple of measurements to get a clue about fluctuation and noise. You may also cancel out some noise by taking the average)
    * the move the sensor upside down and fix it with identical direction on the table.
    (Z should be negative now)
    Now calculate:
    Z_offset = (z_before + z_now)/2
    Z_sensitivity = (z_before - z_now)/2
    Z_multiplier = 1G / z_sensitivity

    For all further Z calculations use: Z = (z_measured - z_offset) * z_multiplier

    Do the same with the other two axis.

    Klaus
    Hi KlausST,
    Thanks for providing the "calibration test" method. Unfortunately, i do not have access to the accelerometer at this moment, but as soon as i get access back on the accelerometer i'll definitely try out this method you suggested.



  10. #10
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Quote Originally Posted by KlausST View Post
    Hi,

    Do some"calibration tests":
    Put the sensor on the table and fix it with a tape.
    (Maybe mark it's position and direction on the table with a pen)
    * do the measurement an write down the values (best if you do a couple of measurements to get a clue about fluctuation and noise. You may also cancel out some noise by taking the average)
    * the move the sensor upside down and fix it with identical direction on the table.
    (Z should be negative now)
    Now calculate:
    Z_offset = (z_before + z_now)/2
    Z_sensitivity = (z_before - z_now)/2
    Z_multiplier = 1G / z_sensitivity

    For all further Z calculations use: Z = (z_measured - z_offset) * z_multiplier

    Do the same with the other two axis.

    Klaus
    Hi Klaus,

    I now have gain access back to the accelerometer and i have took 10 measurement readings from it while it's laying flat on the table. It seems that the z - axis readings are all from 216,217 and 220 so which 1 do i use to determine the offset value? Also what's the z_before and z_now? Can you clarify that for me?



  11. #11
    Super Moderator
    Points: 73,784, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,972
    Helped
    3409 / 3409
    Points
    73,784
    Level
    66

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Hi,

    z_before = the previously written down values.
    z_now = value of the actual measurement.

    If the sensor is corretly oriented, then z should be positive in normal position and negative in upside down position.

    --> please show us all your measurement values with the information of sensor direction.


    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  12. #12
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Quote Originally Posted by KlausST View Post
    Hi,

    z_before = the previously written down values.
    z_now = value of the actual measurement.

    If the sensor is corretly oriented, then z should be positive in normal position and negative in upside down position.

    --> please show us all your measurement values with the information of sensor direction.


    Klaus
    HTML Code:
    Acceleration in X-Axis : -6
    Acceleration in Y-Axis : 5
    Acceleration in Z-Axis : 222
    Acceleration in X-Axis : -6
    Acceleration in Y-Axis : 5
    Acceleration in Z-Axis : 222
    Acceleration in X-Axis : -9
    Acceleration in Y-Axis : 3
    Acceleration in Z-Axis : 219
    Acceleration in X-Axis : -9
    Acceleration in Y-Axis : 3
    Acceleration in Z-Axis : 219
    Acceleration in X-Axis : -10
    Acceleration in Y-Axis : 4
    Acceleration in Z-Axis : 221
    Acceleration in X-Axis : -10
    Acceleration in Y-Axis : 4
    Acceleration in Z-Axis : 218
    Acceleration in X-Axis : -10
    Acceleration in Y-Axis : 4
    Acceleration in Z-Axis : 220
    Acceleration in X-Axis : -8
    Acceleration in Y-Axis : 6
    Acceleration in Z-Axis : 220
    Acceleration in X-Axis : -8
    Acceleration in Y-Axis : 3
    Acceleration in Z-Axis : 217
    Acceleration in X-Axis : -6
    Acceleration in Y-Axis : 6
    Acceleration in Z-Axis : 221
    HTML Code:
    Acceleration in X-Axis : -6
    Acceleration in Y-Axis : 5
    Acceleration in Z-Axis : 222
    Acceleration in X-Axis : -6
    Acceleration in Y-Axis : 5
    Acceleration in Z-Axis : 222
    Acceleration in X-Axis : -9
    Acceleration in Y-Axis : 3
    Acceleration in Z-Axis : 219
    Acceleration in X-Axis : -9
    Acceleration in Y-Axis : 3
    Acceleration in Z-Axis : 219
    Acceleration in X-Axis : -10
    Acceleration in Y-Axis : 4
    Acceleration in Z-Axis : 221
    Acceleration in X-Axis : -10
    Acceleration in Y-Axis : 4
    Acceleration in Z-Axis : 218
    Acceleration in X-Axis : -10
    Acceleration in Y-Axis : 4
    Acceleration in Z-Axis : 220
    Acceleration in X-Axis : -8
    Acceleration in Y-Axis : 6
    Acceleration in Z-Axis : 220
    Acceleration in X-Axis : -8
    Acceleration in Y-Axis : 3
    Acceleration in Z-Axis : 217
    Acceleration in X-Axis : -6
    Acceleration in Y-Axis : 6
    Acceleration in Z-Axis : 221
    (When the accelerometer is upside down)

    These are the data i get from the accelerometer with a loop of 10 times.

    In direction like thisClick image for larger version. 

Name:	forumwithaccelDirectionsa.PNG 
Views:	3 
Size:	25.9 KB 
ID:	152611


    "z_before = the previously written down values.
    z_now = value of the actual measurement."
    I am a little bit confused by this.



  13. #13
    Super Moderator
    Points: 73,784, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,972
    Helped
    3409 / 3409
    Points
    73,784
    Level
    66

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Hi,

    * normal position measurement
    * Z values are around 220. (I asked you to write these values down)
    * then I asked you to turn the sensor upside down
    * Z values now are around -264
    * so z before was about 220 and now it is about -264
    Where is the difficulty?

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  14. #14
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Quote Originally Posted by KlausST View Post
    Hi,

    * normal position measurement
    * Z values are around 220. (I asked you to write these values down)
    * then I asked you to turn the sensor upside down
    * Z values now are around -264
    * so z before was about 220 and now it is about -264
    Where is the difficulty?

    Klaus
    Hi KlausST,
    Omg i felt so dumb and thanks for clearing that up for me. Also all the data was acquired with a loop that goes through 10 times, do i need to average them to get the z_before and z_now?



  15. #15
    Super Moderator
    Points: 73,784, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,972
    Helped
    3409 / 3409
    Points
    73,784
    Level
    66

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Hi,

    You have 10 readings (each). The values vary 218...222 (-262 ... -265) because of noise.
    If you take a random single value frome the 10 values...you also get a noisy result. With high uncertainty.
    If you take the average of the 10 values then you reduce the noise and thus reduce uncertainty.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  16. #16
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Quote Originally Posted by KlausST View Post
    Hi,

    You have 10 readings (each). The values vary 218...222 (-262 ... -265) because of noise.
    If you take a random single value frome the 10 values...you also get a noisy result. With high uncertainty.
    If you take the average of the 10 values then you reduce the noise and thus reduce uncertainty.

    Klaus
    Hi KlausST,
    Got it, thanks.

    [QUOTE=KlausST;1647863
    Z_offset = (z_before + z_now)/2
    Z_sensitivity = (z_before - z_now)/2
    Z_multiplier = 1G / z_sensitivityKlaus
    For all further Z calculations use: Z = (z_measured - z_offset) * z_multiplier[/QUOTE]

    Which means
    Z_offset = (219 + (-263.8)) / 2 = -22.4
    Z_sensitivity = (219 - (-263.8)) / 2 = 241.4
    Z_Multiplier = 256 / 241.4 = 1.061 Am i correct on these calculations?

    Also next time when i needed to get Z-axis in G i have to do all the calculations above right?



    •   AltAdvertisment

        
       

  17. #17
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Hi KlausST,
    Also i have noticed that based on the method you provided, it takes both +- z-axis data in order to calculate the offset and to convert the value in G. Is there any other way that i can get the offset that would be usable for both the +ve and -ve of z-axis? Because what i am doing is a fall detection system using raspberry pi and it will be strap on the wrist and the accelerometer will likely be on the same direction for most of the time so it might be impossible to calculate the offset value and convert it into G all the time.



  18. #18
    Super Moderator
    Points: 73,784, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,972
    Helped
    3409 / 3409
    Points
    73,784
    Level
    66

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Hi,

    Also next time when i needed to get Z-axis in G i have to do all the calculations above right?
    No, this why I explicitely wrote:
    For all further Z calculations use: Z = (z_measured - z_offset) * z_multiplier
    All other calculations are - as written - for the calibration only ... and thus only need to be calculated when you want to run a calibration cycle.

    ****
    Also i have noticed that based on the method you provided, it takes both +- z-axis data in order to calculate the offset and to convert the value in G. Is there any other way that i can get the offset that would be usable for both the +ve and -ve of z-axis?
    Is the given method too simple?

    But yes there are other methods. Like: Bring your sensor to the ISS where is zero-G and then you donīt need positive and negative data.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  19. #19
    Junior Member level 2
    Points: 126, Level: 1

    Join Date
    Apr 2019
    Posts
    23
    Helped
    0 / 0
    Points
    126
    Level
    1

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Quote Originally Posted by KlausST View Post
    Hi,


    No, this why I explicitely wrote:


    All other calculations are - as written - for the calibration only ... and thus only need to be calculated when you want to run a calibration cycle.

    ****

    Is the given method too simple?

    But yes there are other methods. Like: Bring your sensor to the ISS where is zero-G and then you donīt need positive and negative data.

    Klaus
    No, what i mean is the above method will only works with both the positive and the upside down accelerometer data which is the negative data. But i am doing fall detection system and the accelerometer will be attached on the same direction for most of the times which means it might not even get negative data for the z-axis. So in this case i will not be able to perform all this also.
    Quote Originally Posted by KlausST View Post
    Z_offset = (z_before + z_now)/2
    Z_sensitivity = (z_before - z_now)/2
    Z_multiplier = 1G / z_sensitivityKlaus
    For all further Z calculations use: Z = (z_measured - z_offset) * z_multiplier
    As all this will only works with the z_before and z_now which are the positive data and the negative data.
    Last edited by ads-ee; 26th April 2019 at 16:17. Reason: fixed quote



  20. #20
    Super Moderator
    Points: 73,784, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,972
    Helped
    3409 / 3409
    Points
    73,784
    Level
    66

    Re: How to convert accelerometer data into G with the ADXL345 digital accelerometer

    Hi,

    No, what i mean is the above method will only works with both the positive and the upside down accelerometer data which is the negative data. But i am doing fall detection system and the accelerometer will be attached on the same direction for most of the times which means it might not even get negative data for the z-axis. So in this case i will not be able to perform all this also.
    Makes no sense to me...

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



--[[ ]]--