HMC5883 3xsketch 3xohne Funktion

Hallo liebe Forumsmitglieder,
das Projekt Neuanfang habe ich zunächst zurückgestellt.
Hab aus meinen Sensoren den GY271 für die nächsten Versuche auserkoren.
Nun habe ich 3 sketche und keiner bringt das erwartete Ergebnis in die Serielle Ausgabe.
Ich habe die Codes zum kopieren jeweils um die Ausgaben erweitert,der I2c Scanner zeigt kein Ergebnis es werden aber Ausgaben gemacht????
Nr 1

// Kompass mit Kompass-Sensor HMC5883
// Datei: arduino-sensoren-2-kap6-kompass-hmc5583.ino

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

float winkel;
int w;

// Kompass Objekt
HMC5883L compass;
// Fehler
int error = 0;

  
void setup()
{
   // Serielle Schnittstellen
  Serial.begin(9600);
  Wire.begin();
  Serial.print("Start Kompass mit HMC5883");

  // Skalierung setzen
  error = compass.setScale(1.3);
  // falls Fehler
  if(error != 0){
    Serial.println(compass.getErrorText(error));    
  }
  // Continuous Mode setzen
  error = compass.setMeasurementMode(MEASUREMENT_CONTINUOUS);
  if(error != 0){
     Serial.println(compass.getErrorText(error));
  }
}


void loop() 
{ 
  // Rohdaten einlesen
  MagnetometerRaw raw = compass.readRawAxis();
  // Skalierte Daten
  MagnetometerScaled scaled = compass.readScaledAxis();
  // Kalkulation
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
  // Ermittlung Magnetic Declination unter http://www.magnetic-declination.com/
  // Mein Wert: +2° 29' -> 2.48333° -> Rad 0.043342
  float declinationAngle = 0.0349066;
  heading += declinationAngle;
  
  // Korrektur wenn invertiert
  if(heading < 0)
    heading += 2*PI;
  // Limitierung Winkel
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Umrechnung Radiant zu Grad
  winkel = heading * 180/M_PI;
  
  // Winkel als Integer
  w = winkel;
    
  // Ausgabe Winkelgrad
  Serial.print("Winkel: ");
  Serial.println(w);

  delay(1000);
 
 // eingefügt I2c Scanner 

  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
  
}

/* Ausgabe Monitor
08:20:58.816 -> HMC5883 Magnetometer Test
08:20:58.848 -> 
08:21:16.139 -> ------------------------------------
08:21:16.235 -> Sensor:       HMC5883
08:21:16.236 -> Driver Ver:   1
08:21:16.236 -> Unique ID:    12345
08:21:16.245 -> Max Value:    800.00 uT
08:21:16.270 -> Min Value:    -800.00 uT
08:21:16.270 -> Resolution:   0.20 uT
08:21:16.301 -> ------------------------------------
08:21:16.333 -> 
08:21:16.788 -> X: -0.09  Y: -0.09  Z: -0.10  uT
08:21:16.820 -> Heading (degrees): 227.57

08:26:52.264 -> No I2C devices found
08:26:52.296 -> 
08:26:52.296 -> Winkel: 89
08:26:53.269 -> Scanning...
08:26:53.303 -> No I2C devices found
08:26:53.303 -> 
08:26:53.303 -> Winkel: 274
08:26:54.303 -> Scanning...
08:26:54.303 -> No I2C devices found
08:26:54.336 -> 
08:26:54.336 -> Winkel: 90
08:26:55.303 -> Scanning...
08:26:55.335 -> No I2C devices found
*/

Nr 2

// Kompass mit LED-Anzeige
// Datei: arduino-sensoren-2-kap6-kompass-led.ino


/*****************************************************************************/	
//	Function:	 Get the Geographic direction of the X-axis.
//				If X-axis points to the North, it is 0 degree.
//				If X-axis points to the East, it is 90 degrees.
//				If X-axis points to the South, it is 180 degrees.
//				If X-axis points to the West, it is 270 degrees.
//  Hardware:   Grove - 3-Axis Digital Compass
//	Arduino IDE: Arduino-1.0
//	Author:	 Frankie.Chu		
//	Date: 	 Jan 10,2013
//	Version: v1.0
//	by www.seeedstudio.com
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
/*******************************************************************************/

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

