0

I am trying to find the distance to a nearby object using an HC SR04 ultrasonic sensor. I followed the tutorial from the below link:

HC-SR04 Ultrasonic Range Sensor on the Raspberry Pi

I replicated all the instructions as given. I ran into a problem when trying to run the code.

import RPi.GPIO as GPIO
import time

# Setting the GPIO pins mode
GPIO.setmode(GPIO.BCM)

# Setting the pin values
TRIG = 23
ECHO = 24

# Assigning the pins to GPIO
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

# Allowing the sensor to settle down
GPIO.output(TRIG, False)
print('Waiting for thr sensor to settle down!!')
time.sleep(2)

# Sending a trigger pulse of 10 micro seconds duration
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)

# Starting the measurement process
pulse_start = 0
pulse_end = 0

# Checking for the last timestamp at which the ECHO is OFF
while GPIO.input(ECHO) == 0:
    print('Running ECHO = 0 loop')
    pulse_start = time.time()

print('Pulse start: ',pulse_start)

# Checking for the last timestamp at which the ECHO is ON
while GPIO.input(ECHO) == 1:
    print('Running ECHO = 1 loop')
    pulse_end = time.time()

print('Pulse end: ',pulse_end)

# Calculating the pulse duration
pulse_duration = pulse_end - pulse_start

# Distance Calc
distance = round((343/2)*pulse_duration,2)
print('Distance: ', distance, 'm')

# Cleaning up the pins
GPIO.cleanup()

The code doesn't seem to enter the first while loop where the ECHO pin is being checked for low signal. This is giving me a problem in calculating the distance. The output from the above code is as follows:

Code Output

Can anyone point out what the problem could be?

Akhil2511
  • 1
  • 2
  • It seems like your ECHO pin is always ON, is your setup properly wired? – Gabriel Milan Jul 22 '20 at 17:48
  • I'd try deleting these `print('Running ECHO = X loop')` statements and moving those `print('Pulse X: ',pulse_X)` statements to just before the `pulse_duration` calculation like this: https://codeshare.io/G8DB3J – Gabriel Milan Jul 22 '20 at 17:54
  • @GabrielMilan How can the ECHO pin always be ON? Sorry for this question as I am from a mechE background – Akhil2511 Jul 22 '20 at 19:03
  • 1
    Have you tried changing those `print` statements, as I said above? And your ECHO pin would always be ON if you've wired it wrong, by connecting the voltage divider on +3V3 instead of GND – Gabriel Milan Jul 22 '20 at 20:24
  • Thanks for the explanation. Yes, I have tried changing those print statements but the output still remains the same, The value assigned to pulse_start is still 0. I will once again try to rewire the setup. – Akhil2511 Jul 22 '20 at 20:36
  • One thing I would also check is whether the pins are connected correctly on the GPIO. You've set `TRIG = 23` and `ECHO = 24` on the code, please be sure that they correspond to your wiring setup. Besides that, I've made a few changes on your code in an attempt of making it work: https://codeshare.io/amrvl1 . Please let me know if I can help with anything else – Gabriel Milan Jul 22 '20 at 21:01
  • Sorry for the late reply. I rewired the setup once again and also took care of the points you mentioned above. I also changed the code by following your link. But it's causing the same issue. – Akhil2511 Jul 26 '20 at 15:12
  • @Akhil2511 The distance value as shown in your screenshot is too high. Is it possible the sensor cannot detect distances bigger than a specific value? – a_sid Mar 20 '21 at 02:35

0 Answers0