Roo_027
Posts: 42
Joined: Wed Jan 10, 2018 10:10 am

HC-SR04 distance detector code

Thu Feb 15, 2018 11:58 am

Good afternoon

I am having trouble recording a signal from my ultrasonic distance detector. This is connected to a bread board which I have in turn connected to the 4 pins on my 4tronix microcon motor controller board.

According to the 4tronix site, the 4 pins are all connected to one GPIO pin on the Pi so what is confusing me is how can I have a GPIO pin be both an Input and an Output?

http://4tronix.co.uk/blog/?p=200

In my code what I have done is first have the Pin 8 be an Output and then run a GPIO.cleanup() and then run the function where it is an Input to read back the distance.

I do NOT get back any errors when running the script but nothing happens - eventually I ctrl + C it and get the "100" value I have in my "except" part.

Code: Select all

#Testing the sensor

import RPi.GPIO as GPIO
import time

def out_sig():
    #Set type of pin numbering
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    #Set the pin to output
    GPIO.setup(8, GPIO.OUT)

    GPIO.cleanup()

def input_sig(measure="cm"):
    try:
        #Set type of pin numbering
        GPIO.setmode(GPIO.BOARD)
        GPIO.setwarnings(False)
        #Set pin to input for sensor
        GPIO.setup(8, GPIO.IN)

        while GPIO.input(8) == 0:
            nosig = time.time()

        while GPIO.input(8) == 1:
            sig = time.time()

        timeline = sig - nosig

        if measure == "cm":
            distance = timeline / 0.000058
        else:
            print "The wrong measurement has been used - use only cm."
            distance = None

        GPIO.cleanup()
        return distance
    except:
        distance = 100
        GPIO.cleanup()
        return distance

out_sig()
print input_sig("cm")
Thank you in advance.

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

Re: HC-SR04 distance detector code

Thu Feb 15, 2018 12:17 pm

The GPIO (general purpose input output) can be inputs or outputs (or several different modes) but are in only one mode at any one instant. You can certainly set them to be an output, send a ping, then set them to be an input and await an echo. It will work but I expect it will give less reliable results.

Personally I would connect the sensor as it was designed, i.e. use separate GPIO for ping and echo. This will give more reliable results and most sonar ranger software is written for this mode of operation.

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

Re: HC-SR04 distance detector code

Thu Feb 15, 2018 12:40 pm

This is an example program I wrote for use with an HCSR04 ultrasonic sensor, It was written to use 2 gpio pins but I have made some changes so that it will use only pin 8 ( gpio 14 ) accuracy may suffer at shorter ranges due to the need to reconfigure the gpio pin but it should work.

Code: Select all

#!/usr/bin/python

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

#Set type of pin numbering
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

# Define GPIO to use on Pi
GPIO_TRIGGER = 8
GPIO_ECHO    = 8

# Set pins as output 
GPIO.setup(GPIO_TRIGGER,GPIO.OUT)  # Trigger

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

#set # Set pins as input
GPIO.setup(GPIO_ECHO,GPIO.IN)      # Echo

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 traveled 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()
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Roo_027
Posts: 42
Joined: Wed Jan 10, 2018 10:10 am

Re: HC-SR04 distance detector code

Sun Feb 18, 2018 3:13 pm

Hi Thank you both.

PCMan - I tried your code and it doesnt seem to work. I have fiddled around with it and I think I am going to try Joan said about connecting the Trigger and Echo to their own GPIO pins straight on the Pi.

I have one small question regarding this - what resistor do I need to use for the Echo on the breadboard? Apparently when the input is received from the distance detector straight to the Pi it might cause harm to it as it is a higher voltage.

What I am doing is the following, please could you let me know if this is safe and nothing will blow:

From VCC (on SR04 and breadboard) connect to the VIN slot (on the motor controller board)
From GND (on SR04 and breadboard) connect to the GND slot (on the motor controller board)
From Trig on breadboard, connect to GPIO pin 12 directly on the Pi
From Echo on breadboard (with resistor) connect directly to GPIO pin 16 on the Pi

Is this setup fine?

Thank you again.

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

Re: HC-SR04 distance detector code

Sun Feb 18, 2018 3:27 pm

EDITED....................... to show connection direct to gpio.
You must use the 2 resistors shown to prevent damage to your pi.

Image

