<?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[Ultrasonic unit reads erroneous distance]]></title><description><![CDATA[<p dir="auto">Hello, I have a <strong>M5 CoreInk</strong> onto which I have plugged a <a href="https://docs.m5stack.com/en/unit/sonic" target="_blank" rel="noopener noreferrer nofollow ugc"><strong>Ultrasonic sensor</strong></a>. I am getting a <strong>buggy distance</strong> read all the time.</p>
<pre><code>[-] bad distance: 4294967.500000
[-] bad distance: 4294967.500000
[-] bad distance: 4294967.500000
</code></pre>
<p dir="auto">My code is very much taken from <a href="https://github.com/m5stack/M5Stack/blob/master/examples/Unit/ULTRA_SONIC/ULTRA_SONIC.ino" target="_blank" rel="noopener noreferrer nofollow ugc">this example</a>.</p>
<p dir="auto">This is how I initialized Wire. I am not sure about this <code>Wire.begin(21,22)</code>. It is taken from <a href="https://github.com/m5stack/M5Stack/blob/master/examples/Unit/ULTRA_SONIC/ULTRA_SONIC.ino" target="_blank" rel="noopener noreferrer nofollow ugc">this code</a>, but this <a href="https://qiita.com/takeru@github/items/4f408e9edaf915f168d2" target="_blank" rel="noopener noreferrer nofollow ugc">other code uses other values</a>...</p>
<pre><code>void setup() {

    M5.begin(); //Init CoreInk.  
    if( !M5.M5Ink.isInit()) //Init CoreInk screen.
    {
        Serial.printf("[-] M5Ink init failed");  
        while (1) delay(100);
    }

    Wire.begin(21,22);
    Serial.printf("[+] Wire begin done\n");
    
    M5.M5Ink.clear();   //Clear screen.  
    ...
</code></pre>
<p dir="auto">And this is how I read distance. This looks good to me...</p>
<pre><code>#define M5_I2C_ADDR_ULTRASONIC 0x57
float readDistance() {
  uint32_t data;
  Wire.beginTransmission(M5_I2C_ADDR_ULTRASONIC); 
  Wire.write(0x01);
  Wire.endTransmission(); 
  delay(120);
  Wire.requestFrom(M5_I2C_ADDR_ULTRASONIC,3); 
  data  = Wire.read();data &lt;&lt;= 8;
  data |= Wire.read();data &lt;&lt;= 8;
  data |= Wire.read();
  return float(data) / 1000; // distance in mm
}
</code></pre>
<p dir="auto">and then the <code>loop</code> - can't see anything wrong either...</p>
<pre><code>void loop() {
  static float newvalue = 0;
  newvalue = readDistance();
  if ((newvalue &lt; 1500) &amp;&amp; (newvalue &gt; 0)) {
    Serial.printf("Distance: ",newvalue);
  } else {
    Serial.printf("[-] bad distance: %f\n",newvalue);
  
  }
  delay(100);

}
</code></pre>
<p dir="auto">Can you help me spot the error? Or is my sensor faulty? or is something different with CoreInk?</p>
<p dir="auto">Thanks</p>
]]></description><link>https://community.m5stack.com/topic/4122/ultrasonic-unit-reads-erroneous-distance</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 02:29:38 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/4122.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 14 Mar 2022 10:49:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Ultrasonic unit reads erroneous distance on Mon, 14 Mar 2022 13:30:36 GMT]]></title><description><![CDATA[<p dir="auto">The distances I get are really <em>strange</em>:</p>
<ul>
<li>The values are well below or well above the expected ones. For example, for a distance of ~95cm the sensor answers 4.5cm. For a distance of ~110cm, 4.9cm. For ~35cm, 3.7cm etc.</li>
<li>The values change for the same distance. So different measures of ~200cm gives 13.7cm or 79.6cm the next time...</li>
</ul>
<p dir="auto">The command <code>Wire.begin(32,33)</code> seems correct as I realize those are the ports for the sensor on the CoreInk.</p>
<p dir="auto">So, what am I doing wrong?</p>
]]></description><link>https://community.m5stack.com/post/16924</link><guid isPermaLink="true">https://community.m5stack.com/post/16924</guid><dc:creator><![CDATA[aafortinet]]></dc:creator><pubDate>Mon, 14 Mar 2022 13:30:36 GMT</pubDate></item><item><title><![CDATA[Reply to Ultrasonic unit reads erroneous distance on Mon, 14 Mar 2022 12:30:42 GMT]]></title><description><![CDATA[<p dir="auto">I fixed my problem using</p>
<pre><code>Wire.begin(32,33);
</code></pre>
<p dir="auto">The distance now makes sense. I don't know what these values are for, though...</p>
<p dir="auto">Also, my distance is not precise. I'll investigate and post again if the problem persists.</p>
]]></description><link>https://community.m5stack.com/post/16922</link><guid isPermaLink="true">https://community.m5stack.com/post/16922</guid><dc:creator><![CDATA[aafortinet]]></dc:creator><pubDate>Mon, 14 Mar 2022 12:30:42 GMT</pubDate></item></channel></rss>