Hi forum,

I in fact try to configure this sensor with my Bela mini.
I have read this topic >>>
https://forum.bela.io/d/925-using-uart1-on-bela-mini/25
And many orders too, Bela.io tutorials...

But as my coding experience is 0, I'm not able to add the appropriate render.cpp file to monitor the UART output.
I'll use the r serial-uart4 object in Pure Data.

Help appreciated !
Many thank in advance

All the best

I've found on the DFrobot website this code for arduino


/*
  *@File  : DFRobot_Distance_A02.ino 
  *@Brief : This example use A02YYUW ultrasonic sensor to measure distance
  *         With initialization completed, We can get distance value 
  *@Copyright [DFRobot](https://www.dfrobot.com),2016         
  *           GUN Lesser General Pulic License
  *@version V1.0           
  *@data  2019-8-28
*/

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11,10); // RX, TX
unsigned char data[4]={};
float distance;

void setup()
{
 Serial.begin(57600);
 mySerial.begin(9600); 
}

void loop()
{
    do{
     for(int i=0;i<4;i++)
     {
       data[i]=mySerial.read();
     }
  }while(mySerial.read()==0xff);

  mySerial.flush();

  if(data[0]==0xff)
    {
      int sum;
      sum=(data[0]+data[1]+data[2])&0x00FF;
      if(sum==data[3])
      {
        distance=(data[1]<<8)+data[2];
        if(distance>30)
          {
           Serial.print("distance=");
           Serial.print(distance/10);
           Serial.println("cm");
          }else 
             {
               Serial.println("Below the lower limit");
             }
      }else Serial.println("ERROR");
     }
     delay(100);
}

As there seems to be no writes to the sensor via myserial, then it would seem that reading from it is enough.

That code is a bit weird (the 4 reads in the for() followed by one in the read()).
Anyhow, looking at the specs, it seems you should be fine just reading four bytes at a time, verify that the first one is 255, then consider the next two ones and ignore the fourth (checksum, or use it to validate the others)
alt text

I think you should be able to:

  • update your board to the dev branch (updating to the experimental release at the bottom here).
  • connect your device's GND to Bela Mini's GND and the device's UART Tx pin to BelaMini's P2.05
  • run this in the terminal at the bottom of the IDE: config-pin P2.05 uart
  • create and run the following patch:

alt text

    giuliomoro

    So many thanks !

    I just followed your step by step instructions.
    And here is what appears on the IDE.

    How does it sound for you ?
    alt text

      And after a reboot >>>

      alt text

      It seems to read something, but it only prints 0 or 0 0

      It's probably due to the warning message about 'unsigned int' and 'float' in my previous post ?

      Thanks again

        heliopolarthing How does it sound for you ?

        this looks like you created an object [new myserial ...] instead of a message

        heliopolarthing It's probably due to the warning message about 'unsigned int' and 'float' in my previous post ?

        no

        heliopolarthing It seems to read something, but it only prints 0 or 0 0

        Remember to always run the config-pin line upon reboot.

        heliopolarthing nd here is what appears when I use symbols instead of floats in the pd message

        you shouldn't do that.

        Upon further inspection reading the code, I think floats the way it is implemented currently expects to receive a string representing a float from the serial port, so it won't work for you. Update your board to dev branch again, where I just pushed some changes that should allow you to use

        [new myserial /dev/ttyS4 9600 255 bytes (

        and try that.

          6 days later

          giuliomoro

          So many thanks again, Indeed, I first made a mistake by creating an object rather than a message 🙁

          So I followed all your valuable information, and it works!!
          You really are by far the best 🙂
          Here is a screenshot of the print results.
          Also, you can see my pd patch which uses DFRobot's specs for distance calculation.
          alt text