1.將Adruino的I2C pin A4,A5接到GY-273的SDA,SCL。GY-273的VCC可以直接接Arduino上的 5V,GY-273支援5~3V輸入。再連接上兩塊板子的GND,硬體設置便完成了。
2.找個blog[1]下載的Library,
3.在壓縮檔內還有一個範例程式HMC5883L_Example.ino,我們可以先用這個程式來驗証GY-273的功能。
4. 當你把程式成功upload到Arduino的板子後,再打開Serial Monitor視窗,就可以看到Arduino從HMC5883L讀到的數值。最左邊是三個軸的Raw data,中間的scaled是將Raw data轉成磁場強度,最右邊這排,便是將Raw data轉成磁角度。
在驗証Library的過程中,如果出現下面編輯的錯誤訊息
C:\Users\Documents\Arduino\libraries\HMC5883L\HMC5883L.cpp: In member function 'void HMC5883L::Write(int, int)':
C:\Users\Documents\Arduino\libraries\HMC5883L\HMC5883L.cpp:110: error: 'class TwoWire' has no member named 'send'
C:\Users\Documents\Arduino\libraries\HMC5883L\HMC5883L.cpp:111: error: 'class TwoWire' has no member named 'send'
C:\Users\Documents\Arduino\libraries\HMC5883L\HMC5883L.cpp: In member function 'uint8_t* HMC5883L::Read(int, int)':
C:\Users\Documents\Arduino\libraries\HMC5883L\HMC5883L.cpp:118: error: 'class TwoWire' has no member named 'send'
C:\Users\Documents\Arduino\libraries\HMC5883L\HMC5883L.cpp:129: error: 'class TwoWire' has no member named 'receive'
這是因為Arduino版本的問題,在舊版的arduino,Wire的函式是用send,receive指令來收發資料,但新的Arduino是用write,read。要修正這個問題,只要將 HMC5883L.cpp內的head檔
#include <WProgram.h>
改成下面這行, 就可以解決這個問題了。
#include <Arduino.h>
Reference
[1] http://bluelemonlabs.blogspot.tw/2013/08/arduino-simple-compass-with-hmc5883l.html
1 則留言:
贊!
張貼留言