I was wanting to build this test circuit.
https://www.electronoobs.com/eng_arduino_tut77.php
But when I tried to verify the code on this line,
Wire.beginTransmission(address)
I got this error.
'Wire' was not declared in this scope
#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 int xmin,xmax,ymin,ymax,zmin,zmax; xmin=0; xmax=0; ymax=0; ymin = 0; zmin=0;zmax=0; //Tell the HMC5883 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); }
@casey
Did you include the following header file?
#include <Wire.h>
No I didn't. I simply copied and pasted the code which didn't have the include.
It now compiles ok. Thank you for that one.
Just uploaded the file to the Arduino but moving the circuit about doesn't seem to effect the readings which remain at fixed values although the x value changes with a reset.
x:8201 y:0 z:773
Hitting the reset button,
x:31003 y:0 z:773
So I have some debug work to do. I am using the duinotech 3 axis compass magnetometer module.