# Ultrasonic sensor malfunction

**URL:** <https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375>\
**Category:** Support\
**Created:** [June 3, 2024, 6:28am UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375 "2024-06-03T06:28:28Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![asb](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@asb](https://xrp.discourse.group/u/asb)\
**Post date:** [June 3, 2024, 6:28am UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/1 "2024-06-03T06:28:28Z")

</div>

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.

 ![Screenshot 2024-05-29 143644](https://canada1.discourse-cdn.com/flex011/uploads/xrp/original/1X/a32c704e9f9267a7c23eaea8bdfd51df46b37d55.png)

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![Fgrossman](https://avatars.discourse-cdn.com/v4/letter/f/b77776/32.png) [@Fgrossman](https://xrp.discourse.group/u/Fgrossman)\
**Post date:** [June 3, 2024, 3:13pm UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/2 "2024-06-03T15:13:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![asb](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@asb](https://xrp.discourse.group/u/asb)\
**Post date:** [June 4, 2024, 1:52pm UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/3 "2024-06-04T13:52:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fgrossman](https://avatars.discourse-cdn.com/v4/letter/f/b77776/32.png) [@Fgrossman](https://xrp.discourse.group/u/Fgrossman)\
**Post date:** [June 4, 2024, 3:09pm UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/4 "2024-06-04T15:09:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SparkFro](https://yyz1.discourse-cdn.com/flex011/user_avatar/xrp.discourse.group/sparkfro/32/12_2.png) [@SparkFro](https://xrp.discourse.group/u/SparkFro)\
**Post date:** [June 4, 2024, 4:23pm UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/5 "2024-06-04T16:23:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![FIRSTinVT](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@FIRSTinVT](https://xrp.discourse.group/u/FIRSTinVT)\
**Post date:** [June 23, 2024, 5:21pm UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/6 "2024-06-23T17:21:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![asb](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@asb](https://xrp.discourse.group/u/asb)\
**Post date:** [July 11, 2024, 11:00am UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/7 "2024-07-11T11:00:50Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![SparkFro](https://yyz1.discourse-cdn.com/flex011/user_avatar/xrp.discourse.group/sparkfro/32/12_2.png) [@SparkFro](https://xrp.discourse.group/u/SparkFro)\
**Post date:** [July 11, 2024, 2:48pm UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/8 "2024-07-11T14:48:03Z")

</div>

Gotcha, in that case it may very well be a defective sensor. I would suggest doing an RMA: [Return Policy - SparkFun Electronics](https://www.sparkfun.com/returns)

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

---

<div class="post-metadata">

**Author:** ![asb](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@asb](https://xrp.discourse.group/u/asb)\
**Post date:** [July 18, 2024, 4:06am UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/9 "2024-07-18T04:06:25Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![asb](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@asb](https://xrp.discourse.group/u/asb)\
**Post date:** [July 18, 2024, 4:10am UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/10 "2024-07-18T04:10:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RG12](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@RG12](https://xrp.discourse.group/u/RG12)\
**Post date:** [June 23, 2025, 12:42am UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/11 "2025-06-23T00:42:26Z")

</div>

The same issue is happening with me as well.

---

<div class="post-metadata">

**Author:** ![SparkFro](https://yyz1.discourse-cdn.com/flex011/user_avatar/xrp.discourse.group/sparkfro/32/12_2.png) [@SparkFro](https://xrp.discourse.group/u/SparkFro)\
**Post date:** [June 25, 2025, 3:47pm UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/12 "2025-06-25T15:47:57Z")

</div>

Please check the pins inside your connectors. If there are any bent pins, that is likely the source of your problem.

 ![image](https://canada1.discourse-cdn.com/flex011/uploads/xrp/original/1X/22d88dd296d8332c3a147538ed726a9ded9c8311.jpeg)

If any pins are bent like above, they can be bent back with a pair of tweezers (be careful to not fatigue the pins!).

Bent pins like this typically happens if the connector is not plugged in straight. So be sure to always plug in the connector straight on.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![RG12](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@RG12](https://xrp.discourse.group/u/RG12)\
**Post date:** [June 30, 2025, 10:16pm UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/13 "2025-06-30T22:16:54Z")

</div>

@SparkFro I checked all the pins, and they don’t appear bent. Though, I still appreciate your response!

---

<div class="post-metadata">

**Author:** ![RG12](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@RG12](https://xrp.discourse.group/u/RG12)\
**Post date:** [July 12, 2025, 9:49pm UTC](https://xrp.discourse.group/t/ultrasonic-sensor-malfunction/375/14 "2025-07-12T21:49:10Z")

</div>

I have been researching and have figured out the solution to the problem! Turns out there was a problem with the wiring. The four colored wires need to be inserted in the correct order! On the other side if it says GND that is where the black wire goes and then the red wire goes where it says VCC. Hope this helps future problems!
