Search
 
SCRIPT & CODE EXAMPLE
 

CPP

HMC 5883 Example to return x y z values

#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 setup(void)
{
Serial.begin(9600);

/* 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);
}
}

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);
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: string class in c++ 
Cpp :: function for reversing an array c++ stl 
Cpp :: c++ Closest Pair of Points | O(nlogn) Implementation 
Cpp :: crud with template c++ 
Cpp :: pointers in cpp 
Cpp :: vector keyword in c++ 
Cpp :: CPP print executable name 
Cpp :: uint16_t in c++ 
Cpp :: idnefier endl in undefince 
Cpp :: c++ break statement 
Cpp :: kadane algo 
Cpp :: reverse the number codechef solution in c++ 
Cpp :: print all substrings in c++ 
Cpp :: c++ restrict template types 
Cpp :: dream speedrun song mp4 
Cpp :: c++ how to use and or in if 
Cpp :: gcd multi num 
Cpp :: Missing GL version 
Cpp :: comentar todas linhas de uma vez vs code 
Cpp :: variadic template constructor matches better than copy constructor 
Cpp :: sleep function i nc++ 
Cpp :: c++ bind what are placeholders 
Cpp :: C++ (.NET CLI) 
Cpp :: C++ OpenCV Face Recognition 
Cpp :: ue4 execute delegate from blueprint 
Cpp :: high school hacking competition 
Cpp :: warning in range-based for loop in C++. How to resolve it in vscode? 
Cpp :: How to make an array dynamically using pointers 
Cpp :: stack in c++ data structure 
Cpp :: simplest code for stack implementation in c++ 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =