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?