I have problems getting anything to work with HMC5883L Magnetometer sensor:
I have tried more than 5 internet examples with this sensor. None of them have worked.
I checked conductivity with my wires and breadboard with my multitester. everything seems fine.
I checked voltage output from my arduino. Everything seems fine.
I even asked for a sensor replacement because i thought the sensor wasn't working.
Nothing seems to work so i ask you guys please to help me troubleshoot this damn thing.
Any clue about what could be the problem will be very appreciated.
Below are the code i used, the console output and the serial monitor output.
Here is the last code i tried even though i've tried many others.
/*
* adopted to Wire library version distributed with Arduino IDE 1.0 and later
* where send() and receive() have been replaced with read() and write(). ( by
* Janusz Bucki November 2015
*/
#include <Wire.h> //I2C Arduino Library
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
int i=0;
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.println(i++);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.println(z);
delay(250);
}
This is SOME of the the output from the console when i upload my code
(i can't put everything due to character limit).
Using board 'uno' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr
Detecting libraries used...
"C:\\Program Files (x86)\\...
"C:\\Users\\marti\\AppData\\Local\\Temp\\arduino_build_32704\\sketch\\magsensor.ino.cpp" -o nul
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10807 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-
Using cached library dependencies for file: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src\utility\twi.c
Generating function prototypes...
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10807 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-I
Compiling
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10807 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-
Compiling libraries...
Compiling library "Wire"
Compiling core...
Using precompiled core: C:\Users\marti\AppData\Local\Temp\arduino_cache_100756\core\core_arduino_avr_uno_0c812875ac70eb4a9b385d8fb077f54c.a
Linking everything together...
Usando librería Wire con versión 1.0 en la carpeta: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire
"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-size" -A "C:\\Users\\marti\\AppData\\Local\\Temp\\arduino_build_32704/magsensor.ino.elf"
El Sketch usa 3898 bytes (12%) del espacio de almacenamiento de programa. El máximo es 32256 bytes.
Las variables Globales usan 416 bytes (20%) de la memoria dinámica,
C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM4 -b115200 -D -Uflash:w:C:\Users\marti\AppData\Local\Temp\arduino_build_32704/magsensor.ino.hex:i
avrdude: Version 6.3-20171130
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch
System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"
Using Port : COM4
Using Programmer : arduino
Overriding Baud Rate : 115200
AVR Part : ATmega328P
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PC2
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :
Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 65 20 4 0 no 1024 4 0 3600 3600 0xff 0xff
flash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xff
lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
Programmer Type : Arduino
Description : Arduino
Hardware Version: 3
Firmware Version: 4.4
Vtarget : 0.3 V
Varef : 0.3 V
Oscillator : 28.800 kHz
SCK period : 3.3 us
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "C:\Users\marti\AppData\Local\Temp\arduino_build_32704/magsensor.ino.hex"
avrdude: writing flash (3898 bytes):
Writing | ################################################## | 100% 0.60s
avrdude: 3898 bytes of flash written
avrdude: verifying flash memory against C:\Users\marti\AppData\Local\Temp\arduino_build_32704/magsensor.ino.hex:
avrdude: load data flash data from input file C:\Users\marti\AppData\Local\Temp\arduino_build_32704/magsensor.ino.hex:
avrdude: input file C:\Users\marti\AppData\Local\Temp\arduino_build_32704/magsensor.ino.hex contains 3898 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.47s
avrdude: verifying ...
avrdude: 3898 bytes of flash verified
avrdude done. Thank you.
And this is the output from serial monitor... no matter how much i move the sensor or rotate it, it always prints the same numbers.
0
x: -18171 y: 128 z: 773
1
x: -18171 y: 128 z: 773
2
x: -18171 y: 128 z: 773
3
x: -18171 y: 128 z: 773
4
x: -18171 y: 128 z: 773
Finally this is a picture of my current wiring
Arduino -> HMC5833L
5V -> VCC
GND -> GND
SCL -> SCL
SDA -> SDA