Quantcast
Viewing all articles
Browse latest Browse all 5103

Beginners • A02YYUW Waterproof sensor, UART works, but need to write data to read data

HI,
I have A02YYUW (UART version, A0221A) running on Pi Pico 2. but to get the code below to work I must to write a byte to it each time before data is returned. I can't get a stream of data like websites imply is true.

From this website https://dronebotworkshop.com/waterproof-ultrasonic/ it says "(Sensor's RX pin controls mode) When held HIGH or not connected (it is internally pulled up), it will operate every 300ms. If the RX pin is held LOW then the data is output every 100ms."

I've tried to remove the wire (sensor's rx which is tx on pico) but it doesn't work. Even tried to remove sensor's tx Image may be NSFW.
Clik here to view.
:D


Any thoughts?

Below is my code (also on my GitHub: https://github.com/bradcar/artcar-ultra ... a02yyuw.py )

Code:

from machine import UART, Pinfrom os import unamefrom sys import implementationimport time# A02YYUW Waterproof sensor, UART-version (mine is: A0221A)# connected with RS-485## Notes:# * Pico has two UARTs: uart(0) and uart(1)# * Define uart1 pins for ultrasonic, uart0 typical for REPL so don't use uart0## * 4 Sensor wires: 1) Red=Vcc, 2)Blk=Gnd,#                   3) Yellow=rx_sensor=pico_tx1(GP20)#                   4) White =tx_sensor=pico_rx1(GP21)## * https://dronebotworkshop.com/waterproof-ultrasonic/  (note diff wire colors on sensor)#   Sensor's RX pin controls mode. When held HIGH or not connected (it is internally pulled up),#   it will operate every 300ms. If the RX pin is held LOW then the data is output every 100ms.#   Pico's UART RX pin to receive the signal/values from the senor's TX pin##   BUT, only way it works for me is to write a byte each time to get sensor to respond## DEBUG NEEDED: "with Rx on the sensor floating, it will output data ever 300ms" -- CANT GET THIS WORKING## by bradcardebug =Falseuart1 = machine.UART(1, 9600, tx=20, rx=21)# expect: UART(1, baudrate=9600, bits=8, parity=None, stop=1, tx=20, rx=21, txbuf=256, rxbuf=256, timeout=0, timeout_char=2, invert=None, irq=0)print(uart1)# Function to calculate checksum, make sure only 1 byte returneddef calculate_checksum(data_buffer):    return (data_buffer[0] + data_buffer[1] + data_buffer[2]) & 0x00ffdef ultrasonic_distance_uart():    """    :returns: distance in cm (type: float), The sensor returns mm in integer    """    # REQUIRED write !!!!   to get uart1 to send data    uart1.write(b'\xff')        if uart1.any():           # Read the first byte to check for the packet header (0xFF)        if uart1.read(1) == b'\xff':            # Create an array to store the data buffer            data_buffer = bytearray(4)            data_buffer[0] = 0xff            # Read the next 3 bytes            data_buffer[1:4] = uart1.read(3)            if debug: print(f"data_buffer = {data_buffer}")            # Compute checksum            checksum = calculate_checksum(data_buffer)            if debug: print(f"checksum={checksum:x}")            # Verify if the checksum matches the last byte in the packet            if data_buffer[3] == checksum:                # Calculate distance in mm from the data bytes                distance = (data_buffer[1] << 8) + data_buffer[2]                if debug: print(f"distance={distance} mm\n")                return distance/10.0    return None# startup codeprint("Starting...")print("====================================")print(implementation[0], uname()[3],      "\nrun on", uname()[4])print("====================================")# A02YYUW seems to need one call before use, then wait 500ms, otherwise get errors the first time_ = ultrasonic_distance_uart()time.sleep(.5)# Main loopwhile True:    cm = ultrasonic_distance_uart()    if cm:        print(f"Distance= {cm:.1f} cm")    else:        print("ERROR A02YYUW: No valid data received.")        time.sleep(1)  # arbitrary One second between measurements
BTW, if any beginners need them, on my GitHub I've also got working code for SSD1309(SDI & I2C), button with interrupt debounce, DHT22, HC-SR04 (PWM), onboard temp sensor, DS18B20(onewire) all in micopython for Pi Pico 2.

Statistics: Posted by bradcar — Fri Oct 25, 2024 2:13 am



Viewing all articles
Browse latest Browse all 5103

Trending Articles