Ultrasonic unit reads erroneous distance

  • Hello, I have a M5 CoreInk onto which I have plugged a Ultrasonic sensor. I am getting a buggy distance read all the time.

    [-] bad distance: 4294967.500000
    [-] bad distance: 4294967.500000
    [-] bad distance: 4294967.500000

    My code is very much taken from this example.

    This is how I initialized Wire. I am not sure about this Wire.begin(21,22). It is taken from this code, but this other code uses other values...

    void setup() {
        M5.begin(); //Init CoreInk.  
        if( !M5.M5Ink.isInit()) //Init CoreInk screen.
            Serial.printf("[-] M5Ink init failed");  
            while (1) delay(100);
        Serial.printf("[+] Wire begin done\n");
        M5.M5Ink.clear();   //Clear screen.  

    And this is how I read distance. This looks good to me...

    #define M5_I2C_ADDR_ULTRASONIC 0x57
    float readDistance() {
      uint32_t data;
      data  = Wire.read();data <<= 8;
      data |= Wire.read();data <<= 8;
      data |= Wire.read();
      return float(data) / 1000; // distance in mm

    and then the loop - can't see anything wrong either...

    void loop() {
      static float newvalue = 0;
      newvalue = readDistance();
      if ((newvalue < 1500) && (newvalue > 0)) {
        Serial.printf("Distance: ",newvalue);
      } else {
        Serial.printf("[-] bad distance: %f\n",newvalue);

    Can you help me spot the error? Or is my sensor faulty? or is something different with CoreInk?


  • I fixed my problem using


    The distance now makes sense. I don't know what these values are for, though...

    Also, my distance is not precise. I'll investigate and post again if the problem persists.

  • The distances I get are really strange:

    • 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.
    • The values change for the same distance. So different measures of ~200cm gives 13.7cm or 79.6cm the next time...

    The command Wire.begin(32,33) seems correct as I realize those are the ports for the sensor on the CoreInk.

    So, what am I doing wrong?