Duinotech magnetometer breakout board for HMC5883L - locking up after some hours

As a relative novice, I'd appreciate any suggestions here. I'm running this board and, despite needing to average out a lot of random noise, getting quite good results. But after a (variable) time lag the readings lock up as per the attached jpg.

Reloading the sketch fixes it for a while, but that's no comfort as I want it to run unsupervised 24/7.

I don't think it can be a coding problem.

The only thought I have is that that the board makers did not include pull-up resistors as the Honeywell chip (or it's cheap knock-off substitute) seems to require.

thanks!

magnetometer_code_9.19_with_averaging_and_zero_calibration.ino (2.17 KB)

If you have Win10, perhaps the PLX-DAQ doesn't like Win10, or vice versa.

For a test, I would run the serial data into putty or the Arduino IDE monitor. Then copy and paste into excel. Hint If you are not familiar with Data/Text to columns you should look into it.

Wideband noise is difficult to filter digitally. I would remove the filtering from the arduino code and see what it takes to filter it in Excel, then port that to the arduino.

John

Thanks for your interest, I'll keep looking into that.
I've since discovered that the readings lock up even using the serial monitor without PLX-DAQ; so I'm no wiser. It's odd that it works well for an unspecified period then locks up.

OP code so we don't have to download it. OP, read the how to use this forum-please read sticky to see how to properly post code.

#include <Wire.h>           //I2C Arduino Library
#define address 0x1E        //0011110b, I2C 7bit address of HMC5883 (note, no = sign or semi-colon)
int row = 0;                //forPLX-DAQ
float Xcal = 0;
float Ycal = 0;
float Zcal = 0;

void setup()
{
  Serial.begin(38400);                  //Initialize Serial communications
  Serial.println("CLEARDATA");          // set up PLX-DAQ spreadsheet
  Serial.println("LABEL,Time,av X,av Y, av   Z,row,X");
  Wire.begin();                         //Initialize I2C communications.
  Wire.beginTransmission(address);      //open communication with HMC5883, initiate correct operating mode.
  Wire.write(0x02);                     //select mode register
  Wire.write(0x00);                     //continuous measurement mode
  Wire.endTransmission();
}

void loop()
{
  int X, Y, Z, n;         //X,Y,Z= readings;
  float sumX, sumY, sumZ;      //'sum' has 2 functions: sum of n readings, then average thereof.
  sumX = 0;
  sumY = 0;
  sumZ = 0;
  n = 0;

  while (n < 41)
  {
    Wire.beginTransmission(address);      //Tell the HMC5883L where to begin reading data
    Wire.write(0x03);                     //select register 3, X MSB register
    Wire.endTransmission();
    Wire.requestFrom(address, 6);         //Read data from each axis, 2 registers per axis
    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
      sumX = sumX + X;
      sumY = sumY + Y;
      sumZ = sumZ + Z;
      n++;
      delay(200);
    }
  }
  if (row == 0)
  {
    Xcal = sumX;
    Ycal = sumY;
    Zcal = sumZ;
  }

  sumX = (sumX - Xcal)/40;   //actually, this averages X. n = 40 in this interation of the sketch
  sumY = (sumY - Ycal)/40;
  sumZ = (sumZ - Zcal)/40;

  Serial.print("DATA, TIME,");
  Serial.print(sumX);
  Serial.print(",");
  Serial.print(sumY);
  Serial.print(",");
  Serial.print(sumZ);
  Serial.print(",");
  Serial.print(row);
  Serial.print(",");
  Serial.print (X);
  Serial.println(",");
  row++;
  delay(100);

}

As a test i would increase the range of X, Y, Z to int32_t In theory it should not matter but just a test.

The other thought is noise on the power supply to the sensor. Perhaps it is getting reset, I'm not familiar with your sensor, what happens if it is not initialized after a power interruption?

Thanks everyone for your interest and advice, I'll follow up your suggestions.

The fault is not with the Excel macro - it still locks up when I just use the Arduino serial monitor or serial plotter.

I've replaced both the USB lead and the Arduino Nano with others - no difference.

It is certainly not some sort of overload/out of range problem, as bringing a magnet close to the sensor sends the plot rocketing up or down.

It's not related to any external change, other apps running on the PC, or to a set time - it appears to happen quite at random, sometimes after running successfully for several hours.

When the fault occurs, simply closing the serial monitor and opening it again restores normal functioning. No need to touch the hardware.