SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmc6309_get_data.ino
Go to the documentation of this file.
1
30#include <MagnetometerDrv.hpp>
31
32#ifdef ARDUINO_T_BEAM_S3_SUPREME
33#include <XPowersAXP2101.tpp> //PMU Library https://github.com/lewisxhe/XPowersLib.git
34#endif
35
36#ifndef SENSOR_SDA
37#define SENSOR_SDA 17
38#endif
39
40#ifndef SENSOR_SCL
41#define SENSOR_SCL 18
42#endif
43
45
47{
48#if defined(ARDUINO_T_BEAM_S3_SUPREME)
49 XPowersAXP2101 power;
50 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
51 power.disableALDO1();
52 power.disableALDO2();
53 delay(250);
54 power.setALDO1Voltage(3300);
55 power.enableALDO1();
56 power.setALDO2Voltage(3300);
57 power.enableALDO2();
58#endif
59}
60
61void setup()
62{
63 Serial.begin(115200);
64 while (!Serial);
65
66 // LilyGo T-Beam-Supreme sensor requires a power source to function.
67 beginPower();
68
69 if (!magnetometer.begin(Wire, QMC6309_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
70 while (1) {
71 Serial.println("Failed to find QMC6309 - check your wiring!");
72 delay(1000);
73 }
74 }
75
76 // Self-test
77 int16_t x_result = 0, y_result = 0, z_result = 0;
78 bool self_test_result = magnetometer.selfTest(x_result, y_result, z_result);
79 Serial.print("Self-test ");
80 Serial.println(self_test_result ? "passed" : "failed");
81 Serial.print("X-axis: "); Serial.println(x_result);
82 Serial.print("Y-axis: "); Serial.println(y_result);
83 Serial.print("Z-axis: "); Serial.println(z_result);
84
85 delay(3000);
86
87 // The desired output data rate in Hz. Allowed values are 1.0, 10.0, 50.0, 100.0 and 200.0HZ.
88 float data_rate_hz = 200.0f;
89 // op_mode: Allowed values are SUSPEND, NORMAL, SINGLE_MEASUREMENT, CONTINUOUS_MEASUREMENT
91 // full_scale: Allowed values are FS_8G, FS_16G ,FS_32G
93 // over_sample_ratio: Allowed values are OSR_1, OSR_2, OSR_4, OSR_8
95 // down_sample_ratio: QMC6309 does not support downsampling rate settings; this parameter is ignored.
97
98 /* Config Magnetometer */
100 op_mode,
101 full_scale,
102 data_rate_hz,
103 over_sample_ratio,
104 down_sample_ratio)) {
105 Serial.println("Magnetometer configured successfully.");
106 } else {
107 Serial.println("Magnetometer configuration failed.");
108 while (1);
109 }
110
112 Serial.print("Manufacturer: "); Serial.println(info.manufacturer);
113 Serial.print("Model: "); Serial.println(info.model);
114 Serial.print("I2C Address: 0x"); Serial.println(info.i2c_address, HEX);
115 Serial.print("Version: "); Serial.println(info.version);
116 Serial.print("UID: 0x"); Serial.println(info.uid);
117 Serial.print("Type: "); Serial.println(SensorUtils::typeToString(info.type));
118
120 Serial.print("DataRate: "); Serial.println(cfg.sample_rate);
121 Serial.print("FullScaleRange: "); Serial.println(cfg.range);
122 Serial.print("Mode: "); Serial.println((uint8_t)cfg.mode);
123 Serial.println();
124
125 //Find the magnetic declination : https://www.magnetic-declination.com/
126 float declination_deg = MagnetometerUtils::dmsToDecimalDegrees(-3, 20); // -3.3333
127
128 magnetometer.setDeclination(declination_deg);
129
130 Serial.print(" Magnetic Declination: ");
131 Serial.print(declination_deg, 2);
132 Serial.println("°");
133
134 Serial.print(" Sensitivity: ");
135 Serial.print(magnetometer.getSensitivity(), 6);
136 Serial.println(" Gauss/LSB");
137
138 delay(3000);
139
140 Serial.println("Read data now...");
141}
142
143void loop()
144{
145
146 MagnetometerData data;
147
148 if (magnetometer.readData(data)) {
149
150 // Gauss to μT
154
155 Serial.print("Mag:");
156 Serial.print(" X:"); Serial.print(x);
157 Serial.print(" Y:"); Serial.print(y);
158 Serial.print(" Z:"); Serial.print(z);
159 Serial.print(" μT");
160
161 Serial.print(" Metadata:");
162 Serial.print(" X:");
163 Serial.print(data.raw.x);
164 Serial.print(" Y:");
165 Serial.print(data.raw.y);
166 Serial.print(" Z:");
167 Serial.print(data.raw.z);
168
169 Serial.print(" Heading (rad): ");
170 Serial.print(data.heading, 6);
171 Serial.print(" rad");
172
173 Serial.print(" Heading (deg): ");
174 Serial.print(data.heading_degrees, 2);
175 Serial.print("°");
176
178 strength = MagnetometerUtils::gaussToMicroTesla(strength);
179 Serial.print(" Magnetic Strength: ");
180 Serial.print(strength, 2);
181 Serial.println(" μT");
182
183 if (data.overflow) {
184 Serial.println("\tWarning: Data Overflow occurred!");
185 }
186 }
187 delay(10);
188}
@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 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.
bool selfTest() override
Checks if the sensor is functioning correctly.
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
SensorQMC6309 magnetometer
void beginPower()
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]