SHARE
TWEET

Untitled

a guest Dec 5th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>  
  2. #include <Wire.h>  // Wire library - used for I2C communication
  3. int Contrast=133;
  4. int bright = 50;
  5.  
  6. /////////////////////////
  7. const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
  8.  
  9. int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
  10. int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
  11. int16_t temperature; // variables for temperature data
  12.  
  13. char tmp_str[7]; // temporary variable used in convert function
  14.  
  15. char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
  16.   sprintf(tmp_str, "%6d", i);
  17.   return tmp_str;
  18. }
  19.  
  20. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  
  21.  
  22.  void setup()
  23.  {
  24.     analogWrite(6,Contrast);
  25.      lcd.begin(16, 2);
  26.      analogWrite(10, bright);
  27. /////////////////////////
  28.   Serial.begin(4800);
  29.   Wire.begin();
  30.   Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
  31.   Wire.write(0x6B); // PWR_MGMT_1 register
  32.   Wire.write(0); // set to zero (wakes up the MPU-6050)
  33.   Wire.endTransmission(true);
  34.      
  35.   }  
  36.  
  37. void loop()
  38.  {  
  39.  
  40.  lcd.begin(2,16);              
  41.  lcd.clear();                
  42.  lcd.setCursor(0,0);  
  43.  lcd.print("X= ");
  44.  lcd.print(accelerometer_x);  
  45.  lcd.setCursor(10,0);          
  46.  lcd.print("Y= ");
  47.  lcd.print(accelerometer_y);
  48.  lcd.setCursor(5,1);          
  49.  lcd.print("Z= ");
  50.  lcd.print(accelerometer_z);
  51.  delay(100);
  52.  
  53. /////////////////////////
  54.   Wire.beginTransmission(MPU_ADDR);
  55.   Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
  56.   Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
  57.   Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers
  58.  
  59.   // "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
  60.   accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  61.   accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  62.   accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  63.   temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  64.   gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  65.   gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  66.   gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
  67.  
  68.   // print out data
  69.   Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
  70.   Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
  71.   Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));
  72.   // the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
  73.   Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53);
  74.   Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
  75.   Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
  76.   Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));
  77.   Serial.println();
  78.  
  79.   // delay
  80.   delay(1000);
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top