Advertisement
pleasedontcode

"MPU6050 Initialization" rev_05

Jun 23rd, 2024
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "MPU6050 Initialization"
  13.     - Source Code compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-06-23 08:08:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read gyroscope at maximum rate and show on serial. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Wire.h>
  26. #include <MPU6050.h>  //https://github.com/electroniccats/mpu6050
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t sensor_MPU6050_Interrupt_PIN_D2 = 2;
  34.  
  35. /***** DEFINITION OF I2C PINS *****/
  36. const uint8_t sensor_MPU6050_I2C_PIN_SDA_A4 = A4;
  37. const uint8_t sensor_MPU6050_I2C_PIN_SCL_A5 = A5;
  38. const uint8_t sensor_MPU6050_I2C_SLAVE_ADDRESS = 104;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. MPU6050 accelgyro(sensor_MPU6050_I2C_SLAVE_ADDRESS); // Initialize MPU6050 with the specified I2C address
  42.  
  43. void setup(void) {
  44.     // put your setup code here, to run once:
  45.     Wire.begin(sensor_MPU6050_I2C_PIN_SDA_A4, sensor_MPU6050_I2C_PIN_SCL_A5); // Initialize I2C communication
  46.     Serial.begin(38400); // Initialize serial communication
  47.     Serial.println("Initializing I2C devices...");
  48.  
  49.     accelgyro.initialize(); // Initialize MPU6050
  50.     Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  51.  
  52.     pinMode(sensor_MPU6050_Interrupt_PIN_D2, INPUT); // Set interrupt pin as input
  53. }
  54.  
  55. void loop(void) {
  56.     // put your main code here, to run repeatedly:
  57.     int16_t ax, ay, az;
  58.     int16_t gx, gy, gz;
  59.  
  60.     // Read accelerometer and gyroscope data
  61.     accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  62.  
  63.     // Print the data to the Serial Monitor
  64.     Serial.print("a/g:\t");
  65.     Serial.print(ax); Serial.print("\t");
  66.     Serial.print(ay); Serial.print("\t");
  67.     Serial.print(az); Serial.print("\t");
  68.     Serial.print(gx); Serial.print("\t");
  69.     Serial.print(gy); Serial.print("\t");
  70.     Serial.println(gz);
  71.  
  72.     // Removed delay to read gyroscope at maximum rate
  73. }
  74.  
  75. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement