MMA8541 odd values

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
nevets89
 
Posts: 3
Joined: Tue Jun 02, 2015 12:28 am

MMA8541 odd values

Post by nevets89 »

I am working on a raspberry pi and using the MMA8541 acceleromer. I was getting what looks like valid values for the 2G range. I was getting between 0 and 100 for the X and Y values and between 4080 and 4096 for the Z. The positive Z value should have been a read flag but I was excited to have valid looking data since I am new to raspberry pi, I2C and python.

Enough of the background, My problem is that it seems like I am getting the absolute value not the real value. Am I doing something wrong when I am combining the bytes into an int or is it something else. Below is my pyhton code and below that is the code for the arduino.

Code: Select all

    def get_data(self):
        xmsb = self._read_data(self.REG_OUT_X_MSB)
        xlsb = self._read_data(self.REG_OUT_X_LSB)

        ymsb = self._read_data(self.REG_OUT_Y_MSB)
        ylsb = self._read_data(self.REG_OUT_Y_LSB)

        zmsb = self._read_data(self.REG_OUT_Z_MSB)
        zlsb = self._read_data(self.REG_OUT_Z_LSB)

        x= int_from_bytes(xmsb, xlsb)
        y= int_from_bytes(ymsb, ylsb)
        z= int_from_bytes(zmsb, zlsb)

        return [x,y,z]

Code: Select all

void Adafruit_MMA8451::read(void) {
  // read x y z at once
  Wire.beginTransmission(_i2caddr);
  i2cwrite(MMA8451_REG_OUT_X_MSB);
  Wire.endTransmission(false); // MMA8451 + friends uses repeated start!!

  Wire.requestFrom(_i2caddr, 6);
  x = Wire.read(); x <<= 8; x |= Wire.read(); x >>= 2;
  y = Wire.read(); y <<= 8; y |= Wire.read(); y >>= 2;
  z = Wire.read(); z <<= 8; z |= Wire.read(); z >>= 2;


  uint8_t range = getRange();
  uint16_t divider = 1;
  if (range == MMA8451_RANGE_8_G) divider = 1024;
  if (range == MMA8451_RANGE_4_G) divider = 2048;
  if (range == MMA8451_RANGE_2_G) divider = 4096;

  x_g = (float)x / divider;
  y_g = (float)y / divider;
  z_g = (float)z / divider;

}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: MMA8541 odd values

Post by adafruit_support_rick »

The Arduino code is dividing x, y, and z by 4 before applying the range divider. You're not doing that in the python. Otherwise, you seem to be doing everything right.

User avatar
nevets89
 
Posts: 3
Joined: Tue Jun 02, 2015 12:28 am

Re: MMA8541 odd values

Post by nevets89 »

adafruit_support_rick wrote:The Arduino code is dividing x, y, and z by 4 before applying the range divider. You're not doing that in the python. Otherwise, you seem to be doing everything right.
I don't think this is the issue. since the values I am getting are correct-ish but don't have the right sign. My Z value should be around -4096 but I'm getting 4096 instead. I have it in 2g mode and the documentation says that the sensitivity is 4096 counts/g.

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”