// Kompass Objekt
HMC5883L compass;
// Fehler
int error = 0;

// Wert Winkel
float headingDegrees;

// LED-Pins
int PinLED1 = 2;
int PinLED2 = 3;
int PinLED3 = 4;
int PinLED4 = 5;
int PinLED5 = 6;
int PinLED6 = 7;
int PinLED7 = 8;
int PinLED8 = 9;


void setup()
{
  // Ausgänge LED
  pinMode(PinLED1, OUTPUT);
  pinMode(PinLED2, OUTPUT);
  pinMode(PinLED3, OUTPUT);
  pinMode(PinLED4, OUTPUT);
  pinMode(PinLED5, OUTPUT);
  pinMode(PinLED6, OUTPUT);
  pinMode(PinLED7, OUTPUT);
  pinMode(PinLED8, OUTPUT);
  
  // Start serielle Schnittstelle
  Serial.begin(9600);

  Serial.println("Starting the I2C interface.");
  Wire.begin(); // Start the I2C interface.

  Serial.println("Constructing new HMC5883L");
    
  Serial.println("Setting scale to +/- 1.3 Ga");
  error = compass.setScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.getErrorText(error));
  
  Serial.println("Setting measurement mode to continous.");
  error = compass.setMeasurementMode(MEASUREMENT_CONTINUOUS); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.getErrorText(error));
}


void loop()
{
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.readRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.readScaledAxis();
  
  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
  // 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: -2��37' which is -2.617 Degrees, or (which we need) -0.0456752665 radians, I will use -0.0457
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = -0.0349;
  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;
   
  // Umrechnung Radiant zu Grad
  headingDegrees = heading * 180/M_PI; 

  // Output the data via the serial port.
  //Output(raw, scaled, heading, headingDegrees);

  // Ansteuerung LED
  SetLED(headingDegrees);

  // Ausgabe Winkel
  Serial.print(headingDegrees);
  Serial.println(" Grad");

  delay(1000);
}


void SetLED(float headingDegrees)
{
  float w = headingDegrees;

  // Ausgabe des Winkels, Aktivierung der zugehörigen LED
  
  // LED1, 337.5 - 360, 0-22.5
  if (w > 337.5 &&  w < 361)
  {
    digitalWrite(PinLED1, HIGH);
  }
  else
  {
    digitalWrite(PinLED1, LOW);
  }
  if (w > 0 &&  w < 22.5)
  {
    digitalWrite(PinLED1, HIGH);
  }
   else
  {
    digitalWrite(PinLED1, LOW);
  }
  
  // LED2, 22.5-67.5
  if (w > 22.5 &&  w < 67.5)
  {
    digitalWrite(PinLED2, HIGH);
  }
  else
  {
    digitalWrite(PinLED2, LOW);
  } 
  
  // LED3, 67.5-112.5
  if (w > 67.5 &&  w < 112.5)
  {
    digitalWrite(PinLED3, HIGH);
  }
  else
  {
    digitalWrite(PinLED3, LOW);
  }
 
  // LED4, 112.5-157.5
  if (w > 112.5 &&  w < 157.5)
  {
    digitalWrite(PinLED4, HIGH);
  }
  else
  {
    digitalWrite(PinLED4, LOW);
  }
  
  // LED5, 157.5-202.5
  if (w > 157.5 &&  w < 202.5)
  {
    digitalWrite(PinLED5, HIGH);
  }
  else
  {
    digitalWrite(PinLED5, LOW);
  }

  // LED6, 202.5-247.5
  if (w > 202.5 &&  w < 247.5)
  {
    digitalWrite(PinLED6, HIGH);
  }
  else
  {
    digitalWrite(PinLED6, LOW);
  }

  // LED7, 247.5-292.5
  if (w > 247.5 &&  w < 292.5)
  {
    digitalWrite(PinLED7, HIGH);
  }
  else
  {
    digitalWrite(PinLED7, LOW);
  }

  // LED8, 292.5-337.5
  if (w > 292.5 &&  w < 337.5)
  {
    digitalWrite(PinLED8, HIGH);
  }
  else
  {
    digitalWrite(PinLED8, LOW);
  }    

}
  