You should find my code will work if you use this version that was written with those gpio pins as the connections.

Code: Select all

#!/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.BCM)
GPIO.setwarnings(False)

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

# 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()
Last edited by pcmanbob on Sun Feb 18, 2018 3:56 pm, edited 1 time in total.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Roo_027
Posts: 42
Joined: Wed Jan 10, 2018 10:10 am

Re: HC-SR04 distance detector code

Sun Feb 18, 2018 3:31 pm

Thank you again.

I need the board in order to run the DC motors. I thought the whole point of using the motor controller board was to make things more simple but now it looks like its making them more difficult.

The only resistors I have on hand are 330 ohm and a 4.7K ohm resistor. Will the 4.7K be enough to use for the Echo?

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

Re: HC-SR04 distance detector code

Sun Feb 18, 2018 3:38 pm

The only resistors I have on hand are 330 ohm and a 4.7K ohm resistor. Will the 4.7K be enough to use for the Echo?
NO you need to use the 2 resistors as shown.

OK if you want to keep the board in place then you will have to use other gpio that are free.

I will adjust the image and the code and edit my previous post.

give me some time.......


Edit.......
new details for connection and code changes now posted..
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Roo_027
Posts: 42
Joined: Wed Jan 10, 2018 10:10 am

Re: HC-SR04 distance detector code

Mon Feb 19, 2018 10:14 am

Hi pcmanbob

Thank you for adjustments in your diagram. I decided to give the code another go, however, with all pins connected to the motor controller board (GPIO pin 8 for both output and input) and after making a few adjustments to it I have gotten readings! However, they seem a bit off.

Even when I hold an object about 4 or 5cm from the distance detector it gives me a number of 180.67898 etc.

I have attached the code below with all the previous pieces of it I commented out - note that I removed the "while" loops as I think they were the ones giving problems.

Code: Select all

#Testing the sensor

import RPi.GPIO as GPIO
import time

GPIO_Trigger = 8
GPIO_Echo = 8

#Set type of pin numbering
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#Set the pin to output
GPIO.setup(GPIO_Trigger, GPIO.OUT)

print "Ultrasonic Measurement"
#Switch the pin off first
GPIO.output(GPIO_Trigger, False)
time.sleep(0.5)

#Now switch the output pin on to send out a pulse
GPIO.output(GPIO_Trigger, True)
time.sleep(0.00001)
#Switch it off and and record the current time
GPIO.output(GPIO_Trigger, False)
start = time.time()
print "tracking"


#Set pin to input for sensor
GPIO.setup(GPIO_Echo, GPIO.IN)
GPIO.input(GPIO_Echo)
##while GPIO.input(GPIO_Echo) == 0:
##    start = time.time()

#GPIO.input(GPIO_Echo) == 1
stop = time.time()

elapsed = stop - start

#Distance pulse travelled is time multiplied by speed of sound in cm/s
distancet = elapsed * 34300

distance = distancet / 2

print distance


GPIO.cleanup()

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

Re: HC-SR04 distance detector code

Mon Feb 19, 2018 2:50 pm

you need a while loop in there because they are what decides when the echo returns and so gives you the stop time.

may be you are running it to problems because you are using the same pin for trigger and echo via your motor driver board.

try this code

Code: Select all

#Testing the sensor

import RPi.GPIO as GPIO
import time

GPIO_Trigger = 8
GPIO_Echo = 8

#Set type of pin numbering
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#Set the pin to output
GPIO.setup(GPIO_Trigger, GPIO.OUT)

print "Ultrasonic Measurement"
#Switch the pin off first
GPIO.output(GPIO_Trigger, False)
time.sleep(0.5)

#Now switch the output pin on to send out a pulse
GPIO.output(GPIO_Trigger, True)
time.sleep(0.00001)
#Switch it off and and record the current time
GPIO.output(GPIO_Trigger, False)
start = time.time()
print "tracking"


#Set pin to input for sensor
GPIO.setup(GPIO_Echo, GPIO.IN)


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

print "Start ", start
print "stop ", stop  
  
elapsed = stop - start

#Distance pulse travelled is time multiplied by speed of sound in cm/s
distancet = elapsed * 34300

distance = distancet / 2

print distance


GPIO.cleanup()
and see what you get as the results for start and stop might give us a clue as to what's happening.

