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.

  • Why are you packing/unpacking x, y, and z. Could you just print status, x, y, z as received. – joan Dec 19 '19 at 11:18
  • 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 Dec 19 '19 at 12:56
  • 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 Dec 19 '19 at 13:29
  • 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 Dec 19 '19 at 13:30
  • To troubleshoot, usually I start reading the application notes: penzu.com/public/f38b4cd1 – tlfong01 Dec 19 '19 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.