pablok
Posts: 26
Joined: Thu Dec 26, 2013 4:46 pm
Location: Westerkwartier, Groningen (NL)

learn resource example code ultrasonic distance sensor

Fri Aug 11, 2017 11:51 am

Dear members,

I am experimenting, playing, with a raspberry zero and a hc-sr04 sensor.
I came across the learning resource

https://www.raspberrypi.org/learning/ph ... /distance/

and tried the code there.
I was unhappy with the results (and the example code given in the article).
The output of the measuring is invariably 1. [print(ultrasonic.distance)] I suspect the default value.
When I wave my hands above the sensor, the output does not change.
There is something wrong and it is not the Pi or the sensor. Because when I change back to earlier code all functions fine.
The only change I made to the example code is

Code: Select all

ultrasonic = DistanceSensor(echo=24, trigger=23)
Even the basic recipe from the documentation:
http://gpiozero.readthedocs.io/en/stabl ... nce-sensor
does not work.

Any advice? Please.

pablok

User avatar
MrYsLab
Posts: 39
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: learn resource example code ultrasonic distance sensor

Fri Aug 11, 2017 1:26 pm

It would help if you posted the earlier code that worked. Are you using the same wiring for both the working and non-working examples?

You mention you tried the basic recipe. Your code snippet reverses the echo and trigger pin from the code in the recipe. Did you mean to reorder the pins from the example? Which pins you pick is not important, but the echo and trigger have to be defined in the function call exactly as you wired it.

pablok
Posts: 26
Joined: Thu Dec 26, 2013 4:46 pm
Location: Westerkwartier, Groningen (NL)

Re: learn resource example code ultrasonic distance sensor

Fri Aug 11, 2017 2:38 pm

Code that works

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

TRIG = 23
ECHO = 24

print ("meting is bezig")

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG, False)
print ("Wachten op rust")
time.sleep(2)
print ("rust is voorbij")

GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
pulse_start = -1.0
pulse_end = 0.0

while GPIO.input(ECHO)==0:
    pulse_start = time.time()
    # print ("0",)

while GPIO.input(ECHO)==1:
    pulse_end = time.time()
    # print ("1")

pulse_duur = pulse_end - pulse_start
print ("pulseduur: ",pulse_duur)

afstand = pulse_duur * 17150
afstand = round(afstand, 2)

print ("Afstand: ", afstand," cm")

GPIO.cleanup()
Other code that works comes from:
|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
# ultrasonic_2.py
# Measure distance using an ultrasonic module
# in a loop.
#
# Author : Matt Hawkins
# Date : 28/01/2013


You spotted the reversion of the pins. They are as stated echo= gp24 and trigger=gp23!

User avatar
MrYsLab
Posts: 39
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: learn resource example code ultrasonic distance sensor

Sat Aug 12, 2017 10:46 am

It appears there are some known issues with hc-sr04 support listed in github https://github.com/RPi-Distro/python-gp ... issues/385

pablok
Posts: 26
Joined: Thu Dec 26, 2013 4:46 pm
Location: Westerkwartier, Groningen (NL)

Re: learn resource example code ultrasonic distance sensor

Sat Aug 12, 2017 1:06 pm

Thank you for finding this.
Being that as it is I would think there should be a comment to warn 'the kids' there are problems with
this "Raspberry Pi Learning Resources" on the
https://www.raspberrypi.org/learning/ph ... /distance/ -page.

Some problems are good for learning. This is a bit too much, I think.

pablok

User avatar
MrYsLab
Posts: 39
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: learn resource example code ultrasonic distance sensor

Sat Aug 12, 2017 3:48 pm

I couldn't agree with you more.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 10 guests