I think if you just went to the way I suggested with power coming from your motor board and trigger/echo being direct connections to the gpio you would find it would just work.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Roo_027
Posts: 42
Joined: Wed Jan 10, 2018 10:10 am

Re: HC-SR04 distance detector code

Mon Feb 19, 2018 5:53 pm

may be you are running it to problems because you are using the same pin for trigger and echo via your motor driver board.
I fully understand that that may be the cause but then how have other people done it using the motor controller board - and why would they create those pins specifically for the distance detector if they do not even work? There has to be a way to resolve this.

I used your code with ONE amendment otherwise I received an error saying "stop is not defined". So I defined "stop" outside of the while loop as well.

Here are the results from running the code:

Code: Select all

#Testing the sensor

import RPi.GPIO as GPIO
import time

GPIO_Trigger = 8
GPIO_Echo = 8

#Set type of pin numbering
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#Set the pin to output
GPIO.setup(GPIO_Trigger, GPIO.OUT)

print "Ultrasonic Measurement"
#Switch the pin off first
GPIO.output(GPIO_Trigger, False)
time.sleep(0.5)

#Now switch the output pin on to send out a pulse
GPIO.output(GPIO_Trigger, True)
time.sleep(0.00001)
#Switch it off and and record the current time
GPIO.output(GPIO_Trigger, False)
start = time.time()
print "tracking"


#Set pin to input for sensor
GPIO.setup(GPIO_Echo, GPIO.IN)
#GPIO.input(GPIO_Echo)
##while GPIO.input(GPIO_Echo) == 0:
##    start = time.time()

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

stop = time.time()
print "Start", start
print "stop", stop

elapsed = stop - start

#Distance pulse travelled is time multiplied by speed of sound in cm/s
distancet = elapsed * 34300

distance = distancet / 2

print distance


GPIO.cleanup()
Ultrasonic Measurement
tracking
Start 1519062675.73
stop 1519062675.75
369.753205776
>>>

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

Re: HC-SR04 distance detector code

Mon Feb 19, 2018 8:10 pm

So I decided to test this for myself .

proved the 2 gpio method works returns results with mm accuracy.

but I cant get it to work using one gpio pin for both echo and trigger, using standard python code.
so if you want to use your sensor you are going to have to use the 2 gpio method if you want to use the standard python code,
or use the special code that 4tronix has here https://github.com/4tronix/PiRoCon/tree/master/python that is 3 years old probably abandoned and no instructions with it. I cant help with this code because I don't have the motor board you have.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

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

Re: HC-SR04 distance detector code

Mon Feb 19, 2018 8:31 pm

Unless I'm missing some subtlety here, you appear to have commented out the while loop that waits for the echo signal to go high. You need that bit.
Submarine communication systems engineer and amateur robot enthusiast.

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

Re: HC-SR04 distance detector code

Mon Feb 19, 2018 8:40 pm

Moe wrote:
Mon Feb 19, 2018 8:31 pm
Unless I'm missing some subtlety here, you appear to have commented out the while loop that waits for the echo signal to go high. You need that bit.
As I said when I tested it I could not get it to work and I was using my original code for the 2 gpio method just adjust to use just 1 gpio but it never saw the echo I don't know if its because both pins are connected to the voltage divider circuit or the fact the the returning echo puts another pulse on the trigger pin. without access to the add on board Roo_027 has I can figure how they have it connected , they do have special software for the board which I linked to but its 3 years old.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

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

Re: HC-SR04 distance detector code

Mon Feb 19, 2018 10:59 pm

@pcmanbob your original code looks fine, but somewhere down this thread this bit got removed:

Code: Select all

while GPIO.input(GPIO_ECHO)==0:
  start = time.time()
I had one of those 4tronix robots with the single-GPIO control thing a couple of years back, and - though it's probably bad practice - it worked. I think the idea is it switches O/P to I/P during the dead time while the HC-SR04 is transmitting, so it shouldn't affect the result .

Roo_027 reported a 'stop is not defined' error, which means the 'stop' definition was never reached, which means GPIO was low at the start of the while loop. This is because the program isn't waiting for it to go high in the first place.

As far as I can see Roo_027's setup is working - he/she is gettting a valid return, but the code is measuring the elapsed time between the trigger pulse and the end of the echo pulse, rather than the length of the echo pulse itself, thereby giving false results.
Submarine communication systems engineer and amateur robot enthusiast.

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

