HEllo...
I have arduino mega and Going to use this magnet sensor.
However , When I run my program, always see 0.
like x=0,y=0,z=0
Could you tell me how could I overcom this problem??
#include <Wire.h> //I2C Arduino Library
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
void setup(){
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
}
void loop(){
int x,y,z; //triple axis data
//Tell the HMC5883L where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 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
}
//Print out values of each axis
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.println(z);
delay(50);
}
I put my pin in right place.
21and 20 are the right place in arduino mega.
/* Magnetometer (kompass) HMC5883L
* Programeksempel som henter rådata fra magnetometeret
* kompasskurs kan vi finne ved atan2-funksjonen
* (se https://www.arduino.cc/en/Math/H)
*
* Databladet sier at sensoren er 12-bits => 2^12 = 4096
* ..det betyr at vi vil få tall i området -2048 .. +2047
*/
#include <Wire.h> //I2C bibliotek
#define Mag 0x1E //I2C 7bit adresse
#define Mag_data 3 //adresse til X-register (det 1. av 6 bytes)
// se databladet for detaljer om disse definisjonene
#define CRA 0 // CRA = Control Register A
#define CRAdata B01001000 // bitene CRA7..CRA0
// #define CRB 1
#define CRBdata B00100000 // bitene CRB7..CRB0
// #define MR 2
#define MRdata B00000000 // bitene MR7..MR0
int x, y, z; //data fra de tre aksene
float xx,yy,zz;
void setup()
{
//Start kommunikasjon mot skjermen og I2C
Serial.begin(9600);
Wire.begin();
//Sett magnetometeret til 'oppførselen' vi vil ha
// se bl.a. side 11 i databladet om "register access"
Wire.beginTransmission(Mag); //start kommunikasjon not sensoren
Wire.write(CRA); // pek først til CRA. iflg.databladet økes peker automatisk
Wire.write(CRAdata); // derfor kan vi skrive til disse tre registerene etter hverandre
Wire.write(CRBdata);
Wire.write(MRdata);
Wire.endTransmission(); // kommunikasjon avsluttes
}
void loop()
{
//Pek til register med adresse 3. Der starter data fra magnetfeltet
Wire.beginTransmission(Mag); // start kommunikasjon
Wire.write(Mag_data); // sett peker til der dataene starter
Wire.endTransmission();
//Les inn tall fra de tre aksene x,y og z
Wire.requestFrom(Mag, 6); // vi skal hente ut 3 heltall = 6 bytes
x = Wire.read() << 8; //X Most Significant Byte
x |= Wire.read(); //X Least S B
z = Wire.read() << 8; //Z MSB
z |= Wire.read(); //Z LSB
y = Wire.read() << 8; //Y MSB
y |= Wire.read(); //Y LSB
//Skriv ut 'rå-verdiene' for de tre aksene
Serial.print("x: "); Serial.print(x);
Serial.print(" y: "); Serial.print(y);
Serial.print(" z: "); Serial.print(z);
xx=x; yy=y; zz=z;
float q=sqrt(sq(xx) + sq(yy) + sq(zz));
Serial.print(" Sum: "); Serial.println(q,1);
delay(500);
}
/*
TEST med to ulike oppsett
#define CRBdata B00100000 // bitene CRB7..CRB0
..ga disse tallene
x: 0 y: 38 z: -537
x: 1 y: 39 z: -538
x: 1 y: 41 z: -537
-------------------------------
#define CRBdata B11000000 // bitene CRB7..CRB0
..ga disse tallene
x: 0 y: 11 z: -163
x: 0 y: 12 z: -162
x: 0 y: 11 z: -162
*/
I'm using this magnetic compass and connect with my mega board but it doesn't work.
SO I think my mega board has something wrong.
Basically, I think changing arduino board to another is a good method.
but Uno Board also didn't work.
I'm going to research it and I find something that didn't help me.
Compass can be 3.3v pin.
I read this phrase but, with my new compass It didn't work though.
Check the adress.
I have 5 compass sensor but ALL of it are 0X0D.(I think this is worng ,but I din't find some clue...
Showing ALL ZERO.
In my case, my serial board show x:0 , y:0, z:0.
i2c Bus show 0
Finally, I write code in matlab. and it said that
addrs = scanI2CBus(a);
show 0X0D(It is same value in arduino sketch)
d = i2cdev(a,'0x0D');
show
Bus = 0
adress = 13
pin = A4,A5(mega was A21,A20)
this point, IF it wasn't wrong, it can show
Bus = 0
adress = 0X0D(if it is right)
pin = A4,A5(mega was A21,A20)
matlab library said
Note
You can specify bus 1 only if you are using Arduino Due boards.
beside, matlab couldn't read value..
I saw that using resistor in SDA and SCL but it's not that helpful
Anyway, I think Board dosn't matter.. in this problem.
I don't know how could I do