Advertisement
microrobotics

Pololu MinIMU-9 Gyro, Accelerometer, and Compass - V5

Apr 22nd, 2023
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu MinIMU-9 v5 is a compact inertial measurement unit (IMU) that includes an LSM6DS33 3-axis gyro and 3-axis accelerometer and an LIS3MDL 3-axis magnetometer. The following example demonstrates how to use an Arduino to read the sensor values from the MinIMU-9 v5.
  3.  
  4. First, you will need to install the "LSM6" and "LIS3MDL" libraries by Pololu. Open the Arduino IDE, navigate to "Tools" > "Manage Libraries..." and search for "LSM6" and "LIS3MDL" by Pololu. Install the libraries, and then restart the Arduino IDE.
  5.  
  6. Next, connect the Pololu MinIMU-9 v5 to your Arduino as follows:
  7.  
  8. Connect the IMU's VDD pin to the Arduino 3.3V pin.
  9. Connect the IMU's GND pin to the Arduino GND pin.
  10. Connect the IMU's SDA pin to the Arduino A4 pin (or SDA pin if using an Arduino board with dedicated SDA/SCL pins).
  11. Connect the IMU's SCL pin to the Arduino A5 pin (or SCL pin if using an Arduino board with dedicated SDA/SCL pins).
  12. Now, upload the following code to your Arduino:
  13.  
  14. This code initializes the LSM6DS33 and LIS3MDL sensors using the Pololu libraries and reads the gyroscope, accelerometer, and magnetometer values in the loop(). The sensor values are then printed to the serial monitor every second.
  15.  
  16. After uploading the code to your Arduino and opening the serial monitor, you should see the gyroscope, accelerometer, and magnetometer values being printed.
  17.  
  18. You can modify the code to process the sensor values, implement sensor fusion algorithms, or control other devices based on the sensor readings according to your project requirements.
  19. */
  20.  
  21. #include <Wire.h>
  22. #include <LSM6.h>
  23. #include <LIS3MDL.h>
  24.  
  25. LSM6 imu;
  26. LIS3MDL compass;
  27.  
  28. void setup() {
  29.   Serial.begin(9600);
  30.   Wire.begin();
  31.  
  32.   if (!imu.init()) {
  33.     Serial.println("Failed to initialize LSM6DS33.");
  34.     while (1);
  35.   }
  36.   imu.enableDefault();
  37.  
  38.   if (!compass.init()) {
  39.     Serial.println("Failed to initialize LIS3MDL.");
  40.     while (1);
  41.   }
  42.   compass.enableDefault();
  43. }
  44.  
  45. void loop() {
  46.   imu.read();
  47.   compass.read();
  48.  
  49.   Serial.print("Gyroscope (dps): ");
  50.   Serial.print(imu.g.x); Serial.print(", ");
  51.   Serial.print(imu.g.y); Serial.print(", ");
  52.   Serial.print(imu.g.z); Serial.println();
  53.  
  54.   Serial.print("Accelerometer (g): ");
  55.   Serial.print(imu.a.x); Serial.print(", ");
  56.   Serial.print(imu.a.y); Serial.print(", ");
  57.   Serial.print(imu.a.z); Serial.println();
  58.  
  59.   Serial.print("Magnetometer (Gauss): ");
  60.   Serial.print(compass.m.x); Serial.print(", ");
  61.   Serial.print(compass.m.y); Serial.print(", ");
  62.   Serial.print(compass.m.z); Serial.println();
  63.  
  64.   Serial.println();
  65.   delay(1000);
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement