HMC5883L shows constant reading

Hey there,
i recently purchased an HMC5883L Magnetometer. Aber i hooked it up with my Arduino i could only get "x: 0, y: 0, z: 0", everytime the Serial monitor refreshed.

So i switched provider and got a new one and used different code. Now it does show values but instead of outputing different readings, depending on the direction of the Magnetometer, the serial monitor gives me the same value everytime.

I thought my location might be a problem so i went somewhere else and indeed the value changed. But again, no matter in which direction i pointed my HMC5883L, there was always the same reading.

Could my laptop (max. distance when tested: ~30cm) be the problem?

Thanks a lot,
Gus

Could my laptop (max. distance when tested: ~30cm) be the problem?

No, But the connections and the code can.
More info needed.

ok so this was my Code
i just copy-pasted it (https://www.meccanismocomplesso.org/en/arduino-magnetic-magnetic-magnetometer-hmc5883l/) and also connected the wires as told on the website to try if the compass works.

the only difference between my compass and the one used is that mine got way more contacts than the one portrayed on the website so i only connected the ones, which where the same (VCC, GND, SCL, SDA).

does that help?

#include <Wire.h>
#include <HMC5883L.h>

HMC5883L compass;

void setup()
{
Wire.begin();
Serial.begin(9600);
compass = HMC5883L();

Serial.println(“Setting scale to +/- 1.3Ga”);
int error = compass.SetScale(1.3);
if(error != 0)
Serial.println(compass.GetErrorText(error));

Serial.println(“Setting measurement mode to continuous”);
error = compass.SetMeasurementMode(Measurement_Continuous);
if(error != 0)
Serial.println(compass.GetErrorText(error));
}

void loop()
{
MagnetometerRaw raw = compass.ReadRawAxis();
float heading = atan2(raw.YAxis, raw.XAxis);
if(heading < 0)
heading += 2*PI;
float headingDegrees = heading * 180/M_PI;
Serial.println(headingDegrees);
delay(1000);
}

Try using the I2Cscanner program to verify that the Arduino can detect the compass.

Note that there are fake HMC5883L that don’t work, and mislabeled QMC5883 sensors that require different code.

ok so i tried the i2c scanner and it found the device and now the serial monitor gives me a different value when i use the old code
but still always the same no matter what direction

it found the device

Did it return the correct device address?

how do i know?

GusWT:
how do i know?

It should be documented in the device’s datasheet.