1

First of all let me introduce the problem. I am busy working on a HC SR04 + servo combination to measure the distance in certain angles. But lately some weird errors are coming up. The script( which i will post down here) worked when we started with it but lately it stopped working and we are getting certain errors which never occured.

This error keeps coming up and we really dont know what we have to do anymore. We installed a clean and new version of Raspbian but that didnt help. Changed our pins but didn't work. Changed the script to work with a Try statement and an except keyboard statement but that didn't work either. So if anyone knows what we are doing wrong, please tell us.

      Error #
      sonar2.py:17: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
      GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
    ^CTraceback (most recent call last):
      File "sonar2.py", line 32, in <module>
        start = time.time()
    KeyboardInterrupt

Script#
#!/usr/bin/python

# Import required Python libraries
import time
import RPi.GPIO as GPIO

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BOARD)

# Define GPIO to use on Pi
GPIO_TRIGGER = 21
GPIO_ECHO = 15


# Set pins as output and input
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Allow module to settle
time.sleep(0.5)

# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
start = time.time()
while GPIO.input(GPIO_ECHO)==0:
  start = time.time()

while GPIO.input(GPIO_ECHO)==1:
  stop = time.time()

# Calculate pulse length
elapsed = stop-start

# Distance pulse travelled in that time is time
# multiplied by the speed of sound (cm/s)
distance = elapsed * 34300

# That was the distance there and back so halve the value
distance = distance / 2

print "Afstand : %.1f" % distance

# Reset GPIO settings
GPIO.cleanup()
  • What error? There is a warning which you can ignore. – joan Jan 17 '15 at 12:40
  • 1
    Several more points. That warning has been in place for years. I'm not sure why you haven't seen it before. Your comment says BCM GPIO addresses, the code uses pin numbers. A stepper will probably be more accurate than a servo (especially with RPi.GPIO which can't properly position servos). I'd add stop = start before the second while loop to make sure stop is assigned a value (that loop might not be executed). – joan Jan 18 '15 at 8:56
  • The problem is the code worked before but now it doesn't. So there is no use in ignoring the errors. We did try to ignore the errors but it still didn't put out any distance. I added the stop = start but that didn't change anything. – Sebastiaan Jan 19 '15 at 9:32
  • Have you got a voltage divider on the data output line to cut it from 5V to 3V3? Perhaps a gpio has fried. There is little to go wrong with these devices. You could try piscope and use pigs to manually send the trigger. – joan Jan 19 '15 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.