safwan092

Untitled

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