-->
Page 2 of 2

Re: A02yyuw ultrasonic sensor with Esp8266 issue

PostPosted: Sun Mar 12, 2023 3:15 am
by torjays455
In case someone else comes looking for an answer in the future, I figured out how to make this sensor work with an ESP8266 and ESP32. It turns out that you have to provide some signal/random noise on the RX pin of the A02YYUW sensor before taking a reading. I found this out by randomly jiggling the RX wire of the sensor after being frustrated for hours trying to get the sensor to output any measured distance values. The change of voltage levels on the RX pin made it magically work. So I added the following code to make the sensor get actual readings instead of ERRORing out.

Code: Select allvoid GetReading()
{
sensorSoftwareSerial.println("1"); // ** Put this line before your sensor reading code is **

// Code to perform the sensor reading goes here
}


Note that the value passed into println() doesn't matter. You can put any text in there (or even an empty string). This code line will cause the TX line on the ESP8266/ESP32 (which is connected to the RX of the sensor) to generate signals and thus "activating" the sensor.

Re: A02yyuw ultrasonic sensor with Esp8266 issue

PostPosted: Thu Apr 06, 2023 8:09 am
by nathan3000
Hello,
I have been unsuccessful so far despite following you advice.
Could you please have a quick look?
Thanks a lot
Code: Select all#include <SoftwareSerial.h>

// Ultrasonic Sensor
SoftwareSerial mySerial(10,9); // RX, TX

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

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

void loop() {

  mySerial.println("1"); // ** Put this line before your sensor reading code is **

  //distance sensor start

      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(1000);

  //distance sensor end
}