]> git.piffa.net Git - sketchbook_andrea/blob - hardware/bussola/magsensor/magsensor.ino
bussola
[sketchbook_andrea] / hardware / bussola / magsensor / magsensor.ino
1 /***************************************************************************
2   This is a library example for the HMC5883 magnentometer/compass
3   
4   Collegamenti:
5   
6   Sensore    + - SCL SDA  (da SX.)
7   Arduino    + - A5  A4
8
9   Designed specifically to work with the Adafruit HMC5883 Breakout
10   http://www.adafruit.com/products/1746
11  
12   *** You will also need to install the Adafruit_Sensor library! ***
13
14   These displays use I2C to communicate, 2 pins are required to interface.
15
16   Adafruit invests time and resources providing this open source code,
17   please support Adafruit andopen-source hardware by purchasing products
18   from Adafruit!
19
20   Written by Kevin Townsend for Adafruit Industries with some heading example from
21   Love Electronics (loveelectronics.co.uk)
22  
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.
26  
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.
31
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/>.
34  
35  
36
37  ***************************************************************************/
38
39 #include <Wire.h>
40 #include <Adafruit_Sensor.h>
41 #include <Adafruit_HMC5883_U.h>
42
43 /* Assign a unique ID to this sensor at the same time */
44 Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
45
46 void displaySensorDetails(void)
47 {
48   sensor_t sensor;
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("------------------------------------");
58   Serial.println("");
59   delay(500);
60 }
61
62 void setup(void) 
63 {
64   Serial.begin(9600);
65   Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
66   
67   /* Initialise the sensor */
68   if(!mag.begin())
69   {
70     /* There was a problem detecting the HMC5883 ... check your connections */
71     Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
72     while(1);
73   }
74   
75   /* Display some basic information on this sensor */
76   displaySensorDetails();
77 }
78
79 void loop() 
80 {
81   /* Get a new sensor event */ 
82   sensors_event_t event; 
83   mag.getEvent(&event);
84  
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");
89
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);
93   
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;
100   
101   // Correct for when signs are reversed.
102   if(heading < 0)
103     heading += 2*PI;
104     
105   // Check for wrap due to addition of declination.
106   if(heading > 2*PI)
107     heading -= 2*PI;
108    
109   // Convert radians to degrees for readability.
110   float headingDegrees = heading * 180/M_PI; 
111   
112   Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
113   
114   delay(500);
115 }