A01NYUB Ultrasonic Sensor

Hai There,
Recently I bought this A01NYUB Waterproof Ultrasonic Sensor.
I got the details of the sensor from the same website itself. Actually, I wanted to print the distance in the serial monitor of Arduino (In inch). So I modified the code a little bit to print in inches and it is working without any problem.

But my problem is that I want to add one more sensor (The same sensor) but measuring a different distance. And display both the distance in the serial monitor like Sensor1-__ , Sensor2-__ . I tried it by myself but there was a lot of error and both the distances were wrong... Can somebody modify the code for me, please... I only know the basics...

The original code can be found on the website which I mentioned before. The below code is the one that I modified.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,10); 

unsigned char S1[4]={};// I am not sure what is the use of the array in this code.
int i,SensorMM,sum,SensorCM,Inch,Approx;

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

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

     mySerial.flush();

    if(S1[0]==0xff)
     {
        sum=(S1[0]+S1[1]+S1[2])&0x00FF;
       
    if(sum==S1[3])
       {
        SensorMM=(S1[1]<<8)+S1[2];
       
    if(SensorMM>280)
          {
              SensorCM=SensorMM/10;
              Inch=SensorCM/2.54;//I want the output in Inch in the serial monitor.
              Approx = (int)(Inch+0.5);// I just want a single-digit value (without and decimal) in the serial monitor. Here I am rounding the value to the nearest one.  
              Serial.print(Approx);
              Serial.print("Inch"); 
              delay(100);
           }else 
               { 
                 Serial.println("Too Close");      
               }
         }else Serial.println("ERROR");
      }
       delay(150);
}

Thanks,
Akash

Hello and welcome to the forum akash.
Well done posting your code as a code-section

When I see somebody new on the forum asking for "can somebody write code for me?"

I'm looking up the user
Your info is:

Posted 3 hours ago

Joined 6 hours ago

Read 4m

So this means you have spent a minimum time on the forum asking for almost the maximum of work that others shall do for you.
To this I say NO!

If you want my help - the minimum what you have to do is:

  • providing the original demo-sketch

  • posting the serial-output you get when using the original demo-code

  • posting the serial-output you get when using your modified version of the code.

  • a more or less specific question about what detail is unclear to you.

best regards Stefan

Thanks for the reply.
This is my new account. For this long, I've been using my parent's email address and all. So I wanted to create an account using my credentials.

I am not a programmer. I just know the basics. I can understand simple codes which are not too complicated but I cannot write any programs. I am just learning to program.

  • The original demo code is in the link that I mentioned.

  • There is nothing much in the serial monitor when running the demo code. It just displays the distance (distance=___cm).

  • When running the modified program, I get the distance in Inch (___Inch).

  1. My problem I that, I don't know how to add one more sensor to the code. ie: How to define the pins of the second sensor?

  2. How to differentiate between the two sensors in the code.

  3. I tried to add the second sensor in the code but there are lots of errors and I completely messed up with the curly brackets which I think is the reason for the errors.

  4. Also can you explain why are we using array in the program? This sensor is the same as HC-SR04 Ultrasonic Sensor. But the program is entirely different.

Regards,
Akash

You are talking to your sensor using SoftwareSerial on pins 11 and 10. You will need to create another instance of SoftwareSerial using different pins and a different name.

You should also google how to use two instances of SoftwareSerial since you can not just simply use both of them. You have to select 1, read sensor 1 and then select the other one and read that.

Apparently it is NOT the same because the HC-SR04 uses a trig pin and an echo pin not a serial interface.

1 Like

Yeah I noticed it but I thought that the function is same and working principal.. Thanks

So will it work if I use two softwareSerial with different names and pins. And also I should copy paste the rest of the code with different variables?

It sounds like you should take some time to study some C/C++ tutorials and/or check out all the examples that come with the Arduino IDE...

No. As blh64 said there is a different technique for using two software serial ports. You could have easily found examples on the same sight this forum is on.

https://www.arduino.cc/en/Tutorial/LibraryExamples/TwoPortReceive

I haven't developed the sensor. This sensor could behave in thousands of ways. So I don't know nothing about the behaviour of the sensor.
Posting the original serial output 1 : 1 that you get from the original demo-code - even if it is only very very few characters what gets printed gives information about the behaviour of the sensor. And this is important.

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