SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmc5883l_get_data.ino
Go to the documentation of this file.
1
30#include <MagnetometerDrv.hpp>
31
32
33#ifndef SENSOR_SDA
34#define SENSOR_SDA 27
35#endif
36
37#ifndef SENSOR_SCL
38#define SENSOR_SCL 26
39#endif
40
42
43void setup()
44{
45 Serial.begin(115200);
46 while (!Serial);
47
48 if (!magnetometer.begin(Wire, QMC5883L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
49 while (1) {
50 Serial.println("Failed to find QMC5883L - check your wiring!");
51 delay(1000);
52 }
53 }
54
55 // The desired output data rate in Hz. Allowed values are 10.0, 50.0, 100.0 and 200.0HZ.
56 float data_rate_hz = 200.0f;
57 // op_mode: Allowed values are SUSPEND, NORMAL, SINGLE_MEASUREMENT, CONTINUOUS_MEASUREMENT
59 // full_scale: Allowed values are FS_2G, FS_8G
61 // over_sample_ratio: Allowed values are OSR_1, OSR_2, OSR_4, OSR_8
63
64 /* Config Magnetometer */
66 op_mode,
67 full_scale,
68 data_rate_hz,
69 over_sample_ratio)) {
70 Serial.println("Magnetometer configured successfully.");
71 } else {
72 Serial.println("Magnetometer configuration failed.");
73 while (1);
74 }
75
77 Serial.print("Manufacturer: "); Serial.println(info.manufacturer);
78 Serial.print("Model: "); Serial.println(info.model);
79 Serial.print("I2C Address: 0x"); Serial.println(info.i2c_address, HEX);
80 Serial.print("Version: "); Serial.println(info.version);
81 Serial.print("UID: 0x"); Serial.println(info.uid);
82 Serial.print("Type: "); Serial.println(SensorUtils::typeToString(info.type));
83
85 Serial.print("DataRate: "); Serial.println(cfg.sample_rate);
86 Serial.print("FullScaleRange: "); Serial.println(cfg.range);
87 Serial.print("Mode: "); Serial.println((uint8_t)cfg.mode);
88 Serial.println();
89
90 //Find the magnetic declination : https://www.magnetic-declination.com/
91 float declination_deg = MagnetometerUtils::dmsToDecimalDegrees(-3, 20); // -3.3333
92
93 magnetometer.setDeclination(declination_deg);
94
95 Serial.print(" Magnetic Declination: ");
96 Serial.print(declination_deg, 2);
97 Serial.println("°");
98
99 Serial.print(" Sensitivity: ");
100 Serial.print(magnetometer.getSensitivity(), 6);
101 Serial.println(" Gauss/LSB");
102
103 delay(3000);
104
105 Serial.println("Read data now...");
106}
107
108
109void loop()
110{
111 MagnetometerData data;
112
113 if (magnetometer.readData(data)) {
114
115 // Gauss to μT
119
120 Serial.print("Mag:");
121 Serial.print(" X:"); Serial.print(x);
122 Serial.print(" Y:"); Serial.print(y);
123 Serial.print(" Z:"); Serial.print(z);
124 Serial.print(" μT");
125
126 Serial.print(" Sensitivity: ");
127 Serial.print(magnetometer.getSensitivity(), 6);
128 Serial.print(" Gauss/LSB");
129
130 Serial.print(" Metadata:");
131 Serial.print(" X:");
132 Serial.print(data.raw.x);
133 Serial.print(" Y:");
134 Serial.print(data.raw.y);
135 Serial.print(" Z:");
136 Serial.print(data.raw.z);
137
138 Serial.print(" Heading (rad): ");
139 Serial.print(data.heading, 6);
140 Serial.print(" rad");
141
142 Serial.print(" Heading (deg): ");
143 Serial.print(data.heading_degrees, 2);
144 Serial.print("°");
145
147 strength = MagnetometerUtils::gaussToMicroTesla(strength);
148 Serial.print(" Magnetic Strength: ");
149 Serial.print(strength, 2);
150 Serial.println(" μT");
151
152 if (data.overflow) {
153 Serial.println("\tWarning: Data Overflow occurred!");
154 }
155 if (data.skip_data) {
156 Serial.println("\tWarning: Data Skip occurred!");
157 }
158 }
159 delay(50);
160}
@license MIT License
OperationMode
Enumeration of sensor operation modes.
MagFullScaleRange
Enumeration defining full-scale range settings for the sensor.
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 data_rate_hz, MagOverSampleRatio osr, MagDownSampleRatio dsr=MagDownSampleRatio::DSR_1) 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
SensorQMC5883L magnetometer
void loop()
Structure representing magnetometer data.
RawVector raw
raw data
float heading_degrees
heading angle (degrees)
float heading
heading angle (radians)
bool skip_data
skip data flag
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]