Example Code for Magnetometer Uploads but Doesn't Perform

I am attempting to receive data from a magnetometer. I checked with another program, it is definitely transmitting. Here is the code

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

/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

void displaySensorDetails(void)
{
  sensor_t sensor;
  mag.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
  Serial.println("------------------------------------");
  Serial.println("");
  //delay(500);
}

void setup(void) 
{
  Serial.begin(9600);
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
  
  /* Display some basic information on this sensor */
  displaySensorDetails();

 
}


void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  mag.getEvent(&event);
 Serial.println("void loop working");
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.println("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.println("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.println("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");
  
  // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;

  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 
  
  Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
  
 // delay(500);
}

This code uploads and compiles fine, no errors, but it does not receive the data it should. I have attached a screenshot of what it prints. As you can see, it does not even print the line in the void loop. I cannot imagine a reason for the void loop not to run. I have tried commenting out most of the code, in separate runs and together, but no matter what nothing in the void loop runs.

Thank you for any help.

Your code is blocking on the getEvent() function. It will wait forever until is receives 6 bytes. I am guessing you are not actually talking to the sensor. Try running an i2cbusscan sketch to see which devices are on the bus.

If you look inside the library, you will notice that the begin() function writes to the device, but discards the return value and always return TRUE (even though the example code would have you believe otherwise...)

There are 2 kinds of HM5883 modules out there. One is the genuine HM5883L and the other is a "fake". Maybe yours is the fake. This page explains how to tell a fake and has has more information.