0

this is the simple code to measure distance using ultrasoinic sensor attached to reapberry-pi and by writing python script--

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 16
ECHO = 20
print "Distance Measurement In Progress"
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
while True:
GPIO.output(TRIG, False)
print "Waiting For Sensor To Settle"
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
 pulse_start = time.time()
while GPIO.input(ECHO)==1:
 pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
print "Distance:",distance,"cm"

use case is- I have to run this script only when all the pins of ultrasonic sensor is properly connected to the circuit and the raspberry-pi. so my question is, can we identify the pin connection before running the script? or can we identify whether our hardware is working properly or not?

asked Dec 13, 2016 at 6:41

2 Answers 2

1

Send a trigger. If you do not get a response within a timeout period assume the device isn't present.

answered Dec 13, 2016 at 9:42
2
  • can u plz tell me how to send external trigger in raspberry-pi using python. Commented Dec 13, 2016 at 11:14
  • 1
    @andy I would send the trigger in the same way as the code does at the moment. Commented Dec 13, 2016 at 11:49
0

To the best of my knowledge, unless you use an EEPROM chip (like Pi HATs HAT specification) then you will be able to know what GPIO pins need to be setup, otherwise it's anyone's guess; unless you want to flat out ask the user in the code what pins they have configured (or get that information from command line arguments) i.e. add the following to your code:

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
TRIG = int(raw_input('What pin are you using for the trigger? '))
ECHO = int(raw_input('What pin are you using for the echo? '))
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
answered Dec 13, 2016 at 11:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.