Arduino – Kompas 3-assen (HMC5883L)
Hardware
Met deze module kun je heel gedetailleerd het magnetische veld “in beeld” brengen, en zo gevoelig dat het het magnetisch veld van de aarde kan waarnemen en de ze sensor chip kan worden gebruikt als kompas.
De module gebruikt I2C als gegevensoverdracht, zodoende is 4 aansluitingen voldoende om de module te laten functioneren.
Informatie over de HMC5883L chip (ENG):
The Honeywell HMC5883L is a surface-mount, multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as lowcost compassing and magnetometry. The HMC5883L includes our state-of-theart, high-resolution HMC118X series magneto-resistive sensors plus an ASIC containing amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2° compass heading accuracy. The I 2C serial bus allows for easy interface. The HMC5883L is a 3.0×3.0x0.9mm surface mount 16-pin leadless chip carrier (LCC). Applications for the HMC5883L include Mobile Phones, Netbooks, Consumer Electronics, Auto Navigation Systems, and Personal Navigation Devices.
The HMC5883L utilizes Honeywell’s Anisotropic Magnetoresistive (AMR) technology that provides advantages over other magnetic sensor technologies. These anisotropic, directional sensors feature precision in-axis sensitivity and linearity. These sensors’ solid-state construction with very low cross-axis sensitivity is designed to measure both the direction and the magnitude of Earth’s magnetic fields, from milli-gauss to 8 gauss. Honeywell’s Magnetic Sensors are among the most sensitive and reliable low-field sensors in the industry.
Pinout
Pin (bovenkant met pin headers naar je toe): | Functie: |
1 | +5v |
2 | GND |
3 | SCL (serial clock) |
4 | SDA (serial data) |
5 | DRDY |
6 | +3.3v |
Aansluiten op de Arduino
Sluit de Kompas module aan volgens onderstaand overzicht:
Pin HMC5883L (bovenkant met pin headers naar je toe): | Pin Arduino: |
---|---|
+5v | +5v |
GND | GND |
SCL (serial clock) | A5 |
SDA (serial data) | A4 |
NC | DRDY |
NC | +3.3v |
Script Adafruit
Wat heb je nodig?
1) Adafruit sensor bibliotheek
2) Adafruit HMC5883L Driver
Sluit de kompas module aan zoals hierboven aangegeven.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
/*************************************************************************** This is a library example for the HMC5883 magnentometer/compass Designed specifically to work with the Adafruit HMC5883 Breakout http://www.adafruit.com/products/1746 *** You will also need to install the Adafruit_Sensor library! *** These displays use I2C to communicate, 2 pins are required to interface. Adafruit invests time and resources providing this open source code, please support Adafruit andopen-source hardware by purchasing products from Adafruit! Written by Kevin Townsend for Adafruit Industries with some heading example from Love Electronics (loveelectronics.co.uk) This program is free software: you can redistribute it and/or modify it under the terms of the version 3 GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. ***************************************************************************/ #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_HMC5883_U.h> /* Assign a unique ID to this sensor at the same time */ Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345); void displaySensorDetails(void) { sensor_t sensor; mag.getSensor(&sensor); Serial.println("------------------------------------"); Serial.print ("Sensor: "); Serial.println(sensor.name); Serial.print ("Driver Ver: "); Serial.println(sensor.version); Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT"); Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT"); Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT"); Serial.println("------------------------------------"); Serial.println(""); delay(500); } void setup(void) { Serial.begin(9600); Serial.println("HMC5883 Magnetometer Test"); Serial.println(""); /* Initialise the sensor */ if(!mag.begin()) { /* There was a problem detecting the HMC5883 ... check your connections */ Serial.println("Ooops, no HMC5883 detected ... Check your wiring!"); while(1); } /* Display some basic information on this sensor */ displaySensorDetails(); } void loop(void) { /* Get a new sensor event */ sensors_event_t event; mag.getEvent(&event); /* Display the results (magnetic vector values are in micro-Tesla (uT)) */ Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" "); Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" "); Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT"); // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y // Calculate heading when the magnetometer is level, then correct for signs of axis. float heading = atan2(event.magnetic.y, event.magnetic.x); // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location. // Find yours here: http://www.magnetic-declination.com/ // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians // If you cannot find your Declination, comment out these two lines, your compass will be slightly off. float declinationAngle = 0.22; heading += declinationAngle; // Correct for when signs are reversed. if(heading < 0) heading += 2*PI; // Check for wrap due to addition of declination. if(heading > 2*PI) heading -= 2*PI; // Convert radians to degrees for readability. float headingDegrees = heading * 180/M_PI; Serial.print("Heading (degrees): "); Serial.println(headingDegrees); delay(500); } |
Het resultaat
Script HMC5883L (Creative Electronics)
Wat heb je nodig?
1) HMC5883L (Creative Electronics) bibliotheek
Sluit de kompas module aan zoals hierboven aangegeven.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
/* HMC5883L_Example.ino - Example sketch for integration with an HMC5883L triple axis magnetometer. Copyright (C) 2013 BluLemonLabs (bluelemonlabs.blogspot.com) This program is free software: you can redistribute it and/or modify it under the terms of the version 3 GNU General Public License as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ // Reference the I2C Library #include <Wire.h> // Reference the HMC5883L Compass Library #include <HMC5883L.h> // Store our compass as an object. HMC5883L compass; // Record any errors that may occur in the compass. int error = 0; // Out setup routine, here we will configure the microcontroller and compass. void setup() { // Initialize the serial port. Serial.begin(9600); Serial.println(""); Serial.println("Starting the I2C interface."); Wire.begin(); // Start the I2C interface. Serial.println("Constructing new HMC5883L"); compass = HMC5883L(); // Construct a new HMC5883 compass. //The implementation of the class is provided in the library // Now we have an istance of the class! //Let's initializate it... Serial.println("Setting scale to +/- 1.3 Ga"); error = compass.SetScale(1.3); // Set the scale of the compass to 1.3Ga if(error != 0){ // If there is an error, print it out. Serial.println(compass.GetErrorText(error)); error =0; } Serial.println("Setting measurement mode to continous."); error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous if(error != 0) {// If there is an error, print it out. Serial.println(compass.GetErrorText(error)); //Todo: Error handling for this method in .h and .cpp error=0; } } // Our main program loop. void loop() { // Retrieve the raw values from the magnetometer (not scaled). MagnetometerRaw raw = compass.ReadRawAxis(); // Retrieve the scaled values from the magnetometer (scaled to the configured scale). MagnetometerScaled scaled = compass.ReadScaledAxis(); // Values are accessed like so: int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis) // Calculate heading when the magnetometer is level, then correct for signs of axis. // Atan2() automatically check the correct formula taking care of the quadrant you are in float heading = atan2(scaled.YAxis, scaled.XAxis); // Once you have your heading, you must then add your 'Declination Angle', // which is the 'Error' of the magnetic field in your location. Mine is 0.0404 // Find yours here: http://www.magnetic-declination.com/ // If you cannot find your Declination, comment out these two lines, your compass will be slightly off. float declinationAngle = 0.0404; heading += declinationAngle; // Correct for when signs are reversed. if(heading < 0) heading += 2*PI; // Check for wrap due to addition of declination. if(heading > 2*PI) heading -= 2*PI; // Convert radians to degrees for readability. float headingDegrees = heading * 180/M_PI; // Output the data via the serial port. Output(raw, scaled, heading, headingDegrees); // By default the HMC5883L reads the data 15 time per second (15Hz) // However since we have a long serial out (104ms at 9600) we will let // it run at its natural speed. // delay(66); } // Output the data down the serial port. void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees) { Serial.print("Raw:\t"); Serial.print(raw.XAxis); Serial.print(" "); Serial.print(raw.YAxis); Serial.print(" "); Serial.print(raw.ZAxis); Serial.print(" \tScaled:\t"); Serial.print(scaled.XAxis); Serial.print(" "); Serial.print(scaled.YAxis); Serial.print(" "); Serial.print(scaled.ZAxis); Serial.print(" \tHeading:\t"); Serial.print(heading); Serial.print(" Radians \t"); Serial.print(headingDegrees); Serial.println(" Degrees \t"); } |
Het resultaat
Als je een app op de telefoon vergelijkt, zie je dat de waarden dicht bij elkaar liggen:
ijken
[TODO]
Bronnen:
learn.adafruit.com
bluelemonlabs.blogspot.nl
[#/datasheets/hmc5883l” ]