A01NYUB Waterproof Ultrasonic Sensor

I was unable to use this sensor with Arduino MKR 1010 Wi-Fi board as the SoftwareSerial library does not support CPU core frequency of 48MHz (only 20, 16 & 8) after extensively searching i could not find an alternative library so I switched to a UNO board however the sample code found here A01NYUB Waterproof Ultrasonic Sensor Wiki - DFRobot
does not work either. Any suggestions?

2 Likes

Hi James,

From the looks of the schematic, the MKR uses the native USB of the ATSAMD, rather than a USB-to-serial converter, so you should be able to use the single hardware serial on the board instead of softwareSerial on the example sketch.

Should be as simple as replacing the SoftwareSerial declaration with Serial, just check that there aren’t any funky methods used that aren’t present in the usualy Serial library

-James

EDIT: Screenshot from schematic:
image

1 Like

Ok so here is my code

const int ledPin = 5; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into

void setup() {

// initialize serial communication:
Serial.begin(9600);

// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}

void loop() {

  // LED Test
  //digitalWrite(ledPin, HIGH);
  
  // PASS
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // Read Test
    digitalWrite(ledPin, HIGH);
  
    // FAILED HERE
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    
    // if header "OxFF", turn on the LED:
    if (incomingByte == 255 ) {
      digitalWrite(ledPin, HIGH);
    }

    // if checksum "0xA7" turn off the LED:
    if (incomingByte == 167 ) {
      digitalWrite(ledPin, LOW);
    }
  }
}

This is how it is wired

image001.png

There doesn’t seem to be any data coming in???

4 Likes

Hi James,

I may have lead you astray a little here! I had neglected to check Arduino’s documentation on the serial ports of the MKR:

I’m used to other MCUs where Serial is the pins and USB Serial gets its own commands, like USBSerial or something like that

So changing your code to report back to the PC on Serial (if needed), and, communicate with the sensor on Serial1, should do the trick

-James

3 Likes

This code works on the MRK 1010 board

const int ledPin = 5;  // the pin that the LED is attached to

int incomingByte;      // a variable to read incoming serial data into

void setup() {

  // initialize serial communication:

  Serial.begin(9600);

  Serial1.begin(9600);

  // initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);

}

void loop() {

  // LED Test

  //digitalWrite(ledPin, HIGH);

  // PASS

  // see if there's incoming serial data:

  if (Serial1.available() > 0) {

    // Read Test

    //digitalWrite(ledPin, HIGH);

    // PASS

    // read the oldest byte in the serial buffer:

    incomingByte = Serial1.read();

    // if header "OxFF", turn on the LED:

    if (incomingByte == 255 ) {

      // 1st Byte Test

      digitalWrite(ledPin, HIGH);

      // PASS

    }

  }

}
4 Likes

Hey @James188644,
Glad to hear it’s working! A quick tip on posting code to the forums: If you surround your code with a triple back-tick ``` (it’s on the ~ button, above Tab), it’ll come out nicely formatted:

int void setup(){
}

Edit: I see you beat me to it :slight_smile:

4 Likes

Thanks for the tip.

For anyone who is interested the following code works on a UNO R3 Board with a DFRobot LCD shield;

#include <SoftwareSerial.h>

const byte rxPin = 12;
const byte txPin = 13;
const byte rxPin2 = 10;
const byte txPin2 = 11;

SoftwareSerial portOne(rxPin, txPin);

SoftwareSerial portTwo(rxPin2, txPin2);

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

void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);

  // Start both software serial ports
  portOne.begin(9600);
  portTwo.begin(9600);

}

void loop() {

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

  portOne.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>280)
          {
           Serial.print("Sewage = ");
           Serial.print(distance);
           Serial.println("mm ");
          }else 
              {
                Serial.println("Below the lower limit");        
              }
      }else Serial.println("ERROR");
     }
     delay(150);

}
4 Likes

The issue I have found with Arduino boards is that by using the default UART ports you must remove any connections before uploading code as it essentially the native serial port. SoftwareSerial.h allows for declaration of multiple ports on any digital pins.

4 Likes

with lcd code


#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const byte rxPin = 12;
const byte txPin = 13;
const byte rxPin2 = 10;
const byte txPin2 = 11;

SoftwareSerial portOne(rxPin, txPin);

SoftwareSerial portTwo(rxPin2, txPin2);

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

void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);

  // Start both software serial ports
  portOne.begin(9600);
  portTwo.begin(9600);

  lcd.begin(16, 2);

}

void loop() {

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

  portOne.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>280)
          {
           Serial.print("Sewage = ");
           Serial.print(distance);
           Serial.println("mm ");
          }else 
              {
                Serial.println("Below the lower limit");        
              }
      }else Serial.println("ERROR");
     }
     delay(150);

     updateScreen();
}

// https://forum.arduino.cc/u/johnwasser/summary 
// https://forum.arduino.cc/t/how-to-make-better-screen-update-without-delay/483503/4
void updateScreen() {
unsigned long currentTime = millis();
static unsigned long previousTime = 0;
const unsigned long interval = 1000;  // Every second
if (currentTime - previousTime >= interval) {
    previousTime += interval;
    printScreen();
  }
}

void printScreen() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Sewage Tank 2");
  lcd.setCursor(0, 2);
  lcd.print(distance);
  lcd.print("mm ");
  lcd.print(map(distance, 300, 3000, 100, 0));
  lcd.print("%");
}
3 Likes