HMC-5883L question

Hello all.

I have an GY-85 with HMC-5883L magnetometer onboard.

When I'm working on reading the data from it, sometimes (absolutly randomly for me) data begin jump for hundreds of points. For example, from -200 to 300 and so on. I understand about filters and so on. But what can cause the problem? With all other sensors onboard - all ok. No magnetic sources around.

It looks like this:

Please follow to forum rules and post the code, using code tags.

Are the glitches always in one axis? Does that axis respond to changes in sensor orientation in a sensible way, and otherwise behave like the other two axes?

As for the last observation, the glitches appears on all three axis when the value in near zero. All axis responds ok on movement and shows probably correct data. Looks like the problem can be in reading data from registers, but I followed the standart way:

   mag_d[0] = ((((int) buff[0]) << 8) | buff[1]);  // X axis
    mag_d[1] = ((((int) buff[4]) << 8) | buff[5]);  // Y axis
    mag_d[2] = ((((int) buff[2]) << 8) | buff[3]);  // Z axis

Where mag_d is array of Int.

Post ALL the code. State which Arduino you are using.

You need to be extremely careful with shifting, as the behavior is different for signed versus unsigned integers, and is implementation-dependent.

#define OUTPUT_BAUD_RATE 9600
#define OUTPUT_DATA_INTERVAL 20  // in milliseconds
#define STATUS_LED_PIN 13  // Pin number of status LED

#include <Wire.h>

int magnetom[3];
unsigned long timestamp;
unsigned long timestamp_old;
boolean output_errors = false;  // true or false
boolean output_stream_on;

void turn_output_stream_on()
{
  output_stream_on = true;
  digitalWrite(STATUS_LED_PIN, HIGH);
}

void turn_output_stream_off()
{
  output_stream_on = false;
  digitalWrite(STATUS_LED_PIN, LOW);
}

char readChar()
{
  while (Serial.available() < 1) { } // Block
  return Serial.read();
}

void setup() {
  // Init serial output
  Serial.begin(OUTPUT_BAUD_RATE);
  
  // Init status LED
  pinMode (STATUS_LED_PIN, OUTPUT);
  digitalWrite(STATUS_LED_PIN, LOW);

  // Init sensors
  delay(50);  
  I2C_Init();
  Magn_Init();
  timestamp = millis();
  turn_output_stream_on();
}

void loop() {
  if((millis() - timestamp) >= OUTPUT_DATA_INTERVAL)
  {
    timestamp_old = timestamp;
    timestamp = millis();
    if (timestamp > timestamp_old)
    Read_Magn();
    output_mag_binary();
  }
}
#define MAGN_ADDRESS  ((int) 0x1E) // 0x1E = 0x3C / 2

#if ARDUINO >= 100
  #define WIRE_SEND(b) Wire.write((byte) b) 
  #define WIRE_RECEIVE() Wire.read() 
#else
  #define WIRE_SEND(b) Wire.send(b)
  #define WIRE_RECEIVE() Wire.receive() 
#endif


void I2C_Init()
{
  Wire.begin();
}

void Magn_Init()
{
  Wire.beginTransmission(MAGN_ADDRESS);
  WIRE_SEND(0x00);//
  WIRE_SEND(0x78);  //  
  Wire.endTransmission();
  delay(6);

  Wire.beginTransmission(MAGN_ADDRESS);
  WIRE_SEND(0x01); 
  WIRE_SEND(0x80);  
  Wire.endTransmission();
  delay(6);

  Wire.beginTransmission(MAGN_ADDRESS);
  WIRE_SEND(0x02); 
  WIRE_SEND(0x00);  
  Wire.endTransmission();
  delay(12);
}

void Read_Magn()
{
  int i = 0;
  byte buff[6];
  Wire.beginTransmission(MAGN_ADDRESS); 
  WIRE_SEND(0x03);  // Send address to read from
  Wire.endTransmission();
  
  Wire.beginTransmission(MAGN_ADDRESS); 
  Wire.requestFrom(MAGN_ADDRESS, 6);  // Request 6 bytes
  while(Wire.available())  // ((Wire.available())&&(i<6))
  { 
    buff[i] = WIRE_RECEIVE();  // Read one byte
    i++;
  }
  Wire.endTransmission();
  
  if (i == 6)  // All bytes received?

  {
    magnetom[0] = ((((int) buff[0]) << 8) | buff[1]);  // X axis
    magnetom[1] = ((((int) buff[4]) << 8) | buff[5]);  // Y axis
    magnetom[2] = ((((int) buff[2]) << 8) | buff[3]);  // Z axis
   }
  else
  {
//   Serial.println("Error reading.");
  }
}

Added:
Arduino Nano.

A little addition.

I'd monitored the raw data. When any axis is near zero raw data looks like:

0000
0001
0000
00FF
0000

Or:

FFFE
FFFE
FF00
FFFE

So, the problem is that somehow one of registers gives FF when it is near zero. I tryed different frequencies and gain -- result is the same.

Any ideas?

Defective chip.

Defective HMC? Or the whole GY-85? Or maybe Nano? How can I check it?

Defective HMC5883L, obviously. Buy sensors from reputable manufacturers or resellers like Adafruit, Sparkfun, Pololu, etc. and you can count on genuine items and excellent product support.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.