/* Ausgabe Monitor 
08:40:14.880 -> Starting the I2C interface.
08:40:14.880 -> Constructing new HMC5883L
08:40:14.880 -> Setting scale to +/- 1.3 Ga
08:40:16.353 -> Starting the I2C interface.
08:40:16.385 -> Constructing new HMC5883L
08:40:16.417 -> Setting scale to +/- 1.3 Ga
08:40:34.128 -> Setting measurement mode to continous.
08:40:34.219 -> 86.53 Grad
08:40:35.160 -> 86.53 Grad
08:40:36.160 -> 86.53 Grad
08:40:37.161 -> 86.53 Grad
08:40:38.159 -> 86.53 Grad
08:40:39.158 -> 86.53 Grad
08:40:40.157 -> 86.53 Grad
*/

Wenn ich den Sensor auf dem Tisch rotiere ändert sich nichts.

Wo ist mein Denkfehler ??

Nr 3

/***************************************************************************
  This is a library example for the HMC5883 magnentometer/compass

  Designed specifically to work with the Adafruit HMC5883 Breakout
  http://www.adafruit.com/products/1746
 
  *** You will also need to install the Adafruit_Sensor library! ***

  These displays use I2C to communicate, 2 pins are required to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Kevin Townsend for Adafruit Industries with some heading example from
  Love Electronics (loveelectronics.co.uk)
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the version 3 GNU General Public License as
 published by the Free Software Foundation.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 ***************************************************************************/

#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);

  /* 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: 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.0449066;
     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(1000);
}

/* Ausgabe des Monitors 
08:20:58.848 -> 
08:21:16.139 -> ------------------------------------
08:21:16.213 -> Sensor:       HMC5883
08:21:16.214 -> Driver Ver:   1
08:21:16.214 -> Unique ID:    12345
08:21:16.239 -> Max Value:    800.00 uT
08:21:16.270 -> Min Value:    -800.00 uT
08:21:16.271 -> Resolution:   0.20 uT
08:21:16.301 -> ------------------------------------
08:21:16.333 -> 
08:21:16.788 -> X: -0.09  Y: -0.09  Z: -0.10  uT
08:21:16.820 -> Heading (degrees): 227.57
08:21:17.792 -> X: -0.09  Y: -0.09  Z: -0.10  uT
08:21:17.826 -> Heading (degrees): 227.57
08:21:18.794 -> X: -0.09  Y: -0.09  Z: -0.10  uT
08:21:18.827 -> Heading (degrees): 227.57
08:21:19.796 -> X: -0.09  Y: -0.09  Z: -0.10  uT
*/

Nr 3 ist der Beispielcode den ich um den I2c Scanner erweitert habe,
wo ist es sinnig weiterzumachen?

Text ab hier Bearbeitet 24.3.25 11:55
Der Sensor ist nur mit dem Uno direkt verbunden.
VCC -- VCC
GND -- GND
SLC -- SLC
SDA -- SDA

Wenn im laufenden Betrieb SLC und SDA getauscht werden erfolgt Trotzdem eine Ausgabe im Monitor .

mfg
Walter

Wenn schon der I2C-Scanner nichts findet, brauchst Du mit dem Rest nicht weiter zu machen, bis Du da die Adresse angezeigt bekommst.

  • Verdrahtung richtig? Spannungen richtig?
  • Welcher MC?
  • Sind PullUp-Widerstände auf den Modulen? Wenn nein, selbst welche jeweils von SDA/SCL nach Vcc schalten.

Gruß Tommy

@Tommy56 während deines Post's habe ich den ersten bearbeitet
es sind keine Pullupwiderstände verbaut.
Wie kommt es zu den Ausgaben wenn der I2c Bus nicht läuft bzw SDA und SCL getauscht werden?

I2C => Bitweise Übertragung
Jeder mit Clock erkannte Pegelwechsel auf dem PIN wird (fehl) interpretiert.

1 Like

Hier noch 2 Foto's


