Page 1 of 1
Adafruit MMA8451
Posted: 29 Oct 2020 00:15
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...
Re: Adafruit MMA8451
Posted: 02 Nov 2020 12:12
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
Re: Adafruit MMA8451
Posted: 04 Nov 2020 00:35
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
Re: Adafruit MMA8451
Posted: 04 Nov 2020 11:47
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...
Re: Adafruit MMA8451
Posted: 06 Nov 2020 21:50
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...
Re: Adafruit MMA8451
Posted: 08 Jan 2022 00:52
by TheRafal
Hi, i'm back with my adafruit sensor. I can't use it properly... It's very frustrating. I didn't understood why the bitwise conversion make problem in reading value. Is the Z-uno chip have a different way to read bytes ? maybe the Significant Bit ? I didn't understand very well byte convertion so it's difficult to understand how it doesn't work in my case...
Re: Adafruit MMA8451
Posted: 12 Jan 2022 22:05
by p0lyg0n1
Hi,
What version of ZUno do you use? First generation or the second one? The first generation has 8 bit old school intel 8051 core and software I2C realization. It's very poor in stack and it's not so easy to port library to this architecture "as is".
The second generation has CortexM4 with hardware implementation of I2C. It's much more easy to port a library.
We don't update libraries for the first generation anymore. For the second generation we are able to help you with porting.
Best regards,
Alex.
Re: Adafruit MMA8451
Posted: 12 Jan 2022 23:35
by TheRafal
Hi
Yep it's the first gen... I've tried porting it before you release the gen2 and now i didn't have money buy it...
Thank you for your reply, i will try to do it even if it's difficult ^^
If i bought a Gen2, what's the main difference ?