Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. /***************************************************************************
  2. This is a library example for the HMC5883 magnentometer/compass
  3.  
  4. Designed specifically to work with the Adafruit HMC5883 Breakout
  5. http://www.adafruit.com/products/1746
  6.  
  7. *** You will also need to install the Adafruit_Sensor library! ***
  8.  
  9. These displays use I2C to communicate, 2 pins are required to interface.
  10.  
  11. Adafruit invests time and resources providing this open source code,
  12. please support Adafruit andopen-source hardware by purchasing products
  13. from Adafruit!
  14.  
  15. Written by Kevin Townsend for Adafruit Industries with some heading example from
  16. Love Electronics (loveelectronics.co.uk)
  17.  
  18. This program is free software: you can redistribute it and/or modify
  19. it under the terms of the version 3 GNU General Public License as
  20. published by the Free Software Foundation.
  21.  
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. GNU General Public License for more details.
  26.  
  27. You should have received a copy of the GNU General Public License
  28. along with this program. If not, see <http://www.gnu.org/licenses/>.
  29.  
  30. ***************************************************************************/
  31.  
  32. #include <Wire.h>
  33. #include <Adafruit_Sensor.h>
  34. #include <Adafruit_HMC5883_U.h>
  35.  
  36. /* Assign a unique ID to this sensor at the same time */
  37. Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
  38.  
  39. void displaySensorDetails(void)
  40. {
  41. sensor_t sensor;
  42. mag.getSensor(&sensor);
  43. Serial.println("------------------------------------");
  44. Serial.print ("Sensor: "); Serial.println(sensor.name);
  45. Serial.print ("Driver Ver: "); Serial.println(sensor.version);
  46. Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
  47. Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
  48. Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
  49. Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
  50. Serial.println("------------------------------------");
  51. Serial.println("");
  52. delay(1);
  53. }
  54.  
  55. void setup(void)
  56. {
  57. Serial.begin(9600);
  58. Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
  59. //
  60. // /* Initialise the sensor */
  61. if(!mag.begin())
  62. {
  63. // /* There was a problem detecting the HMC5883 ... check your connections */
  64. Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
  65. while(1);
  66. }
  67.  
  68. /* Display some basic information on this sensor */
  69. displaySensorDetails();
  70. }
  71.  
  72. void loop(void)
  73. {
  74. /* Get a new sensor event */
  75. sensors_event_t event;
  76. mag.getEvent(&event);
  77.  
  78. /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  79. Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
  80. Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
  81. Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
  82.  
  83. // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
  84. // Calculate heading when the magnetometer is level, then correct for signs of axis.
  85. float heading = atan2(event.magnetic.y, event.magnetic.x);
  86.  
  87. // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  88. // Find yours here: http://www.magnetic-declination.com/
  89. // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
  90. // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  91. float declinationAngle = 0.22;
  92. heading += declinationAngle;
  93.  
  94. // Correct for when signs are reversed.
  95. if(heading < 0)
  96. heading += 2*PI;
  97.  
  98. // Check for wrap due to addition of declination.
  99. if(heading > 2*PI)
  100. heading -= 2*PI;
  101.  
  102. // Convert radians to degrees for readability.
  103. float headingDegrees = heading * 180/M_PI;
  104.  
  105. Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
  106.  
  107. delay(500);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement