Translate

Mengakses dan Membandingkan Dua Sensor Compass / Magnetometer GY-271 QMC8553L dan HMC8553L GY-273 Menggunakan ARDUINO Sensor Rotasi dan Arah Mata Angin

Mengakses dan Membandingkan Dua Sensor Compass / Magnetometer GY-271 QMC8553L dan HMC8553L GY-273 Menggunakan ARDUINO Sensor Rotasi dan Arah Mata Angin


       Pada kesempatan yang berbahagia kali ini saya akan menjelaskan mengenai bagaimana cara mengakses sensor compass atau magnetometer untuk mengetahui arah rotasi dan arah mata angin, sensor yang akan dipakai yaitu GY-271 dan GY-273 dengan tampilan interface menggunakan lcd 16x2. untuk lebih jelasnya berikut adalah program dan komponennya. 




a. Arduino Uno





b. Sensor GY-271 qmc8553l




c. Sensor GY-273 hmc8553l




d. LCD 16x2 + I2C






e. Program Arduino IDE GY-271

#include <Wire.h> //I2C Arduino Library
#include <LiquidCrystal_I2C.h>

#define addr 0x0D //I2C Address for The HMC5883
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  lcd.begin();
  lcd.clear();
  lcd.noCursor();
  
  Serial.begin(9600);
  Wire.begin();


  Wire.beginTransmission(addr); //start talking
  Wire.write(0x0B); // Tell the HMC5883 to Continuously Measure
  Wire.write(0x01); // Set the Register
  Wire.endTransmission();
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x09); // Tell the HMC5883 to Continuously Measure
  Wire.write(0x1D); // Set the Register
  Wire.endTransmission();
}

void loop() {

  int x, y, z; //triple axis data

  //Tell the HMC what regist to begin writing data into


  Wire.beginTransmission(addr);
  Wire.write(0x00); //start with register 3.
  Wire.endTransmission();

  //Read the data.. 2 bytes for each axis.. 6 total bytes
  Wire.requestFrom(addr, 6);
  if (6 <= Wire.available()) {
    x = Wire.read(); //MSB  x
    x |= Wire.read() << 8; //LSB  x
    z = Wire.read(); //MSB  z
    z |= Wire.read() << 8; //LSB z
    y = Wire.read(); //MSB y
    y |= Wire.read() << 8; //LSB y
  }

  // Show Values
  Serial.print("X Value: ");
  Serial.println(x);
  Serial.print("Y Value: ");
  Serial.println(y);
  Serial.print("Z Value: ");
  Serial.println(z);
  Serial.println();

  lcd.setCursor(0,0);
  lcd.print("");
  lcd.print(x);
  lcd.print("/");
  lcd.print(y);
  lcd.print("/");
  lcd.print(z);
  lcd.print("      ");

  delay(500);
}





f. Program Arduino IDE GY-273

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include <LiquidCrystal_I2C.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
LiquidCrystal_I2C lcd(0x27,16,2);

void displaySensorDetails(void)
{
  sensor_t sensor;
  mag.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

void setup(void) 
{
  lcd.begin();
  lcd.clear();
  lcd.noCursor();
  
  Serial.begin(9600);
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
  
  /* Display some basic information on this sensor */
  displaySensorDetails();
}

void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mag.getEvent(&event);

  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");

  //lcd.setCursor(0,0);
  //lcd.print(""); lcd.print(event.magnetic.x,1); lcd.print("/");
  //lcd.print(""); lcd.print(event.magnetic.y,1); lcd.print("/");
  //lcd.print(""); lcd.print(event.magnetic.z,1); lcd.print("   ");Serial.println("uT");

  // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.22;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 

  
  Serial.print("Heading (degrees): "); Serial.println(headingDegrees);

  lcd.setCursor(0,0);
  lcd.print("Sudut: "); lcd.print(headingDegrees); lcd.print("      ");

  delay(500);
}






g. VIDEO HASILNYA








No comments:

Post a Comment