3 Axis Digital Compass With Arduino
3 Axis Digital Compass With Arduino
Hardware Installation
1. For Arduino Uno connect SDA pin to analog input pin 4 and SCL to
analog pin 5
2. For Arduino Mega connect SDA to digital pin 20 and SCL to digital pin
21
Programming
he following sketch demonstrates a simple application of reading the angle
between X axis and South direction. To use / test this application, please keep
the compass on a leveled surface.
Algorithm:
Demo Sketch:
Please read the comments in the below demo program. All necessary operations
and logic involved with HMC5883 are explained in detail.
/*
3-Axis Compass Module (HMC5883) Demo code.
2010 Copyright (c) Seeed Technology Inc. All right
reserved.
This library is distributed in the hope that it will be
useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU
Lesser General Public License for more details.
#include <Wire.h>
#include <math.h>
int regb=0x01;
int regbdata=0x40;
int outputData[6];
void setup()
{
Serial.begin(9600);
Wire.begin();
//Initiate the Wire library and
join the I2C bus as a master
void loop() {
int i,x,y,z;
double angle;
Wire.beginTransmission(HMC5883_WriteAddress);
Wire.send(regb);
Wire.send(regbdata);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(HMC5883_WriteAddress); //
Initiate a transmission with HMC5883 (Write address).
Wire.send(HMC5883_ModeRegisterAddress);
//Place
the Mode Register Address in send-buffer.
Wire.send(HMC5883_ContinuousModeCommand);
//Place
the command for Continuous operation Mode in send-buffer.
Wire.endTransmission();
//Send
the send-buffer to HMC5883 and end the I2C transmission.
delay(100);
Wire.beginTransmission(HMC5883_WriteAddress); //
Initiate a transmission with HMC5883 (Write address).
Wire.requestFrom(HMC5883_WriteAddress,6);
//
Request 6 bytes of data from the address specified.
delay(500);
/*
----------------->y
|
|
|
|
|
|
\/
x
N
NW | NE
|
W----------E
|
SW | SE
S
*/