A02YYUW ultrasonic sensor code (mkr wifi 1010)

I'm using a MKR wifi 1010 for a project and want to code the ultrasonic sensor A02YYUW . i found a code on the forum and changed the softwareserial.h to Serial1 so it works with my board (attached below). But i only keep getting Error in my serial monitor and not a reading. Any solution ?


unsigned char data[4]={};
float distance;

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

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

    Serial1.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);
}

check the baudrates.
You have Serial.begin(57600) but you have opened your Serial Monitor at 9600.

This is my version of a A02YYUW sketch:
https://werner.rothschopf.net/microcontroller/202201_a02yyuw_ultrasonic_sensor_en.htm

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.