mma845x accelerometer problem

Viewed 32 times

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

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)

And the output is this:


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
  • 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 Dec 19 ’19 at 13:47
  • 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 Dec 19 ’19 at 13:55
  • Section 3 tells you how to set the active mode: Section 3.0 … The device changes from Standby to Active Mode via bit 0 in register 0x2A, with demo code:. /* ** Set the Active bit in CTRL Reg 1 */ IIC_RegWrite(CTRL_REG1, (IIC_RegRead(CTRL_REG1) | ACTIVE_MASK)); So you might like to try your luck. Warning: I have never seen or played with your accelero module. I am just reading the friendly datasheet and app notes. No guarantee nothing won’t melt down nor blow up. Good luck and cheers! 🙂 – tlfong01 Dec 19 ’19 at 14:05
  • @tlfong01 Thank you, your solution actually worked. The output values are now read properly, but I am having a different problem and that is the SDA/SCL value change and I connect other things to different pin of the pi (in this case quadcopter’s propellers) but that is a different issue. Thanks again. – Stefanos 20 mins ago
  • @Stefanos, How nice to hear that your problem is solved. Ah, SDA/SCL problems are also sometimes tricky. Perhaps you can ask your other I2C questions is this forum. Happy quadcopter propelling! Cheers. – tlfong01 just now   Edit

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: