^Title
Setup:
I’ve tried using the Adafruit ICM as well as a 3D party ICM ( ICM-20948 9DoF IMU, 9-Axis Motion Tracking, Digital Motion Processor, Accelerometer, Gyroscope, Magnetometer, I2C & SPI: Amazon.com: Industrial & Scientific ) from amazon (it doesn’t work with both of them). I have an Arduino Giga R1 Wifi that i’m using to plug in the code. Using I2C wiring.
Code:
I’ve used the sparkfun ICM20948 library as well as the Adafruit ICM20X libraries and I’ve run the example code from both of them (Example1 basics for sparkfun and the ICM_20948 code from the adafruit).
I’m getting either a value of 0 across all axes for the magnetometer or a random value that doesn’t change, even when a magnet is brought close to the sensor. All the other readings are working fine (gyro, accel, temp) except magnetometer. Does anyone know why this is happening?
Wiring:
Edit: The sensor that I thought was from adafruit was actually also another 3rd party sensor ( Amazon.com: CJLHENZM 1Pcs/lot TDK InvenSense ICM-20948 Module 9-DoF IMU-Stemma QT/Qwiic : Industrial & Scientific )
// Basic demo for accelerometer readings from Adafruit ICM20948
// adafruit_icm20948_test from Adafruit ICM20X library
#include <Adafruit_ICM20X.h>
#include <Adafruit_ICM20948.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_ICM20948 icm;
uint16_t measurement_delay_us = 65535; // Delay between measurements for testing
// For SPI mode, we need a CS pin
#define ICM_CS 10
// For software-SPI mode we need SCK/MOSI/MISO pins
#define ICM_SCK 13
#define ICM_MISO 12
#define ICM_MOSI 11
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit ICM20948 test!");
// Try to initialize!
if (!icm.begin_I2C()) {
// if (!icm.begin_SPI(ICM_CS)) {
// if (!icm.begin_SPI(ICM_CS, ICM_SCK, ICM_MISO, ICM_MOSI)) {
Serial.println("Failed to find ICM20948 chip");
while (1) {
delay(10);
}
}
Serial.println("ICM20948 Found!");
// icm.setAccelRange(ICM20948_ACCEL_RANGE_16_G);
Serial.print("Accelerometer range set to: ");
switch (icm.getAccelRange()) {
case ICM20948_ACCEL_RANGE_2_G:
Serial.println("+-2G");
break;
case ICM20948_ACCEL_RANGE_4_G:
Serial.println("+-4G");
break;
case ICM20948_ACCEL_RANGE_8_G:
Serial.println("+-8G");
break;
case ICM20948_ACCEL_RANGE_16_G:
Serial.println("+-16G");
break;
}
Serial.println("OK");
// icm.setGyroRange(ICM20948_GYRO_RANGE_2000_DPS);
Serial.print("Gyro range set to: ");
switch (icm.getGyroRange()) {
case ICM20948_GYRO_RANGE_250_DPS:
Serial.println("250 degrees/s");
break;
case ICM20948_GYRO_RANGE_500_DPS:
Serial.println("500 degrees/s");
break;
case ICM20948_GYRO_RANGE_1000_DPS:
Serial.println("1000 degrees/s");
break;
case ICM20948_GYRO_RANGE_2000_DPS:
Serial.println("2000 degrees/s");
break;
}
// icm.setAccelRateDivisor(4095);
uint16_t accel_divisor = icm.getAccelRateDivisor();
float accel_rate = 1125 / (1.0 + accel_divisor);
Serial.print("Accelerometer data rate divisor set to: ");
Serial.println(accel_divisor);
Serial.print("Accelerometer data rate (Hz) is approximately: ");
Serial.println(accel_rate);
// icm.setGyroRateDivisor(255);
uint8_t gyro_divisor = icm.getGyroRateDivisor();
float gyro_rate = 1100 / (1.0 + gyro_divisor);
Serial.print("Gyro data rate divisor set to: ");
Serial.println(gyro_divisor);
Serial.print("Gyro data rate (Hz) is approximately: ");
Serial.println(gyro_rate);
// icm.setMagDataRate(AK09916_MAG_DATARATE_10_HZ);
Serial.print("Magnetometer data rate set to: ");
switch (icm.getMagDataRate()) {
case AK09916_MAG_DATARATE_SHUTDOWN:
Serial.println("Shutdown");
break;
case AK09916_MAG_DATARATE_SINGLE:
Serial.println("Single/One shot");
break;
case AK09916_MAG_DATARATE_10_HZ:
Serial.println("10 Hz");
break;
case AK09916_MAG_DATARATE_20_HZ:
Serial.println("20 Hz");
break;
case AK09916_MAG_DATARATE_50_HZ:
Serial.println("50 Hz");
break;
case AK09916_MAG_DATARATE_100_HZ:
Serial.println("100 Hz");
break;
}
Serial.println();
}
void loop() {
// /* Get a new normalized sensor event */
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t mag;
sensors_event_t temp;
icm.getEvent(&accel, &gyro, &temp, &mag);
Serial.print("\t\tTemperature ");
Serial.print(temp.temperature);
Serial.println(" deg C");
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tAccel X: ");
Serial.print(accel.acceleration.x);
Serial.print(" \tY: ");
Serial.print(accel.acceleration.y);
Serial.print(" \tZ: ");
Serial.print(accel.acceleration.z);
Serial.println(" m/s^2 ");
Serial.print("\t\tMag X: ");
Serial.print(mag.magnetic.x);
Serial.print(" \tY: ");
Serial.print(mag.magnetic.y);
Serial.print(" \tZ: ");
Serial.print(mag.magnetic.z);
Serial.println(" uT");
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tGyro X: ");
Serial.print(gyro.gyro.x);
Serial.print(" \tY: ");
Serial.print(gyro.gyro.y);
Serial.print(" \tZ: ");
Serial.print(gyro.gyro.z);
Serial.println(" radians/s ");
Serial.println();
delay(100);
// Serial.print(temp.temperature);
//
// Serial.print(",");
//
// Serial.print(accel.acceleration.x);
// Serial.print(","); Serial.print(accel.acceleration.y);
// Serial.print(","); Serial.print(accel.acceleration.z);
//
// Serial.print(",");
// Serial.print(gyro.gyro.x);
// Serial.print(","); Serial.print(gyro.gyro.y);
// Serial.print(","); Serial.print(gyro.gyro.z);
//
// Serial.print(",");
// Serial.print(mag.magnetic.x);
// Serial.print(","); Serial.print(mag.magnetic.y);
// Serial.print(","); Serial.print(mag.magnetic.z);
// Serial.println();
//
// delayMicroseconds(measurement_delay_us);
}
Adafruit library that I used: GitHub - adafruit/Adafruit_ICM20X: Arduino library for ICM20X
Output:
Adafruit ICM20948 test!
ICM20948 Found!
Accelerometer range set to: +-16G
OK
Gyro range set to: 2000 degrees/s
Accelerometer data rate divisor set to: 20
Accelerometer data rate (Hz) is approximately: 53.57
Gyro data rate divisor set to: 10
Gyro data rate (Hz) is approximately: 100.00
Magnetometer data rate set to: Shutdown
Temperature 30.25 deg C
Accel X: -0.08 Y: -0.28 Z: 9.94 m/s^2
Mag X: 0.00 Y: 0.00 Z: 0.00 uT
Gyro X: -0.02 Y: 0.01 Z: 0.01 radians/s
Temperature 30.68 deg C
Accel X: -0.09 Y: -0.26 Z: 9.88 m/s^2
Mag X: 0.00 Y: 0.00 Z: 0.00 uT
Gyro X: -0.02 Y: 0.01 Z: 0.01 radians/s
Temperature 30.68 deg C
Accel X: -0.10 Y: -0.26 Z: 9.86 m/s^2
Mag X: 0.00 Y: 0.00 Z: 0.00 uT
Gyro X: -0.02 Y: 0.01 Z: 0.01 radians/s
Temperature 30.49 deg C
Accel X: -0.12 Y: -0.22 Z: 9.77 m/s^2
Mag X: 0.00 Y: 0.00 Z: 0.00 uT
Gyro X: -0.02 Y: 0.01 Z: 0.01 radians/s
Temperature 30.68 deg C
Accel X: -0.08 Y: -0.21 Z: 9.86 m/s^2
Mag X: 0.00 Y: 0.00 Z: 0.00 uT
Gyro X: -0.02 Y: 0.01 Z: 0.01 radians/s
Temperature 30.39 deg C
Accel X: -0.10 Y: -0.29 Z: 9.84 m/s^2
Mag X: 0.00 Y: 0.00 Z: 0.00 uT
Gyro X: -0.02 Y: 0.01 Z: 0.02 radians/s
Temperature 30.25 deg C
Accel X: -0.04 Y: -0.26 Z: 9.85 m/s^2
Mag X: 0.00 Y: 0.00 Z: 0.00 uT
Gyro X: -0.02 Y: 0.02 Z: 0.01 radians/s
Temperature 30.78 deg C
Accel X: -0.05 Y: -0.24 Z: 9.87 m/s^2
Mag X: 0.00 Y: 0.00 Z: 0.00 uT
Gyro X: -0.02 Y: 0.01 Z: 0.01 radians/s
Setup Picture:
