Ultrasonic sensors with Wipy and expansion board 3.1

Has anyone had any experience using a A01NYUB ultra sonic sensor with a Pycom Wipy 3.0 and expansion board 3.1?

I know there is an tutorial using a HC-SR04 ultrasonic sensor at Ultrasonic Sensing with Pycom - Tutorial Australia
But with this sensor you simply have to calculate the time and divide by 2 to get the distance.

The A01NYUB ultra sonic sensor does all this for you and returns the distance in a serial mode in millimeters. I have managed to connect a A01NYUB ultra sonic sensor to an Arduino UNO and it works well but I need IOT so decided to look at Pycom Wipy and and the expansion board 3.1.

I’m looking for some help in the code for triggering the device to get a stable reading. I can look at the serial stream on UART1 (set to pins P11 & P12) but the data is all over the place. The header byte is never at the start of the data stream and sometimes there is no data for a while even though I can hear the bat like chirps of the sensor albeit softly!

Any help would be appreciated.

Regards

Richard.

2 Likes

Hi Richard,

Welcome to the forum!!

Would it be possible to grab your code that you have running? Note that the sensor operates at 9600 baud rate, you can do a quick verification test by looping your RX to TX on the Wipy and sending a serial string.

1 Like

Can you post an example of the data stream that you have captured? Have you confirmed that the expansion board is not interfering with UART1?

2 Likes

I think I have answered and fixed my own problem with the A01NYUB ultrasonic sensor on a Pycom Wipy 3.0 and expansion board 3.1

Below is my code. Sorry if the formatting is all wrong but this is the first time I have posted code on this forum.

My problem was that the A01NYUB is constantly sending data and filling up the UART receive port. So I simply read all the data and the read a byte at a time until I see a header byte of b’\xff’
I did see on the documentation of the A01NYUB ultra-sonic sensor that the
When “RX” floats or input High level, the module outputs processed value, the data is more steady, response time: 150-300ms; when input Low level, the module outputs real-time value, response time: 150ms.
So I tied this to pin G16 on the expansion board and sent it low then high for a small amount of time. This seem to make the data more stable. By this I mean every time I ran the code with the P16 going low and high the distance returned is constant, but when I comment them out the distance returned seems to vary by a few Cm’s each time.

By the way this sensor does seem to be pretty accurate as long as its about 25cm or further away from the object.

Here is the code if anyone is interested in it.

Now its time to put into my water tank.

from machine import Pin
import time


uart = UART(1, baudrate=9600, pins=('P20','P21'))               # Set UART pins.    A01NYUB TX wire to pin 21
trigger = Pin(Pin.exp_board.G16, mode=Pin.OUT)                  #PIN 16 on expansion board 3.1 to RX wire of A01NYUB ultrasonic sensor


def Get_A01NYUB_dist():
    done = False
    distance = 0
   header_val = b'\xff'
    dummy = uart.readline()                                 # Wipe all data in UART Receive
    while True:
        trigger(0)                                          #Not sure but this seems to make the data more stable
        time.sleep_us(2)  #2usec
        trigger(1)
        time.sleep_us(10)  #10usec
        ByteHeader = uart.read(1)                               # Read 1 byte
        if ByteHeader == header_val:                            # is it a header byte = b'\xff'
            ByteHigh = uart.read(1)                             # read high byte
            ByteLow = uart.read(1)                              # read low byte
            Checksum = uart.read(1)                             # read Checksum byte
            ByteHighIntVal = int.from_bytes(ByteHigh,"little")  # convert high byte to integer
            ByteLowIntVal = int.from_bytes(ByteLow,"little")    # convert low byte to integer
            Dist = (ByteHighIntVal * 256) + ByteLowIntVal   # Calculate distance from returned UART data see https://wiki.dfrobot.com/A01NYUB%20Waterproof%20Ultrasonic%20Sensor%20SKU:%20SEN0313
            return Dist

distance = Get_A01NYUB_dist()
print("Dist - " + str(distance) + " - mm")

Regards

Richard.

2 Likes