Re: HC-SR04 distance detector code

Mon Feb 19, 2018 11:18 pm

Moe
So have you tested the first version I posted using gpio8 for both trigger and echo ?

Roo_027 reported it did not work.

I attempted to test this myself with just the echo running through a potential divider circuit to protect the gpio form the 5v output but could not get it to work as it never saw the high on the echo.
As I don't have a 4tronix board I don't know how they have it connected.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Roo_027
Posts: 42
Joined: Wed Jan 10, 2018 10:10 am

Re: HC-SR04 distance detector code

Tue Feb 20, 2018 10:30 am

Hi again
Unless I'm missing some subtlety here, you appear to have commented out the while loop that waits for the echo signal to go high. You need that bit.
I specifically commented out the while loop where the input GPIO is == 0 - I did this as the code would not produce any results if it was left in.

With the code that I used in my previous post, I received readings - so I am able to send and receive a signal but the measurements are way off - even if I hold an object right next to the distance detector I end up getting almost the same result as if it is pointing at a wall that is quite a distance away.

Here is the code again:

Code: Select all

#Testing the sensor

import RPi.GPIO as GPIO
import time

GPIO_Trigger = 8
GPIO_Echo = 8

#Set type of pin numbering
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#Set the pin to output
GPIO.setup(GPIO_Trigger, GPIO.OUT)

print "Ultrasonic Measurement"
#Switch the pin off first
GPIO.output(GPIO_Trigger, False)
time.sleep(0.5)

#Now switch the output pin on to send out a pulse
GPIO.output(GPIO_Trigger, True)
time.sleep(0.00001)
#Switch it off and and record the current time
GPIO.output(GPIO_Trigger, False)
start = time.time()
print "tracking"


#Set pin to input for sensor
GPIO.setup(GPIO_Echo, GPIO.IN)
GPIO.input(GPIO_Echo)
##while GPIO.input(GPIO_Echo) == 0:
##    start = time.time()

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

stop = time.time()
print "Start", start
print "stop", stop

elapsed = stop - start

#Distance pulse travelled is time multiplied by speed of sound in cm/s
distancet = elapsed * 34300

distance = distancet / 2

print distance


GPIO.cleanup()
I want to point out that whether I run it with "GPIO.input(GPIO_Echo)" or I comment that bit out it will still run and give me a result - but the distance shown is different.
Roo_027 reported a 'stop is not defined' error, which means the 'stop' definition was never reached, which means GPIO was low at the start of the while loop. This is because the program isn't waiting for it to go high in the first place.
Notice that right under my while loop I have placed another "stop = time.time()" - otherwise it doesn't seem to pick up the "stop" within the while loop.

I tried uploading pictures of my RC Car but the file sizes are too large but here is a link that shows exactly I have connected the pins to the board and breadboard:

https://4tronix.co.uk/blog/?p=551

Look at Step 6 and Step 7 - it is pretty much how mine looks.

I really do not think the problem is with the wiring though since I am receiving results from the distance detector.

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

Re: HC-SR04 distance detector code

Tue Feb 20, 2018 12:53 pm

That's the exact problem I had using the code when I tried to test it, no echo pulse is detected.

So I have dug in to the github link to the 4tronix code and pulled out just the sensor code.

So this is their code designed to work with the board you have, one thing that is different is that all the code they use , uses board no bcm numbering, this may be the problem, so give it a go and let us know if it works.

Code: Select all

import time
import RPi.GPIO as GPIO

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

# Define GPIO to use on Pi
sonar = 8

GPIO.setup(sonar, GPIO.OUT)
# Send 10us pulse to trigger
GPIO.output(sonar, True)
time.sleep(0.00001)
GPIO.output(sonar, False)
start = time.time()
count=time.time()
GPIO.setup(sonar,GPIO.IN)
while GPIO.input(sonar)==0 and time.time()-count<0.1:
    start = time.time()
count=time.time()
stop=count
while GPIO.input(sonar)==1 and time.time()-count<0.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 * 34000
# That was the distance there and back so halve the value
distance = distance / 2
print distance
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

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

Re: HC-SR04 distance detector code

Tue Feb 20, 2018 8:10 pm

