Trouble reading HMC5883L Compass module
Res
Posts: 4
I recently acquired a HMC5883L compass module and connected it to an Arduino to run the test code provided at the Parallax web site. After fixing an error that popped up during compilation (had to change "send" and "receive" to "write" and "read" to conform to the latest Arduino syntax), the code ran, but all I get out of the module are unchanging values on all three axes regardless of orientation. I have experience with I2C software and with I2C software for the Arduino in particular (using the wire library), and the code looks fine. Anyone seen this kind of behavior?
Comments
Thanks!
Res
/*
An Arduino code example for interfacing with the HMC5883
Modified by Res to update "send" to "write" and "receive" to "read" and
to define mode, reg and xreg as registers of interest for experiment 8/9/12.
by: Jordan McConnell
SparkFun Electronics
created on: 6/30/11
license: OSHW 1.0, http://freedomdefined.org/OSHW
Analog input 4 I2C SDA
Analog input 5 I2C SCL
*/
#include <Wire.h> //I2C Arduino Library
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
byte mode = 0x02; //mode select register
byte reg = 0x00; //continuous read mode
byte xreg = 0x03; //first data register (1 of 6, MSB and LSB for x, y and z
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(mode); //select mode register
Wire.write(reg); //continuous measurement mode
Wire.endTransmission();
}
void loop(){
int x,y,z; //triple axis data
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(address);
Wire.write(xreg); //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(250);
}
http://learn.parallax.com/KickStart/29133
If it doesn't work, things to check:
1. Be sure power and ground are connected as shown in the diagram on the above page. Don't use the 3.3V source on the Arduino.
2. Double-check the I2C wiring. It's pretty easy to switch data and clock.
3. Don't operate on a metal table, or near heavy metal objects.
Though you did say you've used I2C before, just as a final sanity check, verify agains the specific Arduino you're using. Be sure to connect I2C according to the needs of your board. On an Uno, that's analog pins 4 and 5. On a Mega, it's pins 20/21. It's also different on the Leonardo.
-- Gordon
Unfortunately, no joy with the code you recommended. I connected 5 volts from the Arduino Uno, made sure Arduino pin A4 is connected to SDA on the 5883 and A5 is connected to SCL. I am presuming the Parallax 5883 module has pull ups on its board for the I2C bus. I made sure the board is clear of large metal things and uploaded the code. This time the code returns x = -1, y = -1 and z = -1, regardless of how I move the module or what orientation it is in. I know the Arduino is working with I2C correctly using the Wire library because I have been experimenting with a Parallax 27911 gyroscope, and it performs just fine. I had been experimenting with the gyro and an accelerometer and was adding the 5883 to complete an inertial measurement unit. By-the-way, I tried the 5883 both by itself with the Arduino Uno and on the same bus as the gyroscope to make sure there wasn't something going on between the two, but got the same results. i'm beginning to think I have a bad unit.
Thanks again!
Res
Beyond that, you might want to contact Parallax customer support at this point. If you got the gyro to work, the compass should, too. So it sounds like it could be a wacky unit.
-- Gordon
I have sent a note and a pair of photos of my module to Parallax Support requesting verification of my finding and and asking what is supposed to occupy those empty solder pads (I can't find a schematic of this module anywhere on the web).
Thanks to those, especially Gordon, who have replied to this thread. I bought this part from Jameco, and will deal with them once I get verification from Parallax.
Res
Did you get your compass module from Parallax? I made the mistake awhile back obtaining a bunch of HMC5883L modules from China for a class I was teaching. Half of them were duds. You can read my mea culpa (i.e. crow-eating) here:
http://forums.parallax.com/discussion/158176/cheap-compass-sensors-a-cautionary-tale
-Phil