GR-ADZUKI MAGNETOMETER

Pocket

GR-ADZUKIには、基板上に加速度センサー(アナログ出力)、デジタルコンパス(I2C通信)用DIPピンソケットが装備されています。
今回は、デジタルコンパスを使用してみます。

3軸ディジタルコンパスモジュール HMC5883L
http://akizukidenshi.com/catalog/g/gM-06820/
(デジタルコンパスは、販売終了か、今取扱いできない商品です。)

データシート
https://cdn-shop.adafruit.com/datasheets/HMC5883L_3-Axis_Digital_Compass_IC.pdf#search=%27HMC5338L%27

2つのソケットのうち照度センサ側のソケットが磁気センサーのソケットになります。
磁気センサモジュールは、ピンのはんだ付け必要です。
ピンのはんだ付けが済んだら、向きを間違えないように差し込みます。
ソケットの凹みがある側とモジュール基板上の凹みの絵がある側を揃えて差し込みます。
今回はI2C通信を使用します。
ピン配置は、
7ピン SCL クロック
8ピン SDA データ
となっています。

プログラムの説明をします。
まずI2Cライブラリを使用するために
#include <Wire.h> //I2C Arduino Library
を宣言します。

setup()関数でシリアル通信の開始を行っています。
Serial.begin(9600); // start serial for output
次にI2C通信の開始を行います。
Wire.begin(); // join i2c bus (address optional for master)

続いて初期化を行います。
Wire.beginTransmission(address)
これでセンサーのアドレスを設定ます。
レジスタ0x02を0x00に設定します。この設定で連続測定モードになります。
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(0x1E); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();

loop()関数でデータを読み込みます。
X軸レジスタxを設定します。
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(0x1E);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();

6バイト読み出します。
//Read data from each axis, 2 registers per axis
Wire.requestFrom(0x1E, 6);
if(6<=Wire.available())
{
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}

X軸とY軸の角度から方位を計算します。
float rad = atan2(y,x);

ラジアン単位を角度単位に変換します。
float degree = rad * 180 / M_PI;
if(degree <0){
degree = 360 + degree;
}

最後に結果をシリアルモニタに送ります。
//Print out values of each axis
Serial.print(“x: “);
Serial.print(x);
Serial.print(” y: “);
Serial.print(y);
Serial.print(” z: “);
Serial.print(z);
Serial.print(” degree: “);
Serial.println(degree);

250ms毎に繰り返します。
delay(250);

サンプルプログラムは以下にあります。
https://github.com/jendo1969/GR-ADZUKI2/blob/master/compass/compass.ino

GR-ADZUKI is equipped with a DIP pin socket for acceleration sensor (analog output) and digital compass (I2C communication) on the board.
This time I will try using the digital compass.

3 Axis Digital Compass Module HMC5883L
http://akizukidenshi.com/catalog/g/gM-06820/
(The digital compass is a product that can not be handled now, because it is sold out.)

Data sheet
https://cdn-shop.adafruit.com/datasheets/HMC5883L_3-Axis_Digital_Compass_IC.pdf#search=%27HMC5338L%27

Of the two sockets, the socket on the illuminance sensor side becomes the socket of the magnetic sensor.
Magnetic sensor module requires pin soldering.
After soldering the pin, insert it so as not to mistake the direction.
Insert the side where the socket is dented and the side with the indentation on the module board aligned.
This time I will use I2C communication.
The pin arrangement is as follows.
7 pin SCL clock
8-pin SDA data

I will explain the program.
First of all, in order to use the I2C library, we make the following declaration.
#include <Wire.h> // I2C Arduino Library

Serial communication is started with setup () function.
Serial.begin(9600); // start serial for output
Next, start I2C communication.
Wire.begin(); // join i2c bus (address optional for master)

Then initialize it.
Wire.beginTransmission(address)
This sets the address of the sensor.
Set register 0x02 to 0x00. With this setting, continuous measurement mode is set.
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(0x1E); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();

Read data with the loop () function.
Set X axis register x.
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(0x1E);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();

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

Calculate the bearing from the angle of X axis and Y axis.
float rad = atan2(y,x);

Convert radians to angle units.
float degree = rad * 180 / M_PI;
if(degree <0){
degree = 360 + degree;
}

Finally, we send the result to the serial monitor.
//Print out values of each axis
Serial.print(“x: “);
Serial.print(x);
Serial.print(” y: “);
Serial.print(y);
Serial.print(” z: “);
Serial.print(z);
Serial.print(” degree: “);
Serial.println(degree);

Repeat every 250 ms.
delay(250);

The sample program is below.
https://github.com/jendo1969/GR-ADZUKI2/blob/master/compass/compass.ino

GR-ADZUKI在板上配有用於加速度傳感器(模擬輸出)和數字羅盤(I2C通信)的DIP引腳插座。
這次我會嘗試使用數字指南針。

3軸數字羅盤模塊 HMC5883L
http://akizukidenshi.com/catalog/g/gM-06820/
(數字羅盤是一種現在無法處理的產品,因為它已經售罄了。)

數據表
https://cdn-shop.adafruit.com/datasheets/HMC5883L_3-Axis_Digital_Compass_IC.pdf#search=%27HMC5338L%27

在這兩個插座中,照度傳感器側的插座成為磁性傳感器的插座。
磁性傳感器模塊需要引腳焊接。
在焊接引腳之後,將其插入以免錯誤方向。
將插座凹陷的一側和模塊板上缺口的一側對齊。
這次我將使用I2C通信。
引腳排列如下。
7引腳SCL時鐘
8引腳SDA數據

我會解釋一下這個程序。
首先聲明以下使用I2C庫。

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

串行通信使用setup()函數啟動。
Serial.begin(9600); // start serial for output
接下來,開始I2C通信。
Wire.begin(); // join i2c bus (address optional for master)

然後初始化它。
Wire.beginTransmission(address)
這設置了傳感器的地址。
將寄存器0x02設置為0x00。 使用此設置,將設置連續測量模式。
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(0x1E); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();

用loop()函數讀取數據。
設置X軸寄存器x。
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(0x1E);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();

讀取6個字節。
//Read data from each axis, 2 registers per axis
Wire.requestFrom(0x1E, 6);
if(6<=Wire.available())
{
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}

從X軸和Y軸的角度計算軸承。
float rad = atan2(y,x);

將弧度轉換為角度單位。
float degree = rad * 180 / M_PI;
if(degree <0){
degree = 360 + degree;
}

最後,我們將結果發送到串行監視器。
//Print out values of each axis
Serial.print(“x: “);
Serial.print(x);
Serial.print(” y: “);
Serial.print(y);
Serial.print(” z: “);
Serial.print(z);
Serial.print(” degree: “);
Serial.println(degree);

每250毫秒重複一次。
delay(250);

示例程序如下。
https://github.com/jendo1969/GR-ADZUKI2/blob/master/compass/compass.ino

Post Tagged with , ,

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください