<?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[Compass Data from M5stack Fire]]></title><description><![CDATA[<p dir="auto">I have the M5Stack fire (Version:2018.2A) but having difficulties in getting magnometer data.<br />
Previously uiflow had the block get Z but it is removed at a later stage.<br />
I tried below Arduino code but readings are incorrect.<br />
Could anyone assist?</p>
<p dir="auto">#include &lt;M5Stack.h&gt;<br />
#include "utility/MPU9250.h"</p>
<p dir="auto">MPU9250 IMU;</p>
<p dir="auto">void setup()<br />
{<br />
M5.begin();<br />
Wire.begin();</p>
<p dir="auto">IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);<br />
IMU.initMPU9250();<br />
IMU.initAK8963(IMU.magCalibration);<br />
}</p>
<p dir="auto">void loop()<br />
{<br />
// If intPin goes high, all data registers have new data<br />
// On interrupt, check if data ready interrupt<br />
if (IMU.readByte(MPU9250_ADDRESS, INT_STATUS) &amp; 0x01)<br />
{<br />
IMU.readAccelData(IMU.accelCount);<br />
IMU.getAres();</p>
<pre><code>IMU.ax = (float)IMU.accelCount[0] * IMU.aRes; // - accelBias[0];
IMU.ay = (float)IMU.accelCount[1] * IMU.aRes; // - accelBias[1];
IMU.az = (float)IMU.accelCount[2] * IMU.aRes; // - accelBias[2];

IMU.readGyroData(IMU.gyroCount);  // Read the x/y/z adc values
IMU.getGres();

// Calculate the gyro value into actual degrees per second
// This depends on scale being set
IMU.gx = (float)IMU.gyroCount[0] * IMU.gRes;
IMU.gy = (float)IMU.gyroCount[1] * IMU.gRes;
IMU.gz = (float)IMU.gyroCount[2] * IMU.gRes;

IMU.readMagData(IMU.magCount);  // Read the x/y/z adc values
IMU.getMres();
// User environmental x-axis correction in milliGauss, should be
// automatically calculated
//IMU.magbias[0] = +470.;
// User environmental x-axis correction in milliGauss TODO axis??
//IMU.magbias[1] = +120.;
// User environmental x-axis correction in milliGauss
//IMU.magbias[2] = +125.;

// Calculate the magnetometer values in milliGauss
// Include factory calibration per data sheet and user environmental
// corrections
// Get actual magnetometer value, this depends on scale being set
IMU.mx = (float)IMU.magCount[0] * IMU.mRes * IMU.magCalibration[0] -
         IMU.magbias[0];
IMU.my = (float)IMU.magCount[1] * IMU.mRes * IMU.magCalibration[1] -
         IMU.magbias[1];
IMU.mz = (float)IMU.magCount[2] * IMU.mRes * IMU.magCalibration[2] -
         IMU.magbias[2];



int x=64+10;
int y=128+20;
int z=192+30;

M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextColor(GREEN , BLACK);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(0, 0); M5.Lcd.print("MPU9250/AK8963");
M5.Lcd.setCursor(0, 32); M5.Lcd.print("x");
M5.Lcd.setCursor(x, 32); M5.Lcd.print("y");
M5.Lcd.setCursor(y, 32); M5.Lcd.print("z");



M5.Lcd.setCursor(0, 64 * 2); M5.Lcd.print((int)(IMU.gx));
M5.Lcd.setCursor(x, 64 * 2); M5.Lcd.print((int)(IMU.gy));
M5.Lcd.setCursor(y, 64 * 2); M5.Lcd.print((int)(IMU.gz));
M5.Lcd.setCursor(z, 64 * 2); M5.Lcd.print("o/s");

M5.Lcd.setCursor(0, 80 * 2); M5.Lcd.print((int)(IMU.mx));
M5.Lcd.setCursor(x, 80 * 2); M5.Lcd.print((int)(IMU.my));
M5.Lcd.setCursor(y, 80 * 2); M5.Lcd.print((int)(IMU.mz));
M5.Lcd.setCursor(z, 80 * 2); M5.Lcd.print("mG");

float headingRadians = atan2((IMU.my), (IMU.mx));
float headingDegrees = headingRadians * 180 / PI;
</code></pre>
<p dir="auto">if (headingDegrees &lt; 0) {<br />
headingDegrees += 360;<br />
}<br />
M5.Lcd.setTextColor(YELLOW , BLACK);<br />
M5.Lcd.setCursor(0, 48 * 2); M5.Lcd.print(headingDegrees);</p>
<pre><code>delay(100);
</code></pre>
<p dir="auto">}</p>
<p dir="auto">}</p>
]]></description><link>https://community.m5stack.com/topic/4491/compass-data-from-m5stack-fire</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 10:02:28 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/4491.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 02 Aug 2022 09:09:15 GMT</pubDate><ttl>60</ttl></channel></rss>