Das Modul kommt von hier

GY-271 QMC5883 Kompass-Modul 3-Achsen Magnetometer Sensor Arduino HMC5883L

sensus-de
Gruß
Walter

Da sind 2 PULLUP drauf.
Wenn Du mit einem Scanner nix findest, dann ist was falsch.

SDA/SCL vertauscht?
Spannungsversorgung OK?
Mit was für einem Controller gehst Du da ran?

Ich kann dir nur empfehlen, den I2C-Scanner alleine, ohne irgend eine Erweiterung zu testen. Da baut man sehr gern und schnell Fehler ein, die einem dann die Funktion verhagelt.

Die Spannung beziehe ich aus einem USB Netzteil von Heitech


Usb kabel aufgetrennt und auf Breakboard geführt dann zum
UNO bei 5V und GND.
In dem USB Kabel zum Laptop die 5V unterbrochen.
Auf dem Steckbrett mess ich 5,1V.

Mit diesem Scanner gibt es eine Ausgabe 0xD

#include<Wire.h>

void setup(){
  Wire.begin();
  Serial.begin(9600);
  Serial.println("I2C Scanner ist bereit.");
  Serial.println();
}

void loop() {
  scanI2C(100000);
  scanI2C(400000);
  scanI2C(1000000); // nur aktivieren, wenn der Microcontroller diese Frequenz unterstützt
  scanI2C(3400000); // nur aktivieren, wenn der Microcontroller diese Frequenz unterstützt
  scanI2C(5000000); // nur aktivieren, wenn der Microcontroller diese Frequenz unterstützt
  
  Serial.println("****************************");
  Serial.println();
  delay(3000);
}

void scanI2C(long frequency){
  String normal = "standard mode (100 kHz):";
  String fast = "fast mode (400 kHz):";
  String fastPlus = "fast mode plus (1 MHz):";
  String highSpeed = "high speed mode (3.4 MHz):";
  String ultraSpeed = "ultra fast mode (5.0 MHz):";
  String defaultStr = " !!!!! Unzulässige Frequenz !!!!!";
  bool error = true;
  bool addressFound = false;

  Serial.print("Scanne im ");
  switch(frequency){
    case 100000:
      Serial.println(normal);
      break;
    case 400000:
      Serial.println(fast);
      break;
    case 1000000:
      Serial.println(fastPlus);
      break;
    case 3400000:
      Serial.println(highSpeed);
      break;
    case 5000000:
      Serial.println(ultraSpeed);
      break;
    default:
      Serial.println(defaultStr);
      break;
  }
  
  Wire.setClock(frequency);
  for(int i=1; i<128; i++){
    Wire.beginTransmission(i);
    error = Wire.endTransmission();
    if(error == 0){
      addressFound = true;
      Serial.print("0x");
      Serial.println(i,HEX);
    }
  }
  if(!addressFound){
    Serial.println("Keine Adresse erkannt");
  }
  Serial.println();
}

0xD halte ich als Adresse für wenig wahrscheinlich, da die Adressen von 0 bis 0xF gemäß Spezifikation reserviert sind.

Gruß Tommy

Hab diesen Beitrag gefunden

wenn ich alles richtig lese gab es dort ähnliche Probleme!

Kopie des Scanners

13:37:04.929 ->

13:37:04.960 -> Scanne im high speed mode (3.4 MHz):

13:37:04.992 -> 0xD

13:37:04.992 ->

13:37:04.992 -> Scanne im ultra fast mode (5.0 MHz):

13:37:05.023 -> 0xD

13:37:05.023 ->

13:37:05.023 -> ****************************

Hast Du Dir den Thread komplett durch gelesen und mal auf Deinen IC geschaut? Evtl. hast Du auch den QMC5833L?
Wenn sich der Hersteller nicht an die Spec hält, kann es auch 0xD sein.

Gruß Tommy

Und hier solltest du besser entsprechend der Kommentare handeln.
Diese Frequenzen werden vom Uno nicht unterstützt.

Auf dem Chip ist nichts zu Lesen hab dann mal dies probiert

#include <Wire.h> //I2C Arduino Library

#define addr 0x1E //I2C Address for The HMC5883

