A01NYUB with Mycodo BASH Command input (howto for beginners)

A01NYUB is an waterproof ultrasonic distance sensor from DFRobot. I use this to measure the water level in my main water tank. You can download the library and an example file through a GITHUB page. Library is in python and the example file also.

Since I am not a developer and not very keen with python programming and after a lot of tries to write a script to add the sensor in mycodo library through custom input with no luck (as instructions and example files proved not very helpful, to me at least), I came up with a workaround by using BASH Command input

If you face similar problem, here is what you have to do

First you need to make some changes.

Go to directory

“/home/username/DFRobot_RaspberryPi_A02YYUW/examples/” (replace username with username you chose when installing Raspbian)

Make a safe copy of the original file “demo_get_distance.py”

cp demo_get_distance.py demo_get_distance.py.bkp


sudo nano demo_get_distance.py

Delete all and Paste the following

# -*- coding:utf-8 -*-

  @file demo_get_distance.py
  @brief Get ranging data.
  @n Connect board with raspberryPi.
  @n --------------------------------------------
  @n sensor pin |         raspberry pi          |
  @n     VCC    |            5V/3V3             |
  @n     GND    |             GND               |
  @n     RX     |          (BCM)14 TX           |
  @n     TX     |          (BCM)15 RX           |
  @n --------------------------------------------
  @Copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
  @license     The MIT License (MIT)
  @author [Arya](xue.peng@dfrobot.com)
  @version  V1.0
  @date  2019-8-31
  @url https://github.com/DFRobot/DFRobot_RaspberryPi_A02YYUW

import sys
import os
import time


from DFRobot_RaspberryPi_A02YYUW import DFRobot_A02_Distance as Board

board = Board()

def print_distance(dis):
  if board.last_operate_status == board.STA_OK:
    print("%d" %dis)
  elif board.last_operate_status == board.STA_ERR_CHECKSUM:
  elif board.last_operate_status == board.STA_ERR_SERIAL:
    print("Serial open failed!")
  elif board.last_operate_status == board.STA_ERR_CHECK_OUT_LIMIT:
    print("Above the upper limit: %d" %dis)
  elif board.last_operate_status == board.STA_ERR_CHECK_LOW_LIMIT:
    print("Below the lower limit: %d" %dis)
  elif board.last_operate_status == board.STA_ERR_DATA:
    print("No data!")

if __name__ == "__main__":
  #Minimum ranging threshold: 0mm
  dis_min = 0
  #Highest ranging threshold: 4500mm 
  dis_max = 4500

  board.set_dis_range(dis_min, dis_max)
  distance = board.getDistance()

Save and exit

These changes return one single value so mycodo can use store and display it.

Create a bash file in home directory for example. I named it A01NYUB.sh

Open it and Paste the following:


python3 /home/username/DFRobot_RaspberryPi_A02YYUW/examples/demo_get_distance.py

replace username with username you chose when installing Raspbian

save and close

from a termina run

chmod +x /home/username/A01NYUB.sh

replace username with username you chose when installing Raspbian

And last step

on mycodo go to Setup/Input

Choose from the dropdown menu “Linux Bash Command: Return Value [Mycodo]”

In the window that opens

In Options section in the Command box paste


In Custom options in the box User enter username you chose when installing Raspbian

And next to it in Current working directory type


replace username with username you chose when installing Raspbian

click save ad then click close

Activate the input you just added


P.S. I know this is basic knowledge to most of you but if I had this earlier It could have saved me a lot of time trying. I suggest that somehow it is a good start for a more detailed manual for beginners like me

1 Like

I’ve had excellent luck using a LIDAR Time of Flight sensor to detect water level. Although there are several in the family, I’ve only gotten one to work to work correctly out of the box:

I’ve also tested VL6180 which really didn’t work well for water levels and I’ve tested the VL53L0X which appears to have a 20mm offset. At this point, I can’t say if that offset is constant or not.

Kyle will eventually test my code for the VL53L4CD and validate it (hopefully). A single board is ~$15 and I’m going to need 8 of them so I’m researching the least expensive source of boards.