Hi,
I'm using an ESP32 and an MMA8452Q. I'm using this code. ESP32 and MMA8452 accelerometer example | ESP32 Learning
The error I'm having is that the same code is repeating line by line and it's 9 digits long. For example:
22:06:23.654 -> Acceleration in Y-Axis : 102901751
22:06:23.690 -> Acceleration in Z-Axis : 152110118
22:06:25.103 -> Acceleration in X-Axis : 12454281
22:06:25.136 -> Acceleration in Y-Axis : 102901751
I am also using ESP32. but when i read the article, it states that it can operate 3.3v but to protect the SDA and SCL lines, you need to add a 330 ohm resistor on both lines. So, what i mean to say in the first reply, did you add the resistors?
Hi. Thanks. Yes you’re right. I didn’t use any resistors. Additionally I used a 3.7 volt battery so that may be the issue as there is a 3.6 maximum voltage.
As a troubleshooting step try changing this: unsigned int data[7];
To: unsigned int data[7] = { 0 };
You'll probably get all zeros.
EDIT
You could also try after this: Wire.requestFrom(Addr, 7);
Adding: delay(10);
EDIT 2
Here is the code referenced in the OP.
It has not been written very nicely because even if it fails to retrieve the expected data from the module, it prints based on whatever has been left in the buffer.
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// MMA8452Q
// This code is designed to work with the MMA8452Q_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Accelorometer?sku=MMA8452Q_I2CS#tabs-0-product_tabset-2
#include <Wire.h>
// MMA8452Q I2C address is 0x1C(28)
#define Addr 0x1C
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise Serial Communication, set baud rate = 9600
Serial.begin(9600);
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select control register
Wire.write(0x2A);
// StandBy mode
Wire.write((byte)0x00);
// Stop I2C Transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select control register
Wire.write(0x2A);
// Active mode
Wire.write(0x01);
// Stop I2C Transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select control register
Wire.write(0x0E);
// Set range to +/- 2g
Wire.write((byte)0x00);
// Stop I2C Transmission
Wire.endTransmission();
delay(300);
}
void loop()
{
unsigned int data[7];
// Request 7 bytes of data
Wire.requestFrom(Addr, 7);
// Read 7 bytes of data
// staus, xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
if(Wire.available() == 7)
{
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
data[3] = Wire.read();
data[4] = Wire.read();
data[5] = Wire.read();
data[6] = Wire.read();
}
// Convert the data to 12-bits
int xAccl = ((data[1] * 256) + data[2]) / 16;
if (xAccl > 2047)
{
xAccl -= 4096;
}
int yAccl = ((data[3] * 256) + data[4]) / 16;
if (yAccl > 2047)
{
yAccl -= 4096;
}
int zAccl = ((data[5] * 256) + data[6]) / 16;
if (zAccl > 2047)
{
zAccl -= 4096;
}
// Output data to serial monitor
Serial.print("Acceleration in X-Axis : ");
Serial.println(xAccl);
Serial.print("Acceleration in Y-Axis : ");
Serial.println(yAccl);
Serial.print("Acceleration in Z-Axis : ");
Serial.println(zAccl);
delay(500);
}