SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmc5883p_get_data.ino
Go to the documentation of this file.
1
30#include <MagnetometerDrv.hpp>
31
32#ifndef SENSOR_SDA
33#define SENSOR_SDA 3
34#endif
35
36#ifndef SENSOR_SCL
37#define SENSOR_SCL 2
38#endif
39
41
42
43void setup()
44{
45 Serial.begin(115200);
46 while (!Serial);
47
48
49 if (!magnetometer.begin(Wire, QMC5883P_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
50 while (1) {
51 Serial.println("Failed to find QMC5883P - check your wiring!");
52 delay(1000);
53 }
54 }
55
56 // The desired output data rate in Hz. Allowed values are 10.0, 50.0, 100.0 and 200.0HZ.
57 float data_rate_hz = 200.0f;
58 // op_mode: Allowed values are SUSPEND, NORMAL, SINGLE_MEASUREMENT, CONTINUOUS_MEASUREMENT
60 // full_scale: Allowed values are FS_2G, FS_8G, FS_12G, FS_30G
62 // over_sample_ratio: Allowed values are OSR_1, OSR_2, OSR_4, OSR_8
64 // down_sample_ratio: Allowed values are DSR_1, DSR_2, DSR_4, DSR_8
66
67 /* Config Magnetometer */
69 op_mode,
70 full_scale,
71 data_rate_hz,
72 over_sample_ratio,
73 down_sample_ratio)) {
74 Serial.println("Magnetometer configured successfully.");
75 } else {
76 Serial.println("Magnetometer configuration failed.");
77 while (1);
78 }
79
81 Serial.print("Manufacturer: "); Serial.println(info.manufacturer);
82 Serial.print("Model: "); Serial.println(info.model);
83 Serial.print("I2C Address: 0x"); Serial.println(info.i2c_address, HEX);
84 Serial.print("Version: "); Serial.println(info.version);
85 Serial.print("UID: 0x"); Serial.println(info.uid);
86 Serial.print("Type: "); Serial.println(SensorUtils::typeToString(info.type));
87
89 Serial.print("DataRate: "); Serial.println(cfg.sample_rate);
90 Serial.print("FullScaleRange: "); Serial.println(cfg.range);
91 Serial.print("Mode: "); Serial.println((uint8_t)cfg.mode);
92 Serial.println();
93
94 //Find the magnetic declination : https://www.magnetic-declination.com/
95 float declination_deg = MagnetometerUtils::dmsToDecimalDegrees(-3, 20); // -3.3333
96
97 magnetometer.setDeclination(declination_deg);
98
99 Serial.print(" Magnetic Declination: ");
100 Serial.print(declination_deg, 2);
101 Serial.println("°");
102
103 Serial.print(" Sensitivity: ");
104 Serial.print(magnetometer.getSensitivity(), 6);
105 Serial.println(" Gauss/LSB");
106
107 delay(3000);
108
109 Serial.println("Read data now...");
110}
111
112void loop()
113{
114 MagnetometerData data;
115
116 if (magnetometer.readData(data)) {
117
118 // Gauss to μT
122
123 Serial.print("Mag:");
124 Serial.print(" X:"); Serial.print(x);
125 Serial.print(" Y:"); Serial.print(y);
126 Serial.print(" Z:"); Serial.print(z);
127 Serial.print(" μT");
128
129 Serial.print(" Sensitivity: ");
130 Serial.print(magnetometer.getSensitivity(), 6);
131 Serial.print(" Gauss/LSB");
132
133 Serial.print(" Metadata:");
134 Serial.print(" X:");
135 Serial.print(data.raw.x);
136 Serial.print(" Y:");
137 Serial.print(data.raw.y);
138 Serial.print(" Z:");
139 Serial.print(data.raw.z);
140
141 Serial.print(" Heading (rad): ");
142 Serial.print(data.heading, 6);
143 Serial.print(" rad");
144
145 Serial.print(" Heading (deg): ");
146 Serial.print(data.heading_degrees, 2);
147 Serial.print("°");
148
150 strength = MagnetometerUtils::gaussToMicroTesla(strength);
151 Serial.print(" Magnetic Strength: ");
152 Serial.print(strength, 2);
153 Serial.println(" μT");
154
155 if (data.overflow) {
156 Serial.println("\tWarning: Data Overflow occurred!");
157 }
158 }
159 delay(10);
160}
161
162
163
@license MIT License
OperationMode
Enumeration of sensor operation modes.
MagFullScaleRange
Enumeration defining full-scale range settings for the sensor.
MagDownSampleRatio
Enumeration defining down-sample ratios for the sensor.
@ DSR_1
1x down-sample ratio
MagOverSampleRatio
Enumeration defining over-sample ratios for the sensor.
@ OSR_1
1x over-sample ratio
bool begin(SensorCommCustom::CustomCallback cb, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialize the sensor using custom callback interface.
virtual void setDeclination(float declination_deg)
Set the magnetic declination correction value.
virtual SensorConfig getConfig() const
Get current sensor configuration.
float getSensitivity() const
Get sensor sensitivity.
SensorInfo getSensorInfo() const
Get sensor information.
bool configMagnetometer(OperationMode mode, MagFullScaleRange range, float odr, MagOverSampleRatio osr, MagDownSampleRatio dsr) override
Configures the magnetometer with multiple parameters.
bool readData(MagnetometerData &data) override
Reads the magnetic field data from the sensor.
static const char * typeToString(SensorType type)
Convert sensor type enumeration to string representation.
float dmsToDecimalDegrees(int degrees, int minutes, float seconds=0.0f)
Convert degrees-minutes-seconds to decimal degrees.
float calculateMagneticStrength(const MagnetometerData &data)
Calculate total magnetic field strength.
float gaussToMicroTesla(float gauss)
Converts Gauss to microTesla.
void setup()
#define SENSOR_SCL
#define SENSOR_SDA
SensorQMC5883P magnetometer
void loop()
Structure representing magnetometer data.
RawVector raw
raw data
float heading_degrees
heading angle (degrees)
float heading
heading angle (radians)
SensorVector magnetic_field
magnetic field strength (Gauss)
bool overflow
data overflow flag
int16_t x
X-axis raw value.
int16_t y
Y-axis raw value.
int16_t z
Z-axis raw value.
Structure representing sensor configuration parameters.
float sample_rate
Sample rate in Hz.
float range
Measurement range.
OperationMode mode
Operation mode.
Structure containing sensor identification information.
uint8_t version
Hardware/firmware version.
uint8_t i2c_address
Default I2C address.
uint32_t uid
Unique identifier.
SensorType type
Sensor type.
const char * manufacturer
Manufacturer name.
const char * model
Model name/identifier.
float x
X-axis component.
float y
Y-axis component.
float z
Z-axis component.
int16_t x[5]
int16_t y[5]