<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problems using MPU6886 (M5 Atom Matrix) with HMC5883L]]></title><description><![CDATA[<p dir="auto">Hello people!</p>
<p dir="auto">I am trying to use the IMU of M5 Atom Matrix with a HMC5883L (magnetometer), nevertheless, when i join the code of HMC5883L and MPU6886, the sensors don't work well (they give wrong informations about roll, pitch and yaw). The accelerometer of MPU6886 keeps the acceleration values freezed (this must be the reason roll and pitch show wrong values). Yaw values given by HMC5883L are strange too. When I test the two codes separated (HMC5883L is tested connected to M5 Atom Matrix), the sensors work perfectly fine. I use I2C ports 25 (SDA) and 21 (SCL). I've already used 21 (SDA) and 22 (SCL) too, but the same problems appear.</p>
<p dir="auto">My code is shown below:</p>
<p dir="auto">#include &lt;Wire.h&gt;<br />
#include &lt;HMC5883L.h&gt;<br />
#include "M5Atom.h"</p>
<p dir="auto">#define CALIBRATION__MAGN_USE_EXTENDED true</p>
<p dir="auto">const float magn_ellipsoid_center[3] = {-112.932, -417.847, -153.557};<br />
const float magn_ellipsoid_transform[3][3] = {{0.922957, 0.0179191, -0.0157219}, {0.0179191, 0.984359, 0.0385355}, {-0.0157219, 0.0385355, 0.890763}};<br />
float pitch, pitch1, roll, roll1, yaw, yaw1;<br />
float accX, accY, accZ, gyroX, gyroY, gyroZ, heading;<br />
float magnetom[3];<br />
float magnetom_tmp[3];</p>
<p dir="auto">HMC5883L compass;</p>
<p dir="auto">void setup()<br />
{<br />
// Enable Serial, enable I2C, enable Display<br />
M5.begin(true, true, true);<br />
<a href="//Wire.begin" target="_blank" rel="noopener noreferrer nofollow ugc">//Wire.begin</a>(25, 21);</p>
<p dir="auto">Serial.begin(115200);</p>
<p dir="auto">//   Initialize HMC5883L<br />
Serial.println("Initialize HMC5883L");<br />
while (!compass.begin())<br />
{<br />
Serial.println("Could not find a valid HMC5883L sensor, check wiring!");<br />
delay(300);<br />
}</p>
<p dir="auto">M5.IMU.Init();</p>
<p dir="auto">// Set measurement range<br />
// +/- 0.88 Ga: HMC5883L_RANGE_0_88GA<br />
// +/- 1.30 Ga: HMC5883L_RANGE_1_3GA (default)<br />
// +/- 1.90 Ga: HMC5883L_RANGE_1_9GA<br />
// +/- 2.50 Ga: HMC5883L_RANGE_2_5GA<br />
// +/- 4.00 Ga: HMC5883L_RANGE_4GA<br />
// +/- 4.70 Ga: HMC5883L_RANGE_4_7GA<br />
// +/- 5.60 Ga: HMC5883L_RANGE_5_6GA<br />
// +/- 8.10 Ga: HMC5883L_RANGE_8_1GA<br />
compass.setRange(HMC5883L_RANGE_1_9GA);</p>
<p dir="auto">// Set measurement mode<br />
// Idle mode:              HMC5883L_IDLE<br />
// Single-Measurement:     HMC5883L_SINGLE<br />
// Continuous-Measurement: HMC5883L_CONTINOUS (default)<br />
compass.setMeasurementMode(HMC5883L_CONTINOUS);</p>
<p dir="auto">// Set data rate<br />
//  0.75Hz: HMC5883L_DATARATE_0_75HZ<br />
//  1.50Hz: HMC5883L_DATARATE_1_5HZ<br />
//  3.00Hz: HMC5883L_DATARATE_3HZ<br />
//  7.50Hz: HMC5883L_DATARATE_7_50HZ<br />
// 15.00Hz: HMC5883L_DATARATE_15HZ (default)<br />
// 30.00Hz: HMC5883L_DATARATE_30HZ<br />
// 75.00Hz: HMC5883L_DATARATE_75HZ<br />
compass.setDataRate(HMC5883L_DATARATE_15HZ);</p>
<p dir="auto">// Set number of samples averaged<br />
// 1 sample:  HMC5883L_SAMPLES_1 (default)<br />
// 2 samples: HMC5883L_SAMPLES_2<br />
// 4 samples: HMC5883L_SAMPLES_4<br />
// 8 samples: HMC5883L_SAMPLES_8<br />
compass.setSamples(HMC5883L_SAMPLES_4);</p>
<p dir="auto">}</p>
<p dir="auto">void Matrix_Vector_Multiply(const float a[3][3], const float b[3], float out[3])<br />
{<br />
for(int x = 0; x &lt; 3; x++)<br />
{<br />
</p><div class="plugin-markdown"><input type="checkbox" />= a[x][0] * b[0] + a[x][1] * b[1] + a[x][2] * b[2];</div><br />
}<br />
}<p></p>
<p dir="auto">float tiltCompensate(Vector mag, float roll, float pitch)<br />
{</p>
<pre><code>// Some of these are used twice, so rather than computing them twice in the algorithem we precompute them before hand.
</code></pre>
<p dir="auto">float cosRoll = cos(roll);<br />
float sinRoll = sin(roll);<br />
float cosPitch = cos(pitch);<br />
float sinPitch = sin(pitch);</p>
<p dir="auto">// Tilt compensation<br />
float Xh = mag.XAxis * cosPitch + mag.ZAxis * sinPitch;<br />
float Yh = mag.XAxis * sinRoll * sinPitch + mag.YAxis * cosRoll - mag.ZAxis * sinRoll * cosPitch;</p>
<p dir="auto">float heading = atan2(Yh, Xh);</p>
<p dir="auto">return heading;<br />
}</p>
<p dir="auto">float correctAngle(float heading)<br />
{<br />
if (heading &lt; 0) { heading += 2 * PI; }<br />
if (heading &gt; 2 * PI) { heading -= 2 * PI; }</p>
<p dir="auto">return heading;<br />
}</p>
<p dir="auto">void loop()<br />
{<br />
Vector raw = compass.readRaw();<br />
Vector norm = compass.readNormalize();</p>
<p dir="auto">M5.IMU.getAccelData(&amp;accX, &amp;accY, &amp;accZ);<br />
M5.IMU.getGyroData(&amp;gyroX, &amp;gyroY, &amp;gyroZ);<br />
M5.IMU.getMadData(&amp;pitch, &amp;roll, &amp;yaw);</p>
<p dir="auto">heading = atan2(magnetom[1], magnetom[0]);<br />
//heading=tiltCompensate(norm, roll, pitch);</p>
<p dir="auto">float declinationAngle = -(43 + (23.0 / 60.0)) / (180 / M_PI);<br />
heading += declinationAngle;<br />
heading = correctAngle(heading);<br />
heading = heading * 180/M_PI;</p>
<p dir="auto">magnetom[0] = raw.XAxis;<br />
magnetom[1] = raw.YAxis;<br />
magnetom[2] = raw.ZAxis;</p>
<p dir="auto">//soft iron calibration<br />
#if CALIBRATION__MAGN_USE_EXTENDED == true<br />
for (int i = 0; i &lt; 3; i++)<br />
magnetom_tmp[i] = magnetom[i] - magn_ellipsoid_center[i];<br />
Matrix_Vector_Multiply(magn_ellipsoid_transform, magnetom_tmp, magnetom);<br />
//hard iron calibration in the HMC5883L.cpp code<br />
#else<br />
magnetom[0] = norm.XAxis;<br />
magnetom[1] = norm.YAxis;<br />
magnetom[2] = norm.ZAxis;<br />
#endif</p>
<p dir="auto">//NWU<br />
//MadgwickAHRSupdate(gyroX * DEG_TO_RAD, gyroY * DEG_TO_RAD, gyroZ * DEG_TO_RAD, accX, accY, accZ, magnetom[1], -magnetom[0], -magnetom[2], &amp;pitch1, &amp;roll1, &amp;yaw1);</p>
<p dir="auto">roll = abs(roll-90);</p>
<p dir="auto">Serial.printf("roll: %5.2f, pitch: %5.2f, heading: %5.2f", roll, pitch, heading);<br />
Serial.printf("\n");<br />
Serial.printf("ax: %5.2f, ay: %5.2f, az: %5.2f", accX, accY, accZ);<br />
Serial.printf(" g");<br />
Serial.printf("\n");<br />
//  Serial.printf("magX: %5.2f, magY: %5.2f, magZ: %5.2f", norm.XAxis, norm.YAxis, norm.ZAxis);<br />
//  Serial.printf("\n");</p>
<p dir="auto">delay(50);<br />
}</p>
]]></description><link>https://community.m5stack.com/topic/3635/problems-using-mpu6886-m5-atom-matrix-with-hmc5883l</link><generator>RSS for Node</generator><lastBuildDate>Fri, 15 May 2026 12:15:40 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/3635.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 Oct 2021 22:51:23 GMT</pubDate><ttl>60</ttl></channel></rss>