Advertisement
LeventeDaradici

HMC5883 - GY-271 Digital Compass Arduino

Sep 19th, 2022 (edited)
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. //
  2. //   HMC5883 - GY-271 Digital Compass Arduino
  3. //   https://www.youtube.com/c/LeventeDaradici/videos
  4. //   the module was bought from here: https://www.aliexpress.com/item/710707414.html
  5. //
  6.  
  7. #include <Wire.h>
  8. #include <Adafruit_Sensor.h>
  9. #include <Adafruit_HMC5883_U.h>
  10.  
  11. Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
  12.  
  13. void displaySensorDetails(void)
  14. {
  15.   sensor_t sensor;
  16.   mag.getSensor(&sensor);
  17.   Serial.println("------------------------------------");
  18.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  19.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  20.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  21.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  22.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  23.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
  24.   Serial.println("------------------------------------");
  25.   Serial.println("");
  26.   delay(500);
  27. }
  28.  
  29. void setup(void)
  30. {
  31.   Serial.begin(9600);
  32.   Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
  33.  
  34.   /* Initialise the sensor */
  35.   if(!mag.begin())
  36.   {
  37.     /* There was a problem detecting the HMC5883 ... check your connections */
  38.     Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
  39.     while(1);
  40.   }
  41.  
  42.   /* Display some basic information on this sensor */
  43.   displaySensorDetails();
  44. }
  45.  
  46. void loop(void)
  47. {
  48.   /* Get a new sensor event */
  49.   sensors_event_t event;
  50.   mag.getEvent(&event);
  51.  
  52.   /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  53.   Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  54.   Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  55.   Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");
  56.  
  57.   // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
  58.   // Calculate heading when the magnetometer is level, then correct for signs of axis.
  59.   float heading = atan2(event.magnetic.y, event.magnetic.x);
  60.  
  61.   // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  62.   // Find yours here: http://www.magnetic-declination.com/
  63.   // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
  64.   // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  65.   float declinationAngle = 0.22;
  66.   heading += declinationAngle;
  67.  
  68.   // Correct for when signs are reversed.
  69.   if(heading < 0)
  70.     heading += 2*PI;
  71.    
  72.   // Check for wrap due to addition of declination.
  73.   if(heading > 2*PI)
  74.     heading -= 2*PI;
  75.    
  76.   // Convert radians to degrees for readability.
  77.   float headingDegrees = heading * 180/M_PI;
  78.  
  79.   Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
  80.  
  81.   delay(500);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement