Issue with Adafruit sensor library

i have a gps + magnetometer module purchased from here and i have connected it to an arduino uno through a 5V-3.3V level shifting module.
To test it i use the TinyGPS++, Adafruit_Sensor and Adafruit_HMC5883_Unified libraries but i run into some issues. First of all the gps data is only shown once and the data from the sensosr_event_t object is not shown at all. The strange thing is that i tested the magnetometer alone and the serial.print commands after i create the sensosr_event_t object don’t work as you can see in the screenshot

Here is the code:

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

   This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   9600-baud serial GPS device hooked up on pins 8(rx) and 9(tx) and a HMC5883 Magnetic Compass
   connected to the SCL/SDA pins.

static const int RXPin = 2, TXPin = 3, RPin = 4;
static const uint32_t GPSBaud = 9600;

// Assign a Uniquej ID to the HMC5883 Compass Sensor
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the NEO-6m GPS module
SoftwareSerial ss(RXPin, TXPin);

void displaySensorDetails(void)
  sensor_t sensor;
  Serial.print  ("Sensor:       "); Serial.println(;
  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");  

void setup()

  Serial.println(F("Simple Test with TinyGPS++ and attached NEO-6M GPS module"));
  Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());

void loop()
  // This sketch displays information every time a new sentence is correctly encoded from the GPS Module.
  while (ss.available() > 0)
    if (gps.encode(

void displayGpsInfo()
  // Prints the location if lat-lng information was recieved
  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
    Serial.print(, 6);
    Serial.print(gps.location.lng(), 6);
  // prints invalid if no information was recieved in regards to location.

  Serial.print(F("  Date/Time: "));
  // prints the recieved GPS module date if it was decoded in a valid response.
  if (
    // prints invalid otherwise.
    Serial.print(F("INVALID DATE"));

  Serial.print(F(" "));
  // prints the recieved GPS module time if it was decoded in a valid response.
  if (gps.time.isValid())
    if (gps.time.hour() < 10) Serial.print(F("0"));
    if (gps.time.minute() < 10) Serial.print(F("0"));
    if (gps.time.second() < 10) Serial.print(F("0"));
    if (gps.time.centisecond() < 10) Serial.print(F("0"));
    // Print invalid otherwise.
    Serial.print(F("INVALID TIME"));

void displayCompassInfo()
  /* Get a new sensor event */ 
  sensors_event_t event; 
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("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:
  // 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.081;
  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);


Simple Test with TinyGPS++ and attached NEO-6M GPS module
Testing TinyGPS++ library v. 1.0.2

Sensor:       HMC5883
Driver Ver:   1
Unique ID:    12345
Max Value:    800.00 uT
Min Value:    -800.00 uT
Resolution:   0.20 uT

Location: INVALID  Date/Time: 5/14/2020 13:49:38.00

Please post the serial output as text, not a screenshot image.

ok edited

after some debugging looks like all the trouble comes from the call mag.getEvent(&event) . if i remove this call the prints work as intended. Anyone knows why? This is a well known library. I don’t know what is the cause of this.
This is what getEvent does: (from the library github)

bool Adafruit_HMC5883_Unified::getEvent(sensors_event_t *event) {
  /* Clear the event */
  memset(event, 0, sizeof(sensors_event_t));

  /* Read new data */
  event->version   = sizeof(sensors_event_t);
  event->sensor_id = _sensorID;
  event->type      = SENSOR_TYPE_MAGNETIC_FIELD;
  event->timestamp = 0;
  event->magnetic.x = _magData.x / _hmc5883_Gauss_LSB_XY * SENSORS_GAUSS_TO_MICROTESLA;
  event->magnetic.y = _magData.y / _hmc5883_Gauss_LSB_XY * SENSORS_GAUSS_TO_MICROTESLA;
  event->magnetic.z = _magData.z / _hmc5883_Gauss_LSB_Z * SENSORS_GAUSS_TO_MICROTESLA;
  return true;

Location: INVALID means that the GPS unit does not have a satellite fix. You must be outdoors, with a clear view of the sky for it to work.

Ok i fixed it…i think. In the c++ file of the Adafruit_HMC5883_Unified library there was a function called read() in which arduino would go idle until it reads 6 bytes of data. I put the while (Wire.available() < 6); in comments and voila the loop continues! But why it expects 6 bytes? I receive less so that might be a problem?

void Adafruit_HMC5883_Unified::read()
  // Read the magnetometer
  #if ARDUINO >= 100
  Wire.requestFrom((byte)HMC5883_ADDRESS_MAG, (byte)6);
  // Wait around until enough data is available
  while (Wire.available() < 6);

  // Note high before low (different than accel)  
  #if ARDUINO >= 100
    uint8_t xhi =;
    uint8_t xlo =;
    uint8_t zhi =;
    uint8_t zlo =;
    uint8_t yhi =;
    uint8_t ylo =;
    uint8_t xhi = Wire.receive();
    uint8_t xlo = Wire.receive();
    uint8_t zhi = Wire.receive();
    uint8_t zlo = Wire.receive();
    uint8_t yhi = Wire.receive();
    uint8_t ylo = Wire.receive();
  // Shift values to create properly formed integer (low byte first)
  _magData.x = (int16_t)(xlo | ((int16_t)xhi << 8));
  _magData.y = (int16_t)(ylo | ((int16_t)yhi << 8));
  _magData.z = (int16_t)(zlo | ((int16_t)zhi << 8));
  // ToDo: Calculate orientation
  _magData.orientation = 0.0;

The magnetometer data are six bytes. If you want all the data, you have to read all six.

This line of code is unnecessary, and shows that the person who wrote the Adafruit library doesn’t understand how Wire.requestFrom() works. Wire.requestFrom() is a complete transaction, and will either return 6 bytes in its buffer, or fail.

  // Wait around until enough data is available
  while (Wire.available() < 6);

Oh ok thanks for the info sir much appreciated!