Compass output being very shaky and nonsensical

How can I fix the HMC5883 inside of the BN-880 from being all shaky?
I am using this code for self testing mode

#include <Wire.h> //I2C Arduino Library
#define addr 0x1E //I2C Address for The HMC5883

void setup(){
  
  Serial.begin(115200);
  Serial.print("HMC5833L COMPASS SENSOR BEGIN");
  Serial.println();
  Wire.begin();
  
  
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x02); // Set the Register
  Wire.write(0x01); // Enter self test
  while(true);

That code isnt actually unfinished, I just cut it off

And I set up processing with this code to show a compass

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

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

void setup(void)
{
  Serial.begin(115200);
  delay(5000);
}

void loop()
{
  sensors_event_t event;
  mag.getEvent(&event);
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  float declinationAngle = 0.22;
  heading += declinationAngle;
  if (heading < 0) {
    heading += 2 * PI;
  }
  if (heading > 2 * PI) {
    heading -= 2 * PI;
  }
  float headingDegrees = heading * 180 / M_PI;
  Serial.println(headingDegrees);
}

and

//Processing code
import processing.serial.*;
Serial port_1;
PImage background;
PImage dial;
float heading;
float headrad;

void setup() {
  //change the 0 to a 1 or 2 etc, to match your port.
  port_1 = new Serial(this, "COM10", 115200);
  background = loadImage("bg.png");//Adding image to processing
  dial = loadImage("dial.png");//Adding image to processing
  size(600, 600);
  frameRate(29);
  port_1.bufferUntil ( '\n' );
}

void draw() {
  translate(width/2, height/2);
  image(background, 0, 0, width, height);
  imageMode(CENTER);
  headrad = heading;//map(heading, 0, 360, 0, 6.283);
  //println(headrad);
  rotate(-headrad);
  image(dial, 0, 0, width, height);
}

void serialEvent (Serial port_1)
{
  heading = float(port_1.readStringUntil('\n'));
}

And that is giving me

(Processing compass)
The compass on the table isn’t moving whatsoever and its showing that its constantly spinning
Does anyone know why this is happening and how to fix it?
I don’t know if the compass is faulty or if I need to calibrate it a different way

Do you have a mechanical compass? Place it in your working area and move it around a bit. The earth magnetic field is quite weak compared to other sources in our environment. Remember moving charges (electric current) create magnetic fields. You will see that your compass needle is moving around near your computer.

As second experiment you do is to use a magnet to overpower the magnetic fields around your circuit and see whether it is working. Place the magnet near your circuit and move it around the chip. You should get a clear indication where your magnet is.

So I don't know why, but putting a magnet next to it suddenly made it start working?

micahsuess:
So I don't know why, but putting a magnet next to it suddenly made it start working?

It’s because the magnets field is much stronger than the other fields around it.

Sounds like you have not calibrated the compass. It won't work until you do.

Comprehensive tutorial