HMC5883L 3축 자계 모듈 테스트

1. HMC5883L

쿼드콥터 만들 때 MPU-6050 센서와 함께 사용하면 자세저어가 가능합니다. MPU-6050 에서 Z축 회전(Yawing, 정지상태에서 좌우회전 움직임)을 정확히 잡을 수 없으므로 HMC5883L 을 사용해서 보완할 수 있습니다.

2. 연결방법

  • Arduino GND -> HMC5883L GND
  • Arduino 3.3V -> HMC5883L VCC
  • Arduino A4 (SDA) -> HMC5883L SDA
  • Arduino A5 (SCL) -> HMC5883L SCL
  • You will also need to add two ‘pull-up’ resistors to enable I2C. For this, connect two 4.7k or 10k resistors between SDA and VCC, and SCL and VCC. (이 부분은 필요 없는듯)

3. 소스코드

3-1. 단순한 형태의 Raw 데이터 읽기/출력

http://sfecdn.s3.amazonaws.com/datasheets/Sensors/Magneto/HMC5883.pde

/*
An Arduino code example for interfacing with the HMC5883

by: Jordan McConnell
 SparkFun Electronics
 created on: 6/30/11
 license: OSHW 1.0, http://freedomdefined.org/OSHW

Analog input 4 I2C SDA
Analog input 5 I2C SCL
*/

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

#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup(){
  //Initialize Serial and I2C communications
  Serial.begin(9600);
  Wire.begin();

  //Put the HMC5883 IC into the correct operating mode
  Wire.beginTransmission(address); //open communication with HMC5883
  Wire.send(0x02); //select mode register
  Wire.send(0x00); //continuous measurement mode
  Wire.endTransmission();
}

void loop(){

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

  //Tell the HMC5883 where to begin reading data
  Wire.beginTransmission(address);
  Wire.send(0x03); //select register 3, X MSB register
  Wire.endTransmission();

 //Read data from each axis, 2 registers per axis
  Wire.requestFrom(address, 6);
  if(6<=Wire.available()){
    x = Wire.receive()<<8; //X msb
    x |= Wire.receive(); //X lsb
    z = Wire.receive()<<8; //Z msb
    z |= Wire.receive(); //Z lsb
    y = Wire.receive()<<8; //Y msb
    y |= Wire.receive(); //Y lsb
  }

  //Print out values of each axis
  Serial.print("x: ");
  Serial.print(x);
  Serial.print("  y: ");
  Serial.print(y);
  Serial.print("  z: ");
  Serial.println(z);

  delay(250);
}

3-2. HMC5883L 라이브러리 사용해서 작성하는 방법

http://bildr.org/2012/02/hmc5883l_arduino/

4. 동작방법

(작성중)

5. 참고자료

HMC5883L Compass tutorial with Arduino library : http://www.loveelectronics.co.uk/Tutorials/8/hmc5883l-tutorial-and-arduino-library

HMC5883L How to : http://bildr.org/2012/02/hmc5883l_arduino/

Digital compass How-to video

You may also like...