HMC5883L reading a single angle only after initialization

I am using an Eelegoo Mega2560 R3
The angle it gets stuck on (that I can recall) varies between 113,157,203, and 247.
image

I have VCC:5V, GND:GND, SCL:21, SDA:20

Code:

#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>
HMC5883L_Simple Compass;

void setup()
{
  Serial.begin(9600);
  Wire.begin(); 
  Compass.SetDeclination(23, 35, 'E');  
  Compass.SetSamplingMode(COMPASS_SINGLE);
  Compass.SetScale(COMPASS_SCALE_250);
  Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
}

// Our main program loop.
void loop()
{
   float heading = Compass.GetHeadingDegrees();
   
   Serial.print("Heading: \t");
   Serial.println( heading );   
   delay(1000);
}

Any ideas what causes this issues and suggested fixes?

Code with annotation:

/*
 * 
 * Compass.ino - Example sketch for reading a heading from an HMC5883L triple axis magnetometer.
 * 
 * GY-273 Compass Module  ->  Arduino
 * VCC  -> VCC  (See Note Below)
 * GND  -> GND
 * SCL  -> A5/SCL, (Use Pin 21 on the Arduino Mega)
 * SDA  -> A4/SDA, (Use Pin 20 on the Arduino Mega)
 * DRDY -> Not Connected (in this example)
 * 
 * Voltage Note
 * ~~~~~~~~~~~~  
 * The GY-273 Board has a 3v3 Regulator on it, and the SDA/SCL are pulled up to that so it is OK to 
 * use with 5v Arduino's.
 * 
 * If you are using any other breakout, or the raw IC, you need to be using 3v3 to supply and signal!
 * 
 * Datasheet: http://goo.gl/w1criV
 * 
 * Copyright (C) 2014 James Sleeman
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
 * THE SOFTWARE.
 * 
 * @author James Sleeman, http://sparks.gogo.co.nz/
 * @license MIT License
 * 
 */

#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>

// Create a compass
HMC5883L_Simple Compass;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
    
  // Magnetic Declination is the correction applied according to your present location
  // in order to get True North from Magnetic North, it varies from place to place.
  // 
  // The declination for your area can be obtained from http://www.magnetic-declination.com/
  // Take the "Magnetic Declination" line that it gives you in the information, 
  //
  // Examples:
  //   Christchurch, 23° 35' EAST
  //   Wellington  , 22° 14' EAST
  //   Dunedin     , 25° 8'  EAST
  //   Auckland    , 19° 30' EAST
  //    
  Compass.SetDeclination(23, 35, 'E');  
  
  // The device can operate in SINGLE (default) or CONTINUOUS mode
  //   SINGLE simply means that it takes a reading when you request one
  //   CONTINUOUS means that it is always taking readings
  // for most purposes, SINGLE is what you want.
  Compass.SetSamplingMode(COMPASS_SINGLE);
  
  // The scale can be adjusted to one of several levels, you can probably leave it at the default.
  // Essentially this controls how sensitive the device is.
  //   Options are 088, 130 (default), 190, 250, 400, 470, 560, 810
  // Specify the option as COMPASS_SCALE_xxx
  // Lower values are more sensitive, higher values are less sensitive.
  // The default is probably just fine, it works for me.  If it seems very noisy
  // (jumping around), incrase the scale to a higher one.
  Compass.SetScale(COMPASS_SCALE_088);
  
  // The compass has 3 axes, but two of them must be close to parallel to the earth's surface to read it, 
  // (we do not compensate for tilt, that's a complicated thing) - just like a real compass has a floating 
  // needle you can imagine the digital compass does too.
  //
  // To allow you to mount the compass in different ways you can specify the orientation:
  //   COMPASS_HORIZONTAL_X_NORTH (default), the compass is oriented horizontally, top-side up. when pointing North the X silkscreen arrow will point North
  //   COMPASS_HORIZONTAL_Y_NORTH, top-side up, Y is the needle,when pointing North the Y silkscreen arrow will point North
  //   COMPASS_VERTICAL_X_EAST,    vertically mounted (tall) looking at the top side, when facing North the X silkscreen arrow will point East
  //   COMPASS_VERTICAL_Y_WEST,    vertically mounted (wide) looking at the top side, when facing North the Y silkscreen arrow will point West  
  Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
  
}

// Our main program loop.
void loop()
{
   float heading = Compass.GetHeadingDegrees();
   
   Serial.print("Heading: \t");
   Serial.println( heading );   
   delay(100);
}

How is it wired? Schematics please.
The compass is a 3 volt device and the Mega is a 5 volt device. A level shifter is needed to be used.

Can you try this instead
Compass.SetSamplingMode(COMPASS_CONTINUOUS);`

Same issue arrises.

If you aren't using a level shifter, the sensor may already have been destroyed.

A level shifter would be to shift the voltage correct? The sensor stated that it is able to handle the 5 V directly from an arduino. (Saw on a data sheet and also in a video).

How might I go about testing if it did get destroyed this way?

Post a link to the product page for the sensor you have. For many cheap sensors 5V power is accepted, but no components for the required logic level conversion are on the board.

How might I go about testing if it did get destroyed this way

It won't function, as you have already reported.

The Adafruit HMC5883L breakout board has level shifting and a 3.3V regulator built into it. Is that the board you're using?

This is exact sensor I am using.

image
However instead of nano it is an arduino Mega with the SDA and SCL ports respectively.

UPDATE:
image
After it changed what angle it was stuck to it then worked perfectly for about 10 seconds before fixing at another angle. With it seems 0.1 degree precision and not jumping around much.
image

That compass board is a 3.3 volt logic device. Don't connect it to the 5 volt Mega without level shifting. Malfunctioning or even damage can be expected.
Datasheet: Datasheet Archive HMC5883L-DEMO datasheet download

The good news is that breakout board seems to have a 3.3V regulator and level shifting MOSFETs as well. So you haven't destroyed it by hooking it up to 5V. The bad news is why it doesn't work (or rather, why it does work but gets stuck on an angle) is still a mystery.

What happens if you run the code from this wiki page?

http://wiki.sunfounder.cc/index.php?title=GY-271_HMC5883L_3-Axis_Magnetic_Electronic_Compass_Module