3 axis digital compass with arduino

7
 3 Axis Compass With Arduino 3-Axis Compass module, a member of Grove family uses I !C based Honeywell HMC5883L digital compass. This ASIC is equipped with high resolution HMC1 18X magneto-resistive sen sors and a 12-bit ADC. It provides compass heading accuracy up to 1° to 2°. Signal conditioning like amplication, automatic degaussing strap drivers and offset cancellation are inbuilt. This module also includes a MIC5205-3.3 for power supply requirement. Hence user can connect any 3.3V to 6V DC power supply. Hardware Installation 1. For Arduino Uno connec t SDA pin to analog input pi n 4 and SCL to analog pin 5 2. For Arduino Mega connect SDA to digital pin 20 and SCL to digital pin 21  Future Electronics Egypt Ltd. (Arduino Egypt).

Upload: andrelvo

Post on 03-Nov-2015

6 views

Category:

Documents


0 download

DESCRIPTION

arduino compass

TRANSCRIPT

  • 3 Axis Compass With Arduino

    3-Axis Compass module, a member of Grove family uses IC based Honeywell HMC5883L digital compass. This ASIC is equipped with high resolution HMC118X magneto-resistive sensors and a 12-bit ADC. It provides compass heading accuracy up to 1 to 2. Signal conditioning like amplification, automatic degaussing strap drivers and offset cancellation are inbuilt. This module also includes a MIC5205-3.3 for power supply requirement. Hence user can connect any 3.3V to 6V DC power supply.

    Hardware Installation1. For Arduino Uno connect SDA pin to analog input pin 4 and SCL to

    analog pin 52. For Arduino Mega connect SDA to digital pin 20 and SCL to digital pin

    21

    Future Electronics Egypt Ltd. (Arduino Egypt).

  • 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:

    Include wire.h function in your program IC connection is established with HMC5883 Mode resister of HMC5883 is set to Continuous Measurement Mode. The values from Data Output registers are read. The angle between X axis and South direction is calculated

    mathematically.

    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.Original Author: LeoContribution: Visweswara R ( Code Redesign and Comments)This demo code is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.

    Future Electronics Egypt Ltd. (Arduino Egypt).

  • This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USAFor more details about the product please check http://www.seeedstudio.com/depot/*/#include #include // Shift the device's documented slave address (0x3C) for write operation// 1 bit right.This compensates for how the TWI library only wants the// 7 most significant bits (with the high bit padded with 0)#define HMC5883_WriteAddress 0x1E // i.e 0x3C >> 1#define HMC5883_ModeRegisterAddress 0x02#define HMC5883_ContinuousModeCommand 0x00#define HMC5883_DataOutputXMSBAddress 0x03int regb=0x01;int regbdata=0x40;int outputData[6];

    Future Electronics Egypt Ltd. (Arduino Egypt).

  • 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); //Read the value of magnetic components X,Y and Z

    Future Electronics Egypt Ltd. (Arduino Egypt).

  • if(6
  • ----------------->y | | | | | | \/ x N NW | NE | W----------E | SW | SE S */ //Print the approximate direction Serial.print("You are heading "); if((angle < 22.5) || (angle > 337.5 )) Serial.print("South"); if((angle > 22.5) && (angle < 67.5 )) Serial.print("South-West"); if((angle > 67.5) && (angle < 112.5 )) Serial.print("West"); if((angle > 112.5) && (angle < 157.5 )) Serial.print("North-West"); if((angle > 157.5) && (angle < 202.5 )) Serial.print("North"); if((angle > 202.5) && (angle < 247.5 )) Serial.print("NorthEast"); if((angle > 247.5) && (angle < 292.5 )) Serial.print("East");

    Future Electronics Egypt Ltd. (Arduino Egypt).

  • if((angle > 292.5) && (angle < 337.5 )) Serial.print("SouthEast"); Serial.print(": Angle between X-axis and the South direction "); if((0 < angle) && (angle < 180) ) { angle=angle; } else { angle=360-angle; } Serial.print(angle,2); Serial.println(" Deg"); delay(100);}

    The results should look like this:

    Future Electronics Egypt Ltd. (Arduino Egypt).