HMC5883 freezes when unplug

Hi everyone,
I’m using an HMC5883L (L883 chip) sensor, libraries Wire.h, Adafruit_hmc5883_unified and Adafruit_sensor.

I’m working on a compass and trying to calibrate it. Example code gived by the h5883 library works fine and also the other that I’ve made… but… when I unplug the arduino from PC and reconnect, it fails!
When this has happened, I’m able to read an initial value on plug, then it freezes and never updates. After this, if I disconnect from PC move the sensor to a new position and replug again, it reads a new value and freezes again.
By freezing I mean that Serial.print returns the values as suposed to do but it gives always the same value no matter if I move the sensor.

Also I’ve found that if I upload an other different code it starts to work well again, updating and writting values as it is supposed to do. But even with a new code, if I disconnect it goes to the same failing point. Also, if I try to upload a second time the same code that has been loaded before, this won’t change anything, it needs to be different to work again.

Can someone help me before I try to kill myself?!

Have a nice day guys!

I would suggest powering down before disconnecting and see if that solves the problem. CMOS devices can latch up internally and behave in weird ways. In some devices it destroys them some nothing happens and others behave weird.

1 Like

Thanks gilshultz,

I just tryed it but still giving the same issue.

Reading the datasheet I’ve found that there are different Register Modes, Register Configurations, Identification Registers, etc…
Is there a way to read those binary values?
I’m thinking that there is an unexpected change between startup on uploading code and startup on reconnections

Hi,
What model Arduino are you using?
Can you post a copy of your circuit please?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

1 Like

Chinese Arduino nano:

sensor is HMC5883L (L883 2125)

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

float xv, yv, zv;

void setup(){
  Serial.begin(9600);
}

void loop() {
  magnet();
  Serial.flush();
  Serial.print(xv,1);
  Serial.print(",");
  Serial.print(yv,1);
  Serial.print(",");
  Serial.print(zv,1);
  Serial.println();
  delay(100);
}

void magnet() {
  sensors_event_t event;
  mag.getEvent(&event);
  xv = event.magnetic.x;
  yv = event.magnetic.y;
  zv = event.magnetic.z;
}
  

Hi,
Remove Serial.flush its not needed.

Tom… :smiley: :coffee: :australia:

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