I tried the following code to communicate via SPI with MPU9250 and Mega2560 But I got the following error: Error initializing communication with IMU
Why I got this error?
/*
* Brian R Taylor
* [email protected]
*
* Copyright (c) 2021 Bolder Flight Systems Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "mpu9250.h"
/* Mpu9250 object, SPI bus, CS on pin 10 */
bfs::Mpu9250 imu(&SPI, 10);
void setup() {
/* Serial to display data */
Serial.begin(115200);
while(!Serial) {}
/* Start the SPI bus */
SPI.begin();
/* Initialize and configure IMU */
if (!imu.Begin()) {
Serial.println("Error initializing communication with IMU");
while(1) {}
}
/* Set the sample rate divider */
if (!imu.ConfigSrd(19)) {
Serial.println("Error configured SRD");
while(1) {}
}
}
void loop() {
/* Check if data read */
if (imu.Read()) {
Serial.print(imu.new_imu_data());
Serial.print("\t");
Serial.print(imu.new_mag_data());
Serial.print("\t");
Serial.print(imu.accel_x_mps2());
Serial.print("\t");
Serial.print(imu.accel_y_mps2());
Serial.print("\t");
Serial.print(imu.accel_z_mps2());
Serial.print("\t");
Serial.print(imu.gyro_x_radps());
Serial.print("\t");
Serial.print(imu.gyro_y_radps());
Serial.print("\t");
Serial.print(imu.gyro_z_radps());
Serial.print("\t");
Serial.print(imu.mag_x_ut());
Serial.print("\t");
Serial.print(imu.mag_y_ut());
Serial.print("\t");
Serial.print(imu.mag_z_ut());
Serial.print("\t");
Serial.print(imu.die_temp_c());
Serial.print("\n");
}
}
Source code:
https://github.com/bolderflight/invensense-imu/blob/main/examples/arduino/mpu9250/spi/spi.ino
Connection:
MPU9250 Mega 2560
GND <.............> GND
3V3 <.............> 3.3V
SCL <.............> pin 52
SDA <.............> pin 51
SDC/SA0 <.............> pin 50
CSB <.............> pin 53
This is the sensor ( IMU 10DOF SENSOR MODULE MPU9250 BMP280 ( GY-91 )) But I only interested on the MODULE MPU9250 readings via SPI
Serial.println("Error initializing communication with IMU");
why does imu.Begin() fail?