Advertisement
pleasedontcode

"Gyroscope Initialization" rev_03

Jun 5th, 2024
446
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: "Gyroscope Initialization"
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-06-05 22:24:38
  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. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Wire.h>
  25. #include <MPU6050.h>  //https://github.com/electroniccats/mpu6050
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t sensor_MPU6050_Interrupt_PIN_D2 = 2;
  33.  
  34. /***** DEFINITION OF I2C PINS *****/
  35. const uint8_t sensor_MPU6050_I2C_PIN_SDA_A4 = A4;
  36. const uint8_t sensor_MPU6050_I2C_PIN_SCL_A5 = A5;
  37. const uint8_t sensor_MPU6050_I2C_SLAVE_ADDRESS = 0x68;  // MPU6050 I2C address
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. MPU6050 accelgyro(sensor_MPU6050_I2C_SLAVE_ADDRESS);  // Initialize MPU6050 object with I2C address
  41.  
  42. void setup(void)
  43. {
  44.     // Initialize I2C communication
  45.     Wire.begin(sensor_MPU6050_I2C_PIN_SDA_A4, sensor_MPU6050_I2C_PIN_SCL_A5);
  46.    
  47.     // Initialize serial communication
  48.     Serial.begin(38400);
  49.     Serial.println("Initializing I2C devices...");
  50.    
  51.     // Initialize MPU6050
  52.     accelgyro.initialize();
  53.    
  54.     // Test connection
  55.     Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  56.    
  57.     // Set interrupt pin as input
  58.     pinMode(sensor_MPU6050_Interrupt_PIN_D2, INPUT);
  59. }
  60.  
  61. void loop(void)
  62. {
  63.     // Variables to hold gyroscope data
  64.     int16_t gx, gy, gz;
  65.    
  66.     // Read gyroscope data
  67.     accelgyro.getRotation(&gx, &gy, &gz);
  68.  
  69.     // Print gyroscope data to serial monitor
  70.     Serial.print("Gyroscope X: "); Serial.print(gx);
  71.     Serial.print(" | Gyroscope Y: "); Serial.print(gy);
  72.     Serial.print(" | Gyroscope Z: "); Serial.println(gz);
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement