Ultrasonic sensor malfunction

I have encountered a technical glitch (software I guess) with the ultrasonic sensor. While running the installation verification code provided as default, the ultrasonic sensor is only displaying the distance intermittently. Sometimes it starts working perfectly after turning the power on and off ( the XRP). I tried running the ultrasonic sensor part of the code separately still result is the same. I don’t think it is not a voltage issue as I measured the VCC - ground voltage and verified. Firmware is also updated.

from XRPLib.defaults import *
import time

# Installation Verification Program
def ivp():
    # Print welcome message
    print("------------------------------------------")
    print("Running Installation Verification Program!")
    print("------------------------------------------")
    print()
    print("Welcome to the XRP! This example code will help")
    print("you verify that everything is working on your robot.")
    print("After each test, press the user button to continue.")
    print()
    
    # Flash LED at 5Hz
    print("Flashing LED")
    board.led_blink(5)
    
    # Wait for user to press button
    print("Press user button to test reflectance sensor")
    board.wait_for_button()
    
    # Stop blinking LED
    board.led_off()
    
    # Print reflectance values until button is pressed
    while not board.is_button_pressed():
        print(f"Reflectance    Left: {reflectance.get_left():.3f}    Right: {reflectance.get_right():.3f}    Press user button to test range sensor")
        time.sleep(0.1)
    
    # Wait until button is released
    while board.is_button_pressed():
        time.sleep(0.1)
    
    # Print range values until button is pressed
    while not board.is_button_pressed():
        print(f"Range    Distance: {rangefinder.distance():.1f}    Press user button to test servo")
        time.sleep(0.1)
    
    # Wait until button is released
    while board.is_button_pressed(): 
        time.sleep(0.1)
    
    # Print warning and wait for button press
    print()
    print("The next test will move the servo, so keep your hands clear!")
    print("Also please make sure the power switch is turned on!")
    print("Press user button to test servo")
    board.wait_for_button()
    
    # Test servo
    print("Testing servo")
    time.sleep(1)
    servo_one.set_angle(90)
    time.sleep(1)
    servo_one.set_angle(0)
    time.sleep(1)
    servo_one.set_angle(180)
    
    # Print warning and wait for button press
    print()
    print("The next test will drive the motors, so place the robot on a flat surface!")
    print("Also please make sure the power switch is turned on!")
    print("Press user button to test drivetrain")
    board.wait_for_button()
    
    # Test drivetrain
    print("Testing drivetrain")
    time.sleep(1)
    drivetrain.straight(25, 0.8)
    time.sleep(1)
    drivetrain.turn(90,0.8)
    time.sleep(1)
    drivetrain.turn(90, -0.8)
    time.sleep(1)
    drivetrain.straight(-25,0.8)
    
    print()
    print("-----------------------------------")
    print("All tests complete! Happy roboting!")
    print("-----------------------------------")

ivp()

The times we have seen this it has been because of a miswiring or a loose wire. Even a different sensor or servo being miswired can cause some problems with the other sensors. If all other parts are working fine with the installation verification, then I would check for a loose wire.

1 Like

@Fgrossman I have double checked the wiring, it doesn’t seem like the cause of the issues. Thanks a lot for your response.

One other thing you can try to see if it is a component on the Ultrasonic sensor, you can try slowing down the loop by increasing the time.sleep to see if it makes a difference.
@SparkFro do you have any thoughts on this?

Since it works intermittently, sounds like it’s a hardware issue rather than a software issue, so let’s assume the code is fine for now. Could be a loose wire as suggested, could be a damaged wire (eg. broken connection within the insulation), could be a defective sensor, could be a defective control board, could be…

@asb Do you happen have multiple XRP kits, or just the one? If you’re able, it would be helpful to try swapping hardware (the sensor, the control board, and the cable) in order to determine if something is defective, and exactly which component. If you don’t have a second XRP kit, you can at least try is swapping the cable with the one for the line following sensor, at least to rule out a bad cable.

This could be difficult to troubleshoot remotely, but hopefully this helps!

Damaged wires or a port not seated correctly could be the cause. However, I have spent several hours trying to debug this with multiple XRPs. One iteration was very interesting.

I had 2 Range Sensors (HC-SR04) that were on my suspect list and one XRP that had all sensors working fine. My test program was sampling at 1 HZ.

I unplugged a working sensor and replaced with a suspected sensor and it only returned values of 65535, so it must be the sensor! Then I replaced the defective sensor with the working one and tested again. It also only returned values of 65535!

I am using 12 XRPs for an upcoming workshop so I will have to avoid using the Range sensors. However, I think this issue needs more research on the firmware side.

@SparkFro Sorry for the late response and thanks a lot for the suggestion. I checked the ultrasonic sensor with an Arduino and I believe the issue is with the ultrasonic sensor as it is still giving intermittent values.

Gotcha, in that case it may very well be a defective sensor. I would suggest doing an RMA: Return Policy - SparkFun Electronics

Link to this thread so our support team knows that I talked with you about this, and hopefully they can get you a replacement!

@FIRSTinVT Thanks for your response, it is really helpful. But in my case, it could be just a faulty sensor. I will update if I can get it sorted by using replacement sensor. Currently I am working on different project enabling remote access for XRP through internet.

@SparkFro Thanks, but for now, I will keep experimenting on the hardware. I appreciate your help a lot.