Kumpulan Tutorial, Referensi dan Project Arduino Open Source
Minggu, 18 Desember 2016
GY-87 : Tutorial dan Sample Code Penggunaan Sensor GY-87
Tutorial dan Sample Code Penggunaan Sensor GY-87 pada Arduino
Sensor GY-87 sebuah device i2C gabungan dari 3 buah Sensor, yaitu :
MPU6050 : Accelerometer Sensor (Address is 0x68)
HMC5883L : Digital Compass Sensor (Address is 0x1E) BMP180 : Barometer & Temperature Sensor (Address is 0x77)
Dari sebuah sensor GY-87 kita dapat memperoleh Out data berupa :
*3 Axis Accelerometer and rotation (x,y,z) -> from MPU6050 device
*Compass Data/Dirrection (angle) -> from HMC5883L device
*Air Pressure, Altimeter and Temperature -> from BMP180 device
Berikut adalah Contoh Source Code untuk menampilkan data dari Sensor GY-87 ke Serial Monitor Arduino IDE
Pasang Sensor GY-87 pada Arduino sesuai Wiring yang ada diatas,
Pada Arduino Uno/Nano/Pro mini, pin SDA berada pada pin A4 dan SCL pada pin A5
Download semua library yang dibutuhkan pada tombol download di atas kemudian copy paste source code dibawah ini dan Upload ke dalam Arduino anda.
Setelah selesai upload buka Serial Monitor pada Arduino IDE pada baudrate 9600 dan pastikan semua data meampilkan value/nilai.
Source Code:
/*GY87_Readerv0.inoThis sketch is based on various sketches and libraries by Jeff Rowberg<jeff@rowberg.net> and discussion thread by pistolero992000 on the Arduino forumhttps://forum.arduino.cc/index.php?topic=223345.0. The BMP180 pressureSenor library is from Love Electronics Ltd (loveelectronics.com)http://embedded-lab.com/blog/bmp180/bmp180_11/It gets all sensors on a GY87 IMU board reporting (something) to theSerial. The challenge is the Digital Compass which is nromally blocked bythe Accelerometer. This is remedied by using an I2C bypass command. Thanks topistolero992000 for this solution.Sensors on board the GY87 are:MPU6050 Accelerometer. Address is 0x68HMC5883L Digital Compass. Address is 0x1EBMP180 Barometer and Temperature Sensor. Address is 0x77Connections are through the i2c bus.SCL to Arduino Pin A5SDA to Arduino Pin A4GND, 5V and 3.3V connections also present.All sensors report sensible values. The tab seperated data covers a lot of screen spaceso you will need to stretch your Serial monitor window to accommodate it.*/#include "I2Cdev.h"#include "MPU6050.h"#include "Wire.h"#include <BMP180.h> //Library for the BMP180 barometer.//MPU6050 Accelerometer MPU6050accelgyro;int16_tax,ay,az;int16_tgx,gy,gz;//HMC5883L Digital Compassconstinthmc5883Address=0x1E;//0011110b, I2C 7bit address for compassconstbytehmc5883ModeRegister=0x02;constbytehmcContinuousMode=0x00;constbytehmcDataOutputXMSBAddress=0x03;//The BMP180 Digital BarometerBMP180barometer;// Store the current sea level pressure at your location in Pascals.floatseaLevelPressure=101325;intLEDPin=13;boolblinkState=false;intx,y,z;//triple axis data from HMC5883L.voidsetup(){Wire.begin();Serial.begin(9600);// initialize deviceSerial.println("Initializing I2C devices...");accelgyro.initialize();// verify connectionSerial.println("Testing device connections...");Serial.println(accelgyro.testConnection()?"MPU6050 connection successful":"MPU6050 connection failed");accelgyro.setI2CBypassEnabled(true);//This sets the bypass so the HMC5883L gets a look in.//Initialise the Digital CompassWire.beginTransmission(hmc5883Address);//Begin communication with compassWire.write(hmc5883ModeRegister);//select the mode registerWire.write(hmcContinuousMode);//continuous measurement modeWire.endTransmission();//Initialise the BMP180 Barometer (and Temperature Sensor)barometer=BMP180();// We check to see if we can connect to the BMP180 sensor.if(barometer.EnsureConnected()){Serial.println("Connected to BMP180.");// When we have connected, we reset the device to ensure a clean start.barometer.SoftReset();// Now we initialize the sensor and pull the calibration data.barometer.Initialize();}else{Serial.println("No BMP180 sensor found.");}// configure Arduino LED forpinMode(LEDPin,OUTPUT);delay(10000);}voidloop(){// read raw accel/gyro measurements from deviceaccelgyro.getMotion6(&ax,&ay,&az,&gx,&gy,&gz);// these methods (and a few others) are also available//accelgyro.getAcceleration(&ax, &ay, &az);//accelgyro.getRotation(&gx, &gy, &gz);// display tab-separated accel/gyro x/y/z valuesSerial.print("a/g:\t");Serial.print(ax);Serial.print("\t");Serial.print(ay);Serial.print("\t");Serial.print(az);Serial.print("\t");Serial.print(gx);Serial.print("\t");Serial.print(gy);Serial.print("\t");Serial.print(gz);Serial.print("\t");// blink LED to indicate activityblinkState=!blinkState;digitalWrite(LEDPin,blinkState);//Accessing the HMC5883L Digital Compass //Tell the HMC5883L where to begin reading the dataWire.beginTransmission(hmc5883Address);Wire.write(hmcDataOutputXMSBAddress);//Select register 3, X MSB registerWire.endTransmission();//Read data from each axis of the Digital CompassWire.requestFrom(hmc5883Address,6);if(6<=Wire.available()){x=Wire.read()<<8;//X msbx|=Wire.read();//X lsbz=Wire.read()<<8;//Z msbz|=Wire.read();//Z lsby=Wire.read()<<8;//Y msby|=Wire.read();//Y lsb }intangle=atan2(-y,x)/M_PI*180;if(angle<0){angle=angle+360;}//Reporting the Compass data to the Serial port//Serial.print("Compass XYZ:\t");//Serial.print(x,y,z);Serial.print("\t");Serial.print("Dir(deg):\t");Serial.print(angle);Serial.print("\t");if(barometer.IsConnected){longcurrentPressure=barometer.GetPressure();// Print out the Pressure.Serial.print("BMP180 P:\t");Serial.print(currentPressure);Serial.print("Pa");Serial.print("\t");// Retrieve the current altitude (in meters). Current Sea Level Pressure is required for this.floataltitude=barometer.GetAltitude(seaLevelPressure);// Print out the Altitude.Serial.print("Alt:\t");Serial.print(altitude);Serial.print(" m");Serial.print("\t");// Retrieve the current temperature in degrees celcius.floatcurrentTemperature=barometer.GetTemperature();// Print out the TemperatureSerial.print("Temp:\t");Serial.print(currentTemperature);Serial.println("C");}}
Pengaplikasiannya dimana ya
BalasHapushello,
BalasHapusi use this code but compass dont work,its for 5883 cheat problem?
a/g: -852 26060 15032 -199 8 109 Dir(deg): 0 BMP180 P: 100741Pa Alt: 48.23 m Temp: 24.90C
a/g: -888 26064 15204 -184 -40 82 Dir(deg): 0 BMP180 P: 100742Pa Alt: 48.74 m Temp: 24.90C
a/g: -732 26008 15124 -207 -26 91 Dir(deg): 0 BMP180 P: 100750Pa Alt: 47.48 m Temp: 24.90C
a/g: -784 26124 15136 -205 -18 118 Dir(deg): 0 BMP180 P: 100741Pa Alt: 47.98 m Temp: 24.90C