Advertisement
microrobotics

Pololu MinIMU-9 v3

Apr 5th, 2023
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu MinIMU-9 v3 is a compact inertial measurement unit (IMU) that combines a 3-axis gyroscope, 3-axis accelerometer, and 3-axis magnetometer. This module uses the L3GD20H gyroscope, the LSM303D accelerometer/magnetometer, and communicates over I2C. Here's an example using the LSM303 and L3GD20 libraries with an Arduino:
  3.  
  4. Install the required libraries:
  5.  
  6. LSM303: https://github.com/pololu/lsm303-arduino
  7. L3GD20: https://github.com/pololu/L3G-arduino
  8. Upload the following code to your Arduino board:
  9.  
  10. This code initializes the LSM303D accelerometer/magnetometer and L3GD20H gyroscope, reads their data, and prints the values to the Serial monitor. Connect the MinIMU-9 v3's SDA, SCL, VCC, and GND pins to the corresponding pins on your Arduino board.
  11. */
  12.  
  13. #include <Wire.h>
  14. #include <LSM303.h>
  15. #include <L3G.h>
  16.  
  17. LSM303 compass;
  18. L3G gyro;
  19.  
  20. void setup() {
  21.   Serial.begin(9600);
  22.   Wire.begin();
  23.  
  24.   // Initialize the compass (accelerometer and magnetometer)
  25.   if (!compass.init()) {
  26.     Serial.println("Failed to detect and initialize compass!");
  27.     while (1);
  28.   }
  29.   compass.enableDefault();
  30.  
  31.   // Initialize the gyroscope
  32.   if (!gyro.init()) {
  33.     Serial.println("Failed to detect and initialize gyro!");
  34.     while (1);
  35.   }
  36.   gyro.enableDefault();
  37. }
  38.  
  39. void loop() {
  40.   // Read the accelerometer and magnetometer
  41.   compass.read();
  42.  
  43.   // Read the gyroscope
  44.   gyro.read();
  45.  
  46.   // Print the accelerometer data
  47.   Serial.print("Accel (X, Y, Z): ");
  48.   Serial.print(compass.a.x); Serial.print(", ");
  49.   Serial.print(compass.a.y); Serial.print(", ");
  50.   Serial.println(compass.a.z);
  51.  
  52.   // Print the magnetometer data
  53.   Serial.print("Mag (X, Y, Z): ");
  54.   Serial.print(compass.m.x); Serial.print(", ");
  55.   Serial.print(compass.m.y); Serial.print(", ");
  56.   Serial.println(compass.m.z);
  57.  
  58.   // Print the gyroscope data
  59.   Serial.print("Gyro (X, Y, Z): ");
  60.   Serial.print(gyro.g.x); Serial.print(", ");
  61.   Serial.print(gyro.g.y); Serial.print(", ");
  62.   Serial.println(gyro.g.z);
  63.  
  64.   Serial.println();
  65.  
  66.   delay(500);
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement