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