下面是 GY-271 廠商的描述
用途:測量地磁方向,測量物體靜止時候的方向,測量傳感器周圍磁力線的方向。註意,測量地磁時候容易受到周圍磁場影響,主芯片HMC5883三軸磁阻傳感器特點:
1,數字量輸出:I2C數字量輸出接口,設計使用非常方便。
2,尺寸小: 3x3x0.9mm LCC封裝,適合大規模量產使用。
3,精度高:1-2度,內置12位A/D,OFFSET, SET/RESET 電路,不會出現磁飽和現象,不會有累加誤差。
4,支持自動校準程序,簡化使用步驟,終端產品使用非常方便。
5,內置自測試電路,方便量產測試,無需增加額外昂貴的測試設備。
6,功耗低:供電電壓1.8V, 功耗睡眠模式-2.5uA 測量模式-0.6mA
連接:
只要連接VCC,GND,SDA,SCL四條線。
Arduino GY-271
-------------------------
VCC VCC
GND GND
A4 SDA
A5 SCL
P.S.我這塊 GY-271 是可以輸入 5V DC,但有的只能輸入3.3V DC,這個要看廠家的說明。
請注意:
HMC5883L.cpp 中必須更改下列字句,不然編譯會出錯,這是新版Arduino IDE變動的關係。
WProgram.h -> Arduino.h
Wire.send() -> Wire.wirte()
Wire.receice() -> Wire.read()
程式碼:
/*
HMC5883L_Example.pde - Example sketch for integration with an HMC5883L triple axis magnetomerwe.
Copyright (C) 2011 Love Electronics (loveelectronics.co.uk)
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include "HMC5883L.h"
// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;
// Out setup routine, here we will configure the microcontroller and compass.
void setup(){
// Initialize the serial port.
Serial.begin(9600);
Serial.println("Starting the I2C interface.");
Wire.begin(); // Start the I2C interface.
Serial.println("Constructing new HMC5883L");
compass = HMC5883L(); // Construct a new HMC5883 compass.
Serial.println("Setting scale to +/- 1.3 Ga");
error = compass.SetScale(1.3); // Set the scale of the compass.
// If there is an error, print it out.
if(error != 0) Serial.println(compass.GetErrorText(error));
// Set the measurement mode to Continuous
Serial.println("Setting measurement mode to continous.");
error = compass.SetMeasurementMode(Measurement_Continuous);
// If there is an error, print it out.
if(error != 0) Serial.println(compass.GetErrorText(error));
}
// Our main program loop.
void loop(){
// Retrive the raw values from the compass (not scaled).
MagnetometerRaw raw = compass.ReadRawAxis();
// Retrived the scaled values from the compass (scaled to the configured scale).
MagnetometerScaled scaled = compass.ReadScaledAxis();
// Values are accessed like so:
int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(scaled.YAxis, scaled.XAxis);
// 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: 2� 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
float declinationAngle = 0.0457;
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;
// Output the data via the serial port.
Output(raw, scaled, heading, headingDegrees);
// Normally we would delay the application by 66ms to allow the loop
// to run at 15Hz (default bandwidth for the HMC5883L).
// However since we have a long serial out (104ms at 9600) we will let
// it run at its natural speed.
// delay(66);
}
// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees){
Serial.print("Raw:(x,y,z)=( ");
Serial.print(raw.XAxis);
Serial.print(", ");
Serial.print(raw.YAxis);
Serial.print(", ");
Serial.print(raw.ZAxis);
Serial.print(") Scaled:(x,y,z)=( ");
Serial.print(scaled.XAxis);
Serial.print(", ");
Serial.print(scaled.YAxis);
Serial.print(", ");
Serial.print(scaled.ZAxis);
Serial.print(") Heading: ");
Serial.print(heading);
Serial.print(" Radians: ");
Serial.print(headingDegrees);
Serial.println(" Degrees.");
delay(500);
}
執行結果
它只負責讀出三軸方向磁場強度,而角度要自己去算。
下載 HMC5883L LIB
https://www.dropbox.com/s/h062nnde78zts1l/HMC5883L.zip
參考資料
Arduino連接HMC5883L三軸電子羅盤傳感器
http://www.geek-workshop.com/thread-105-1-1.html
您好:
回覆刪除請教一下,heading是否是指將磁sensor的X-Y軸平面放在水平的手掌上, 就好像你手掌上放一個普通文具店買的羅盤一樣, 算出的heading就是圍繞你四周360度環繞?
作者已經移除這則留言。
回覆刪除你好:
回覆刪除你那個Wire.send() -> Wire.wirte()打錯囉
是write()不是 wirte()