gjs31
Posts: 25
Joined: Sat Nov 04, 2017 2:42 am

Ultrasonic sensor only returning values of 1.0

Wed Nov 29, 2017 11:07 pm

Hi,

I'm trying to get the ultrasonic theremin to work form the projects pages (https://projects.raspberrypi.org/en/pro ... c-theremin), but after checking everything over again, and even buying a new sensor, all I still get back is 1.0.

I'm using a Rpi3 B, a HS-SR04 sensor, and resistors as per the instructions.

When I run their sample code (below), it hangs for about 30 seconds then starts spitting back the 1.0 values.

Code: Select all

from gpiozero import DistanceSensor
from time import sleep
sensor = DistanceSensor(echo=17, trigger=4)
while True:
     print(sensor.distance)
     sleep(1)
Any suggestions on where I may be going wrong? I've checked the pins using wiringpi gpiotest (after feeding 5v back into my old board).

Any help would be appreciated!!!

DirkS
Posts: 9842
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Ultrasonic sensor only returning values of 1.0

Wed Nov 29, 2017 11:29 pm

First of all: triple-check the wiring
Then make sure the one-wire protocol is disabled (it also uses GPIO4). Use the Pi configuration utility (or raspi-config). Or check if /boot/config.txt contains a line starting with

Code: Select all

dtoverlay=w1-gpio
If everything looks ok you should post a photo showing your wiring.

gjs31
Posts: 25
Joined: Sat Nov 04, 2017 2:42 am

Re: Ultrasonic sensor only returning values of 1.0

Thu Nov 30, 2017 10:12 am

Thanks. Haven’t had a chance to try it, but I know i2c is enabled, so hopefully that will solve it. Any way to run it alongside i2c, is it just a case of using different gpios?

DirkS
Posts: 9842
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Ultrasonic sensor only returning values of 1.0

Thu Nov 30, 2017 10:32 am

gjs31 wrote:
Thu Nov 30, 2017 10:12 am
Any way to run it alongside i2c, is it just a case of using different gpios?
Yes, if you use different GPIOs you should be fine.
I2C uses GPIO2 and 3, so that's fine.

speedgear2
Posts: 7
Joined: Sun Mar 11, 2018 8:32 am

Re: Ultrasonic sensor only returning values of 1.0

Sun Mar 11, 2018 8:34 am

Hi,

Does anyone know how to solve this? I am having the exact issue. Ultrasonic Distance sensor is returning only 1.0 continuously, i checked the wiring also. What else could be the problem, any help will be greatly appreciated.

Thanks,
Murad.

pcmanbob
Posts: 6239
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Ultrasonic sensor only returning values of 1.0

Sun Mar 11, 2018 1:36 pm

Hi.

Possible causes are incorrect wiring post some pictures of your wiring showing both ends may be we can spot an error, other interfaces active on the GPIO pins you are using, try using different GPIO pins or disabling active interfaces on GPIO pins via raspi-config.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported


Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Ultrasonic sensor only returning values of 1.0

Sun Mar 11, 2018 2:39 pm

Oi! I woke up this AM to my left ultrasonic sensor just returning a 1. I re-seated all the wires on the left sensor and wall-la, issue went away.
Without knowing why you are deleting my postings, I will not know how...

User avatar
bensimmo
Posts: 4068
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Ultrasonic sensor only returning values of 1.0

Sun Mar 11, 2018 2:49 pm

joan wrote:
Sun Mar 11, 2018 1:40 pm
Also see https://github.com/RPi-Distro/python-gp ... issues/618
To add to this
Latest release has the fixes, so do an update (apt or pip3 whichever you used)
https://gpiozero.readthedocs.io/en/stab ... 2018-02-20

speedgear2
Posts: 7
Joined: Sun Mar 11, 2018 8:32 am

Re: Ultrasonic sensor only returning values of 1.0

Wed Mar 14, 2018 6:26 am

I updated using sudio apt-get update and then sudo apt-get upgrade, then I reboot and ran the program from Python, it again gives me distance 1.0 :(

speedgear2
Posts: 7
Joined: Sun Mar 11, 2018 8:32 am

Re: Ultrasonic sensor only returning values of 1.0

Wed Mar 14, 2018 6:55 am

It has been giving a warning that reads no echo received. I verified the wiring and it is fine.

User avatar
joan
Posts: 14013
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Ultrasonic sensor only returning values of 1.0

Wed Mar 14, 2018 7:55 am

speedgear2 wrote:
Wed Mar 14, 2018 6:26 am
I updated using sudio apt-get update and then sudo apt-get upgrade, then I reboot and ran the program from Python, it again gives me distance 1.0 :(
What version of gpiozero are you currently using?

pcmanbob
Posts: 6239
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Ultrasonic sensor only returning values of 1.0

Wed Mar 14, 2018 9:52 am

May be its time to start be eliminating the hardware as the problem, by using RPi.GPIO to run the sensor.

try running this code

Code: Select all

import time
import RPi.GPIO as GPIO

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

# Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO    = 24



# 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)

print "Ultrasonic Measurement"

# 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)
distancet = elapsed * 34300

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

print "Distance :", distance

print "Elaspsed time :", elapsed

print "Total distance :", distancet


# Reset GPIO settings
GPIO.cleanup()
I know it works as its what I used to demonstrate the ultrasonic sensor to my granddaughter.

Now you will have to change the GPIO pin numbers to match the ones you have your sensor connected to and remember this code uses BCM numbering.
If you get results from this code then you know its a code problem , if you don't then you need to look at your hardware/wiring.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Troubleshooting”