2024-08-09 9:53 AM
I'm using the STEVAL-MKI245KA development kit, trying to get the IMU readings with an AT-Mega through I2C. The I2C communication between the adapter board of the kit is working, but the IMU values recieved by the arduino are not updating. When I run the code to get the accelerometer and gyroscope values, the value is always 0 for all axes acc and gyro. (the reason I know the I2C is working is because when it wasn't working, the values were all -1, but when it does 'work' the values at all 0).
Here is a screenshot from the datasheet of the IMU with the registers of all the parameters:
And here is the code that I am using to access the values at these registers:
#include <Wire.h>
#define IMU_ADDRESS 0x6B // Address of the IMU device
void setup() {
Wire.begin(); // Initialize I2C communication
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Read STATUS_REG register
Wire.beginTransmission(IMU_ADDRESS);
Wire.write(0x1E); // Address of the STATUS_REG register
Wire.endTransmission(false);
Wire.requestFrom(IMU_ADDRESS, 1);
byte status = Wire.read();
// Check if there is new accelerometer and gyroscope data available
bool newDataAvailable = (status & 0x03) == 0x03;
if (newDataAvailable) {
// Read angular rate of pitch
int pitch = readAxisData(0x22); // OUTX_L_G register
// Read angular rate of roll
int roll = readAxisData(0x24); // OUTY_L_G register
// Read angular rate of yaw
int yaw = readAxisData(0x26); // OUTZ_L_G register
// Read acceleration of X axis
int accelX = readAxisData(0x28); // OUTX_L_A register
// Read acceleration of Y axis
int accelY = readAxisData(0x2A); // OUTY_L_A register
// Read acceleration of Z axis
int accelZ = readAxisData(0x2C); // OUTZ_L_A register
// Print the values
Serial.print("Pitch: ");
Serial.println(pitch);
Serial.print("Roll: ");
Serial.println(roll);
Serial.print("Yaw: ");
Serial.println(yaw);
Serial.print("Accel X: ");
Serial.println(accelX);
Serial.print("Accel Y: ");
Serial.println(accelY);
Serial.print("Accel Z: ");
Serial.println(accelZ);
}
else {
Serial.println("No new data");
}
delay(1000); // Wait for 1 second before reading again
}
int readAxisData(uint8_t reg) {
Wire.beginTransmission(IMU_ADDRESS);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(IMU_ADDRESS, 2);
// Read the 16-bit value
int16_t value = Wire.read() | (Wire.read() << 8);
if (value & (1 << 15)) {
value = value - 65536; // If negative, convert to 16-bit value
}
return value;
}What am I doing wrong? My end goal is to read and use the IMU accel and gyro values for dead reckoning.
2024-08-30 2:36 AM
Hi @cloudymnms ,
Can you try to follow our official PID example on Github and let me know if you will be able to get the data? Thanks
We’re moving the ST Community to a new platform to give you a better and more reliable community experience.