1 /***************************************************************************
2 This is a library example for the HMC5883 magnentometer/compass
6 Sensore + - SCL SDA (da SX.)
9 Designed specifically to work with the Adafruit HMC5883 Breakout
10 http://www.adafruit.com/products/1746
12 *** You will also need to install the Adafruit_Sensor library! ***
14 These displays use I2C to communicate, 2 pins are required to interface.
16 Adafruit invests time and resources providing this open source code,
17 please support Adafruit andopen-source hardware by purchasing products
20 Written by Kevin Townsend for Adafruit Industries with some heading example from
21 Love Electronics (loveelectronics.co.uk)
23 This program is free software: you can redistribute it and/or modify
24 it under the terms of the version 3 GNU General Public License as
25 published by the Free Software Foundation.
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program. If not, see <http://www.gnu.org/licenses/>.
37 ***************************************************************************/
40 #include <Adafruit_Sensor.h>
41 #include <Adafruit_HMC5883_U.h>
43 /* Assign a unique ID to this sensor at the same time */
44 Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
46 void displaySensorDetails(void)
49 mag.getSensor(&sensor);
50 Serial.println("------------------------------------");
51 Serial.print ("Sensor: "); Serial.println(sensor.name);
52 Serial.print ("Driver Ver: "); Serial.println(sensor.version);
53 Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
54 Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
55 Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
56 Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
57 Serial.println("------------------------------------");
65 Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
67 /* Initialise the sensor */
70 /* There was a problem detecting the HMC5883 ... check your connections */
71 Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
75 /* Display some basic information on this sensor */
76 displaySensorDetails();
81 /* Get a new sensor event */
82 sensors_event_t event;
85 /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
86 Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
87 Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
88 Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
90 // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
91 // Calculate heading when the magnetometer is level, then correct for signs of axis.
92 float heading = atan2(event.magnetic.y, event.magnetic.x);
94 // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
95 // Find yours here: http://www.magnetic-declination.com/
96 // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
97 // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
98 //float declinationAngle = 0.22;
99 //heading += declinationAngle;
101 // Correct for when signs are reversed.
105 // Check for wrap due to addition of declination.
109 // Convert radians to degrees for readability.
110 float headingDegrees = heading * 180/M_PI;
112 Serial.print("Heading (degrees): "); Serial.println(headingDegrees);