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)
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.
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?Nothing wrong.
it indicates that the accelerometer may be stationary but is not perfectly vertical.
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:
View attachment 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,
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,
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,
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
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
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
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,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
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 said: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?
No, this why I explicitely wrote:Also next time when i needed to get Z-axis in G i have to do all the calculations above right?
For all further Z calculations use: Z = (z_measured - z_offset) * z_multiplier
Is the given method too simple?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?
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
As all this will only works with the z_before and z_now which are the positive data and the negative data.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
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.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?