Adafruit MMA8451

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
TheRafal
Posts: 12
Joined: 30 Aug 2020 11:01

Adafruit MMA8451

Post by TheRafal »

Hi, i can't get my Adafruit sensor MMA8451 working properly with I2C. I've a kind of noise when reading values. i've tested the same code and wiring with a mega and a nano without troubles.

Here is my code.

Code: Select all


/*
 Name:    Zuno.ino
 Created: 03/03/2019 09:16:11
 Author:  RorshaR
*/

//Libraries

#include <Adafruit_MMA8451.h>


#pragma region Variables Local
Adafruit_MMA8451 mma = Adafruit_MMA8451(); //initialisation du gyro
int ZValuePrivate;
int XValuePrivate;
int YValuePrivate;
#pragma endregion



// the setup function runs once when you press reset or power the board
void setup() {

  mma.begin();

  Serial.begin(9600);

  Serial.println("MMA8451 found!");

  mma.setRange(MMA8451_RANGE_8_G);
  mma.setDataRate(MMA8451_DATARATE_50_HZ);
}

// the loop function runs over and over again until power down or reset
void loop() {
  mma.read();

  Serial.print(mma.x);
  Serial.print(" Y: ");
  Serial.print(mma.y);
  Serial.print(" Z: ");
  Serial.println(mma.z);
  

}
Like i've said in another post, i need to comment the section of code with Adafruit sensor.h include in the mma8451.h file to get it work. I don't have another sensor to test I2C bus and my code to communicate between 2 arduino's doesn't work with z-uno. If someone can help me...

TheRafal
Posts: 12
Joined: 30 Aug 2020 11:01

Re: Adafruit MMA8451

Post by TheRafal »

UP ! I've tryed different things like rebuild my wirring, read all i've found in reference about I2C on z-uno to be sure to don't miss a thing, compare library with another I2C one for Z-uno (BMP180) but i don't understand why this code work on nano and mega but not with z-uno. it's strange, it fell like noisy

TheRafal
Posts: 12
Joined: 30 Aug 2020 11:01

Re: Adafruit MMA8451

Post by TheRafal »

I've a question for the dev, is the I2C bus have a pull-up resistor ? It's maybe the problem i have

TheRafal
Posts: 12
Joined: 30 Aug 2020 11:01

Re: Adafruit MMA8451

Post by TheRafal »

Ok, i've tried to add pull-up resistor on I2C line but unfortunately, no changes on values... i don't understand what to do, the values sended to my other arduino is inconsistente too...

TheRafal
Posts: 12
Joined: 30 Aug 2020 11:01

Re: Adafruit MMA8451

Post by TheRafal »

Ok today i've test to modify the adafruit library. i remove some code inside to have just the Wire.read() values and i get some good news.

Here is the original code of the read function

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;

}
As you can see we have a bit to bit convertion to have the angle in positive and negative value just after the requestFrom. If i remove the convertion

Code: Select all

  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;
and just do this

Code: Select all

  
   Wire.requestFrom(_i2caddr, 8);
  x = Wire.read(); //for fake using of bit
  x = Wire.read();
  y = Wire.read();
  z = Wire.read();
X,Y and Z are stable from 0 to 255 but when i arrived at 0 if i continue to move a drop to 255 and decrease.

My supposition is the Z-Uno can't make this convertion because... I don't know, maybe to much bit to read or the convertion is to complexe for it.

I continu to test some other things, if someone can help me...

Post Reply