HC-SR04 ultrasonic distance sensor issue while robot is in motion

SebOOl84
Posts: 3
Joined: Mon Oct 09, 2017 1:51 pm

HC-SR04 ultrasonic distance sensor issue while robot is in motion

Hi all
I am building a robot based on Raspberry Pi 3 and want to implement distance sensor to avoid hitting walls etc...
I connected the sensor using this manual --> https://www.raspberrypi.org/learning/ph ... /distance/ and wrote my own Python class to control it. (attached at the end of this post). Distance function returns its value in cm.
When the robot is not moving everything works as expected, the distance is being measured and reported correctly.
The problem starts when the robot is in motion. In this case the real value is being reported only once per few outputs. The rest is like:

Code: Select all

``````Current distance = 133.7881088256836 # real value
Current distance = 105.72613477706909 # real value
Current distance = 0.33937692642211914
Current distance = 88.43835592269897 # real value
Current distance = 0.2453327178955078
Current distance = 0.35164356231689453
Current distance = 0.261688232421875
Current distance = 38.378214836120605 # real value
Current distance = 0.33119916915893555
Current distance = 0.2575993537902832
Current distance = 0.2575993537902832
Current distance = 0.2575993537902832
-------------- somewhere here the robot hit the wall and stopped ----------------
Current distance = 0.33937692642211914
Current distance = 0.2575993537902832
Current distance = 0.2575993537902832
Current distance = 0.261688232421875
Current distance = 6.615805625915527 # real value``````

Even when it hits the wall, some time is needed to get real value (for tests the measurment inteval is 1 second)
Probably, I should include the speed of the car which I do not know and which is not constant...
Has anyone seen such issue before or have any idea how to deal with it?

The way I am calling the measurement:

Code: Select all

``````while cur_distance > float(10) or cur_distance < float(5.5):
cur_distance = ds.distance()
time.sleep(1)
print('Current distance = ' + str(cur_distance))
if cur_distance >= float(10) and motor.motors_in_motion != 'forward' :
if MOTORS_ENABLE:
motor.setSpeed(100)
motor.forward()``````

Code: Select all

``````#!/usr/bin/python3
#Libraries
import RPi.GPIO as GPIO
import time

class DistanceSensor:
def __init__(self):
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

self.gpio_trigger = 18  # BCM PIN 24
self.gpio_echo = 22     # BCM PIN 25

GPIO.setup(self.gpio_trigger, GPIO.OUT)
GPIO.setup(self.gpio_echo, GPIO.IN)

def __test__(self):
''' Continuosly prints current distance form the sensor '''
try:
while True:
dist = self.distance()
print("Measured Distance = %.1f cm" % dist)
time.sleep(1)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()

def distance(self):
''' Returns distance in cm'''
GPIO.output(self.gpio_trigger, False)       # set Trigger LOW
time.sleep(0.00002)                         # wait 2ms
GPIO.output(self.gpio_trigger, True)        # set Trigger to HIGH
time.sleep(0.00010)                         # after 10ms
GPIO.output(self.gpio_trigger, False)       # set Trigger to LOW

StartTime = time.time()                     # init value for StartTime
StopTime = time.time()                      # init value for StopTime

while GPIO.input(self.gpio_echo) == 0:      # save StartTime
StartTime = time.time()

while GPIO.input(self.gpio_echo) == 1:      # save time of arrival
StopTime = time.time()

# time difference between start and arrival
TimeElapsed = StopTime - StartTime

# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
``````

OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: HC-SR04 ultrasonic distance sensor issue while robot is in motion

This quite often happens with lots of different sensors, u get noise in the readings.

Remember the ultra sonice sensor works by sending out sound waves and then listening for the echo back. It is possible that the sound or vibration coming from the motion (possibly the motors) is being heard by the mic on the ultrasonic sensor and it is hearing this as the echo back.

first you can try maybe shielding the ultrasonic sensor from sounds coming from car by putting a foam dome around the sensor facing forward so it can only hear sound from infront

Second you can sample at a higher rate and write a routine to sort out the good data from the noise and reject the noise data.

SebOOl84
Posts: 3
Joined: Mon Oct 09, 2017 1:51 pm

Re: HC-SR04 ultrasonic distance sensor issue while robot is in motion

Hi
Thank you for your answer. I have done some testing today and the issue is related to motors.
When I tried to move the robot without motors enabled I was getting 1 correct value per two readings. After I enabled the motors 1 per 20-30 readings.
So it looks like the sensor reads the noise or vibration as the returning sound wave.

SebOOl84
Posts: 3
Joined: Mon Oct 09, 2017 1:51 pm

Re: HC-SR04 ultrasonic distance sensor issue while robot is in motion

Last update.
I have replaced HC-SR04 sensor with US-015 and crazy values are now under control (every second reading is bad).
I can not say in general as I tested only 2 SR04 sensors and 1 US-015 but there is big difference between their readings during motors operation. The wiring is the same but US-015 works better.
I don’t see any difference between them while you only measure distance without motors running.
So it depends what you need.

OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: HC-SR04 ultrasonic distance sensor issue while robot is in motion

Looks like the US-015 might have a narrower band width for the sound that it listens for.

Moe
Posts: 230
Joined: Sun Jan 25, 2015 2:44 pm

Re: HC-SR04 ultrasonic distance sensor issue while robot is in motion

It's more likely to be electrical noise than acoustic, particularly if the sensors and motors are powered directly from the same source. Sensors are by definition sensitive and motors are very noisy. PWM controllers are also noisy, simply because they output high-current square pulses.

If so, it should be easy to fix. First try putting a capacitor across the power lines to the sensor, then run your test again. The HC-SR04 runs at 40kHz which is pretty low, so try to find a big capacitor; 220uF or more. I salvage mine from old power supplies in the WEEE bin at work. If the results are better then you have proved the point.

But you should really suppress the noise at source, i.e across the motor wires, as near to the motor as you can get. And/or loop the motor wires through a ferrite core two or three times. The motors only need a DC signal so there is effectively no limit on the level of suppression you can add.
Submarine communication systems engineer and amateur robot enthusiast.

HermannSW
Posts: 1317
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: HC-SR04 ultrasonic distance sensor issue while robot is in motion

I used HC-SR04 and US-100, seems that your US-015 is cheaper than the US-100.

The biggest problem I faced with sensors on moving robot (with Arduino at that time) is the same that you will have:
You wait for a very long time in your 1st while loop in case no obstacle is near, or even infinitely in case there is no obstacle within sensor reach (4m for US-100). In that case your robot will crash into obstacle.
⇨https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://gitlab.freedesktop.org/HermannSW/gst-template
https://github.com/Hermann-SW/fork-raspiraw