Asked
Viewed 13 times
0
This is my code:
import time
import Adafruit_GPIO.I2C as Adafruit_I2C
import struct
from math import *
# Registers
OUT_X_MSB = 0x01
CTRL_REG1 = 0x2A
XYZ_DATA_CFG = 0x0E
i2c = Adafruit_I2C.Device(0x1c, 1) # Adresse accéléromètre
# Setups for calculation with configuration 2g
gPerCount = 2.0 /128
while True:
#Recovery of data from the accelerometer
(status,x,y,z) = i2c.readList(OUT_X_MSB, 4)
# Formatting for calculation
bytes = struct.pack('BBB', x, y, z)
x, y, z = struct.unpack('bbb', bytes)
# Calculation of the acceleration per axis as a function of the accelerometer parametrage
x *= gPerCount
y *= gPerCount
z *= gPerCount
print (x)
print (y)
print (z)
time.sleep(1)
And the output is this:
-1.4375
1.4375
1.0625
-1.4375
1.4375
1.0625
-1.4375
1.4375
1.0625
-1.4375
1.4375
1.0625
-1.4375
1.4375
1.0625
-1.4375
1.4375
1.0625
I Have checked the wiring and everything and for the life of me I cannot tell what the problem is.
New contributor
-
Why are you packing/unpacking x, y, and z. Could you just print status, x, y, z as received. – joan 2 hours ago
-
Could this be detecting gravity? I don’t know what position you chip is. Does it give the same numbers if you turn it? – NomadMaker 1 hour ago
-
Ah, let me see. (1) Your final output is always “-1.4375, 1.4375.1.0625”, to 4 decimal places. This implies the 2’s complement raw figures always the same. So there is no need to do any calculation to convert the raw 2’s complement numbers. Your code before converting raw data is in two big steps: (1) Config, (2) Read raw data. We can first look at the config/setup part, in your case is only one statement “gPerCount = 2.0 /128”. One thing is check out is to config yourself, usually in two or three steps: (a) Set g range, (b) Set interrupt config, (c) start measurement mode. / to continue, … – tlfong01 27 mins ago
-
We need to read the friendly datasheet to see the config procedure: (1) MMA8451Q 3-Axis, 14-bit/8-bit Digital Accelerometer Datasheet Document Number: MMA8451Q Rev. 10.3, 02/2017- NXP/ nxp.com/docs/en/data-sheet/MMA8451Q.pdf, … / to continue, … – tlfong01 26 mins ago
-
To troubleshoot, usually I start reading the application notes: penzu.com/public/f38b4cd1 – tlfong01 13 mins ago
-
I think we better start with the app note written by the app enggr: App Note AN4076 Data Manipulation and Basic Settings of the MMA8451/2/3Q by: Kimberly Tuck Applications Engineer bdtic.com/download/nxp/AN4076.pdf – tlfong01 10 mins ago
-
I skimmed the app note, and found the following sections useful: Sections 3, Standby and active mode (with code examples) , 7. Convert 2’complement to signed decimal, 9. Polling vs interrupt. One possible reason of your always getting the same data is BECAUSE YOU ARE IN STANDBY MODE, therefore always reading the old, never changing data.. – tlfong01 2 mins ago Edit
.END
Categories: Uncategorized