Notice that right under my while loop I have placed another "stop = time.time()" - otherwise it doesn't seem to pick up the "stop" within the while loop.
As I said, this is because the condition for entering the while loop in the first place (GPIO == 1) is not being satisfied so 'stop' never gets defined. Either the pin has not gone high (yet) or it is not working as an input.

But your input is working - you are successfully detecting the pin being high for a finite time, then going low. That means the wiring and your pin definitions must be are correct.
Submarine communication systems engineer and amateur robot enthusiast.

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

Re: HC-SR04 distance detector code

Tue Feb 20, 2018 8:29 pm

Moe wrote:
Tue Feb 20, 2018 8:10 pm
Notice that right under my while loop I have placed another "stop = time.time()" - otherwise it doesn't seem to pick up the "stop" within the while loop.
As I said, this is because the condition for entering the while loop in the first place (GPIO == 1) is not being satisfied so 'stop' never gets defined. Either the pin has not gone high (yet) or it is not working as an input.

But your input is working - you are successfully detecting the pin being high for a finite time, then going low. That means the wiring and your pin definitions must be are correct.
with ref to my previous post now I have looked at the 4tronics code it seems their add on board was designed/setup to use BOARD numbering were as all the code we have tried used BCM numbering, I have posted part of their code for testing by Roo_027 and I am hope it will solve the problem.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

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

Re: HC-SR04 distance detector code

Wed Feb 21, 2018 12:03 am

I have looked at the 4tronics code it seems their add on board was designed/setup to use BOARD numbering
I'm not sure I understand. The 4tronix board cannot possibly know or care how the pins are defined in a Python script running on a Pi. In any case, the code posted above also uses board numbering and appears to be working. It is, however, measuring the wrong thing, and would therefore give the wrong result regardless of any other problems. I would suggest Roo-027 fixes the obvious errors then moves on.
Submarine communication systems engineer and amateur robot enthusiast.

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

Re: HC-SR04 distance detector code

Wed Feb 21, 2018 12:14 am

Moe wrote:
Wed Feb 21, 2018 12:03 am
I have looked at the 4tronics code it seems their add on board was designed/setup to use BOARD numbering
I'm not sure I understand. The 4tronix board cannot possibly know or care how the pins are defined in a Python script running on a Pi. In any case, the code posted above also uses board numbering and appears to be working. It is, however, measuring the wrong thing, and would therefore give the wrong result regardless of any other problems. I would suggest Roo-027 fixes the obvious errors then moves on.
Of course 4tronix can they designed and built the board which Roo-027 is using, there for they will know exactly how to address it .

we are not talking about connecting the HC-SR04 to the gpio , we are talking about connecting it to this add on board on the provided pins.
Attachments
Microcon3_01a-300x265.jpg
Microcon3_01a-300x265.jpg (33.02 KiB) Viewed 1652 times
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

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

Re: HC-SR04 distance detector code

Wed Feb 21, 2018 12:33 am

It's controlld by a 1-bit binary signal from a single GPIO pin. There is no 'addressing ' going on.
Submarine communication systems engineer and amateur robot enthusiast.

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

Re: HC-SR04 distance detector code

Wed Feb 21, 2018 12:45 am

If the 4tronics python code is using BOARD mode and gpio8 to specify which pin controls the trigger & echo

by changing it to BCM mode and using gpio8 we changed the gpio pin that the code was using to try and talk to the 4tronics board.

hope that makes what I was say a bit clearer when I said addressing
Attachments
gpio.jpg
gpio.jpg (202.48 KiB) Viewed 1645 times
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Roo_027
Posts: 42
Joined: Wed Jan 10, 2018 10:10 am

Re: HC-SR04 distance detector code

Mon Feb 26, 2018 3:01 pm

Hi again

Thank you for the replies and help.

pcmanbob - the code you sent me last for the motor controller board specifically, I tried it and although the measurements have improved, they are still wrong.

So I have given up trying to make the distance detector work using the 4tronix board - I will plug it directly into the breadboard and GPIO pins on the Pi and see how that goes.
So this is their code designed to work with the board you have, one thing that is different is that all the code they use , uses board no bcm numbering, this may be the problem, so give it a go and let us know if it works.
I am not sure what you mean here as in my code I always used BOARD numbering - so that cannot be the problem.

Anyway, I will give it a go without the 4tronix board pins and let you guys know how it goes.

Return to “Automation, sensing and robotics”