Using the ICM20948 with Arduino is a simple matter of wiring up the sensor to your Arduino-compatible microcontroller, installing the Adafruit ICM20X library we've written, and running the provided example code.
I2C Wiring
Use this wiring if you want to connect via I2C interface. The default I2C address for the ICM20948 is 0x69 but it can be switched to 0x68 by pulling the address pin low to GND.
Here is how to wire up the sensor using one of the STEMMA QT connectors. The examples show a Metro but wiring will work the same for an Arduino or other compatible board.
- Connect board VIN (red wire) to Arduino 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
- Connect board GND (black wire) to Arduino GND
- Connect board SCL (yellow wire) to Arduino SCL
- Connect board SDA (blue wire) to Arduino SDA
Here is how to wire the sensor to a board using a solderless breadboard:
- Connect board VIN (red wire) to Arduino 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
- Connect board GND (black wire) to Arduino GND
- Connect board SCL (yellow wire) to Arduino SCL
- Connect board SDA (blue wire) to Arduino SDA
Library Installation
You can install the Adafruit ICM20X library for Arduino using the Library Manager in the Arduino IDE. This library is compatible with both the ICM20948 and it's sister sensor, the ICM20649
Click the Manage Libraries ... menu item, search for Adafruit ICM20X, and select the Adafruit ICM20X library:
Follow the same process for the Adafruit BusIO library.
Finally follow the same process for the Adafruit Unified Sensor library:
After opening the demo file, upload to your Arduino wired up to the sensor. Once you upload the code, you will see the Temperature as well as X, Y, and Z values for the Gyro Accelerometer, and Magnetometer being printed when you open the Serial Monitor (Tools->Serial Monitor) at 115200 baud, similar to this:
// Basic demo for accelerometer readings from Adafruit ICM20948 #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); }
Text editor powered by tinymce.