MMA8451 Libraty Bug?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
vicatcu
 
Posts: 29
Joined: Sat Oct 01, 2011 9:35 pm

MMA8451 Libraty Bug?

Post by vicatcu »

Hey all,

I'm following along with this tutorial and library https://learn.adafruit.com/adafruit-mma ... g-and-test. It works as described, but I think something is wrong.
Example for line 2:
X: -0.07 Y: 0.09 Z: 0.98 m/s^2
I believe (and Wikipedia confirms) that the acceleration due to gravity is 9.8 m/s^2 not 0.98 m/s^2. The library example, true to the tutorial, does produce 0.98 though. I dug in and if I replace the lines:

Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");

with...

Serial.print("X: \t"); Serial.print(mma.x_g * (SENSORS_GRAVITY_STANDARD)); Serial.print("\t");
Serial.print("Y: \t"); Serial.print(mma.y_g * (SENSORS_GRAVITY_STANDARD)); Serial.print("\t");
Serial.print("Z: \t"); Serial.print(mma.z_g * (SENSORS_GRAVITY_STANDARD)); Serial.print("\t");

It comes out right... but that looks like pretty much exactly what is being done in the library internally. Nevertheless, it is coming out of the library differently than if I put the conversion in the sketch directly. I'm compiling in Arduino 1.6.9 on Ubuntu 14.04 if that matters. I traced through the library and nothing jumped out at me as obviously wrong, but there is something nuanced going wrong I'm pretty sure.

I brought it here instead of putting an issue on Git-Hub in accordance with the request on the GitHub issue template. What say you?

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

Re: MMA8451 Libraty Bug?

Post by adafruit_support_rick »

Seems impossible.

Unfortunately, I don't have one of these sensors to play around with.

What range are you set to?

If you change the beginning of loop() to this, can you post the serial monitor output?

Code: Select all

void loop() {
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mma.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("event X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
  Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
  Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
  Serial.println("m/s^2 ");

  Serial.print("raw X:\t"); Serial.print(mma.x); 
  Serial.print("\tY:\t"); Serial.print(mma.y); 
  Serial.print("\tZ:\t"); Serial.print(mma.z); 
  Serial.println();

  

User avatar
vicatcu
 
Posts: 29
Joined: Sat Oct 01, 2011 9:35 pm

Re: MMA8451 Libraty Bug?

Post by vicatcu »

Hey Rick, yea I don't disagree that it's surprising. I tried with all three range settings. You should give it a try and see for yourself I guess.

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

Re: MMA8451 Libraty Bug?

Post by adafruit_support_rick »

Yeah, but like I said, I don't have one. I'll have to order one.
Meanwhile, can you run the code I posted?

User avatar
vicatcu
 
Posts: 29
Joined: Sat Oct 01, 2011 9:35 pm

Re: MMA8451 Libraty Bug?

Post by vicatcu »

Hey Rick,

Sorry I missed that request. Here's a sample of the streaming output you get with just your code snippet in loop.

Code: Select all

event X: 	0.17	Y: 	0.01	Z: 	0.95	m/s^2 
raw X:	712	Y:	54	Z:	3874
event X: 	0.17	Y: 	0.02	Z: 	0.95	m/s^2 
raw X:	700	Y:	84	Z:	3896
event X: 	0.17	Y: 	0.02	Z: 	0.94	m/s^2 
raw X:	690	Y:	76	Z:	3864
event X: 	0.17	Y: 	0.02	Z: 	0.95	m/s^2 
raw X:	692	Y:	66	Z:	3880
event X: 	0.17	Y: 	0.02	Z: 	0.94	m/s^2 
raw X:	716	Y:	72	Z:	3866
event X: 	0.17	Y: 	0.02	Z: 	0.94	m/s^2 
raw X:	716	Y:	62	Z:	3864
event X: 	0.17	Y: 	0.01	Z: 	0.94	m/s^2 
raw X:	696	Y:	58	Z:	3860
event X: 	0.17	Y: 	0.01	Z: 	0.95	m/s^2 
raw X:	696	Y:	44	Z:	3906
event X: 	0.17	Y: 	0.02	Z: 	0.95	m/s^2 
raw X:	692	Y:	66	Z:	3880
event X: 	0.17	Y: 	0.02	Z: 	0.95	m/s^2 
raw X:	698	Y:	70	Z:	3876
event X: 	0.17	Y: 	0.02	Z: 	0.95	m/s^2 
raw X:	700	Y:	70	Z:	3878
event X: 	0.17	Y: 	0.01	Z: 	0.95	m/s^2 
raw X:	710	Y:	52	Z:	3888
event X: 	0.17	Y: 	0.01	Z: 	0.94	m/s^2 
raw X:	688	Y:	58	Z:	3870
event X: 	0.17	Y: 	0.02	Z: 	0.95	m/s^2 
raw X:	696	Y:	66	Z:	3884
event X: 	0.17	Y: 	0.01	Z: 	0.94	m/s^2 
raw X:	700	Y:	48	Z:	3868
event X: 	0.18	Y: 	0.02	Z: 	0.94	m/s^2 
raw X:	728	Y:	70	Z:	3848
event X: 	0.17	Y: 	0.02	Z: 	0.95	m/s^2 
raw X:	690	Y:	92	Z:	3884
event X: 	0.17	Y: 	0.02	Z: 	0.95	m/s^2 
raw X:	692	Y:	88	Z:	3882

User avatar
jecottrell
 
Posts: 249
Joined: Sat Jan 02, 2016 1:47 am

Re: MMA8451 Libraty Bug?

Post by jecottrell »

Looks like it uses the correct factor:

Code: Select all

#define SENSORS_GRAVITY_EARTH             (9.80665F)              /**< Earth's gravity in m/s^2 */
#define SENSORS_GRAVITY_STANDARD          (SENSORS_GRAVITY_EARTH)

I believe the reading you are seeing is "Gs", no?

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

Re: MMA8451 Libraty Bug?

Post by adafruit_support_rick »

OK, so you must have the range set to 2G, right? If so, the raw Z reading will be divided by 4096, giving ~0.94, which is what we see.
So, the event event.acceleration.z is not being multiplied by 9.8. You're seeing the scaled raw reading.

You must have an old version of the library. Originally, it did not multiply by 9.8 when you called getEvent. It does now.

User avatar
vicatcu
 
Posts: 29
Joined: Sat Oct 01, 2011 9:35 pm

Re: MMA8451 Libraty Bug?

Post by vicatcu »

That would make sense. I installed it using the library manager in arduino 1.6.9. Maybe that is staler than GitHub. I imagined it would get the latest from GitHub, but perhaps not. I'll check and confirm back here when I have a chance.

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

Re: MMA8451 Libraty Bug?

Post by adafruit_support_rick »

Make sure you don't have an old version laying around someplace, that's getting used instead of the new one.

User avatar
vicatcu
 
Posts: 29
Joined: Sat Oct 01, 2011 9:35 pm

Re: MMA8451 Libraty Bug?

Post by vicatcu »

When it compiles the verbose output shows;
Using library Adafruit_Unified_Sensor at version 1.0.2 in folder: /home/vic/sketchbook/libraries/Adafruit_Unified_Sensor
Using library Adafruit_MMA8451_Library at version 1.0.0 in folder: /home/vic/sketchbook/libraries/Adafruit_MMA8451_Library

Downloaded the sources from Git-Hub and the Adafruit_MMA8451_Library differs in many ways, among which certainly explain this. So I went into the Library Manager in Arduino and clicked "Update". Lo' and behold the my library is now in sync with Git-Hub. So either you guys just updated it in the last 48 hours (?) or I did it wrong somehow (perhaps I chose 1.0.0 from the version drop down or something?).

Anyway, sorry for the red herring, and thanks for your time.

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

Re: MMA8451 Libraty Bug?

Post by adafruit_support_rick »

Let's see... the last time the library version was updated was 2 months ago. The 9.8m/s^2 stuff was added 5 months ago.

Anyway, glad we got it sorted!

User avatar
juergen001
 
Posts: 4
Joined: Fri Apr 13, 2018 4:21 am

Re: MMA8451 Libraty Bug?

Post by juergen001 »

Hello, I´m Jürgen
I write to you from germany because my friends in the german arduino forum can´t help. My english is not the best, sorry. I hope, you understand my problem nevertheless.
I have an arduino due and a new MMA8451. The terminal window shows

Adafruit MMA8451 test!
Couldnt start

Maybe I´m a fool, because I thought, the bug in the lib was fixed 2015.

I controlled the soldering dots - they are fine
I fair the resistors sca -vcc and sda- vcc = 9.8 Kohms
the i2c scanner shows 0x1D

can you help me with an idea?

a lot of thanks

Jürgen

Your other boards (two 16 channel PWM Servo Driver and an VL 53LOX Distance Sensor) works fine with my arduino due

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: MMA8451 Libraty Bug?

Post by Franklin97355 »

Can you post pictures of your connections and tell us which code you are running? The board should already have pull-ups so you should not need to add any. If you haven't already, take a look at this tutorial.

User avatar
juergen001
 
Posts: 4
Joined: Fri Apr 13, 2018 4:21 am

Re: MMA8451 Libraty Bug?

Post by juergen001 »

]
franklin97355 wrote:Can you post pictures of your connections and tell us which code you are running? The board should already have pull-ups so you should not need to add any. If you haven't already, take a look at this tutorial.
hello Franklin,
many thanks for your answer. Yes I know, the Board does not need extern pullup resistors. I controlled only my soldering points.
my code is the original

Code: Select all

/**************************************************************************/
/*!
    @file     Adafruit_MMA8451.h
    @author   K. Townsend (Adafruit Industries)
    @license  BSD (see license.txt)

    This is an example for the Adafruit MMA8451 Accel breakout board
    ----> https://www.adafruit.com/products/2019

    Adafruit invests time and resources providing this open source code,
    please support Adafruit and open-source hardware by purchasing
    products from Adafruit!

    @section  HISTORY

    v1.0  - First release
*/
/**************************************************************************/

#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>

Adafruit_MMA8451 mma = Adafruit_MMA8451();

void setup(void) {
  Serial.begin(9600);
  
  Serial.println("Adafruit MMA8451 test!");
  

  if (! mma.begin()) {
    Serial.println("Couldnt start");
    while (1);
  }
  Serial.println("MMA8451 found!");
  
  mma.setRange(MMA8451_RANGE_2_G);
  
  Serial.print("Range = "); Serial.print(2 << mma.getRange());  
  Serial.println("G");
  
}

void loop() {
  // Read the 'raw' data in 14-bit counts
  mma.read();
  Serial.print("X:\t"); Serial.print(mma.x); 
  Serial.print("\tY:\t"); Serial.print(mma.y); 
  Serial.print("\tZ:\t"); Serial.print(mma.z); 
  Serial.println();

  /* Get a new sensor event */ 
  sensors_event_t event; 
  mma.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
  Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
  Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
  Serial.println("m/s^2 ");
  
  /* Get the orientation of the sensor */
  uint8_t o = mma.getOrientation();
  
  switch (o) {
    case MMA8451_PL_PUF: 
      Serial.println("Portrait Up Front");
      break;
    case MMA8451_PL_PUB: 
      Serial.println("Portrait Up Back");
      break;    
    case MMA8451_PL_PDF: 
      Serial.println("Portrait Down Front");
      break;
    case MMA8451_PL_PDB: 
      Serial.println("Portrait Down Back");
      break;
    case MMA8451_PL_LRF: 
      Serial.println("Landscape Right Front");
      break;
    case MMA8451_PL_LRB: 
      Serial.println("Landscape Right Back");
      break;
    case MMA8451_PL_LLF: 
      Serial.println("Landscape Left Front");
      break;
    case MMA8451_PL_LLB: 
      Serial.println("Landscape Left Back");
      break;
    }
  Serial.println();
  delay(500);
  
}
compiling and flashing works fine, then comes in the Terminal:
Adafruit MMA8451 test
Couldnt start
[img]

[/img]
The sda and slc connections are on Pin 20 and 21 at the Due

Best regards

Jürgen

hello once more,

just in time came my MEGA 2560. I connected it at the MMA, flashed it with the same code and it runs fine ?????
The tutorial I know, but it doesn´t help
Attachments
Due4.png
Due4.png (615.6 KiB) Viewed 363 times
Due3.png
Due3.png (640.81 KiB) Viewed 363 times
MMA2.png
MMA2.png (698.08 KiB) Viewed 363 times

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

Re: MMA8451 Libraty Bug?

Post by adafruit_support_rick »

Originally, the Wire library on the Arduino Due did not support I2C repeated starts. A few years ago, support was added through an undocumented overload of the Wire.requestFrom function:
https://forum.arduino.cc/index.php?topi ... msg2947227

The MMA8451 library did not implement this fix. I have just now updated the library to version 1.0.5. Look for the new version in the Arduino Library Manager.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”