void setup(){
  
  Serial.begin(9600);
  Wire.begin();
  
  
  Wire.beginTransmission(addr); //start talking
  Wire.write(0x02); // Set the Register
  Wire.write(0x00); // Tell the HMC5883 to Continuously Measure
  Wire.endTransmission();
}


void loop(){
  
  int x,y,z; //triple axis data

  //Tell the HMC what regist to begin writing data into
  Wire.beginTransmission(addr);
  Wire.write(0x03); //start with register 3.
  Wire.endTransmission();
  
 
 //Read the data.. 2 bytes for each axis.. 6 total bytes
  Wire.requestFrom(addr, 6);
  if(6<=Wire.available()){
    x = Wire.read()<<8; //MSB  x 
    x |= Wire.read(); //LSB  x
    z = Wire.read()<<8; //MSB  z
    z |= Wire.read(); //LSB z
    y = Wire.read()<<8; //MSB y
    y |= Wire.read(); //LSB y
  }
  
  // Show Values
  Serial.print("X Value: ");
  Serial.println(x);
  Serial.print("Y Value: ");
  Serial.println(y);
  Serial.print("Z Value: ");
  Serial.println(z);
  Serial.println();
  
  delay(500);
}

/* Ausgabe Monitor ändert sich nicht bei Bewegung des Sensors
mit Adresse 0xD
Ausgabe alle Achsen 0
14:58:07.593 -> 
14:58:08.039 -> X Value: -22522
14:58:08.072 -> Y Value: 237
14:58:08.072 -> Z Value: 128
14:58:08.072 -> 
14:58:08.555 -> X Value: -22522
14:58:08.556 -> Y Value: 237
14:58:08.588 -> Z Value: 128
14:5
*/

Ja hab ich gelesen sehe aber nicht ob es dann geklappt hat !

Hast du die Doku gelesen?
Dort stehen die I2C Adressen, wenn mit denen nich klappt un die Lib Beispiele nicht funktionieren ist das Ding defekt.

Doku post #4 ja gelesen dauert bei mir länger Englisch mangelhaft :grinning_face:
Adresse 0x3D oder 0x3E gibt die ausgabe wie post #15
aber auch Ausgaben für
X Value: -22522
Y Value: 0
Z Value: 128

Nimm doch eine Lib, teste die Beispiele.

@fony
Habe mit diesem Beispiel begonnen es hat sich bei Sensorbewegung nichts verändert.

/***************************************************************************
  This is a library example for the HMC5883 magnentometer/compass

  Designed specifically to work with the Adafruit HMC5883 Breakout
  http://www.adafruit.com/products/1746
 
  *** You will also need to install the Adafruit_Sensor library! ***

  These displays use I2C to communicate, 2 pins are required to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Kevin Townsend for Adafruit Industries with some heading example from
  Love Electronics (loveelectronics.co.uk)
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the version 3 GNU General Public License as
 published by the Free Software Foundation.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 ***************************************************************************/

#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);
 
  /* 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: 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.22;
  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);
}
/* ####  Ausgabe hierhin kopiert ##########
-----------------------------------
Sensor:       HMC5883
Driver Ver:   1
Unique ID:    12345
Max Value:    800.00 uT
Min Value:    -800.00 uT
Resolution:   0.20 uT
------------------------------------

X: -0.09  Y: -0.09  Z: -0.10  uT
Heading (degrees): 237.61
X: -0.09  Y: -0.09  Z: -0.10  uT
Heading (degrees): 237.61
X: -0.09  Y: -0.09  Z: -0.10  uT
Heading (degrees): 237.61
X: -0.09  Y: -0.09  Z: -0.10  uT

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

X: -0.09  Y: -0.09  Z: -0.10  uT
Heading (degrees): 237.61
X: -0.09  Y: -0.09  Z: -0.10  uT
Heading (degrees): 237.61
X: -0.09  Y: -0.09  Z: -0.10  uT
Heading (degrees): 237.61
X: -0.09  Y: -0.09  Z: -0.10  uT

Wird denn dein Sensor erkannt ?
Kommt im seriellen Monitor eine Info oder Meldung?