I want to access the raw XYZ data directly using address and register values. (with the library it already comes predefined, so I don't have the freedom of setting register values). Here is what I have so far
Code: Select all
#include <ICM_20948.h>
#include <Wire.h>
long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
int IMU_209 = 0x69;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
void setup() {
Serial.begin(9600);
Wire.begin();
setupMPU();
}
void loop() {
recordAccelRegisters();
recordGyroRegisters();
printData();
delay(100);
}
void setupMPU(){
Wire.beginTransmission(IMU_209); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
Wire.write(0x127); // Register Bank selection
Wire.write(0b00000000); // Register Bank selection
Wire.write(0x06); //Accessing the register 6B - Power Management (Sec. 4.28)
Wire.write(6); //Setting SLEEP register to 0. (Required; see Note on p. 9)
Wire.endTransmission();
Wire.beginTransmission(IMU_209); //I2C address of the MPU
Wire.write(0x01); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
Wire.write(0x00011100); //Setting the gyro to full scale +/- 250deg./s
Wire.endTransmission();
Wire.beginTransmission(IMU_209); //I2C address of the MPU
Wire.write(0x14); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
Wire.write(0b00011100); //Setting the accel to +/- 2g
Wire.endTransmission();
}
void recordAccelRegisters() {
Wire.beginTransmission(IMU_209); //I2C address of the MPU
Wire.write(0x127);// Register Bank selection
Wire.write(0b00000000);// Register Bank selection
Wire.write(0x2D); //Starting register for Accel Readings
Wire.endTransmission();
Wire.requestFrom(IMU_209,6); //Request Accel Registers (3B - 40)
while(Wire.available() < 6);
accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
processAccelData();
}
void processAccelData(){
gForceX = accelX / 16384.0;
gForceY = accelY / 16384.0;
gForceZ = accelZ / 16384.0;
}
void recordGyroRegisters() {
Wire.beginTransmission(IMU_209); //I2C address of the MPU
Wire.write(0x127);// Register Bank selection
Wire.write(0b00000000);// Register Bank selection
Wire.write(0x33); //Starting register for Gyro Readings
Wire.endTransmission();
Wire.requestFrom(IMU_209,6); //Request Gyro Registers (43 - 48)
while(Wire.available() < 6);
gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
processGyroData();
}
void processGyroData() {
rotX = gyroX / 131.0;
rotY = gyroY / 131.0;
rotZ = gyroZ / 131.0;
}
void printData() {
Serial.print("Gyro (deg)");
Serial.print(" X=");
Serial.print(rotX);
Serial.print(" Y=");
Serial.print(rotY);
Serial.print(" Z=");
Serial.print(rotZ);
Serial.print(" Accel (g)");
Serial.print(" X=");
Serial.print(gForceX);
Serial.print(" Y=");
Serial.print(gForceY);
Serial.print(" Z=");
Serial.println(gForceZ);
}
This fails to output the value from the sensor. I've used the data sheet (IMU-20948) to get the register values in (hexadecimal). Do I need to declare anything else to get access to the sensor values? What can I do?
I'm using Arduino 1.8.14 IDE.