2
我想在我的RPi 2模型B上用python運行HC-SR04模塊,雖然我沒有成功。佈線看起來很好,編碼也是如此,但是脈衝不會被髮出或接收,任何人都可以幫忙嗎?HC-SR04不工作RPi2與python
import RPi.GPIO as GPIO
import time
#GPIO Operandi (BOARD/BCM)
GPIO.setmode(GPIO.BCM)
# Set the GPIO pins
TRIG = 35
ECHO = 37
# Define the direction in which the pins are working
# If they send a signal OUT or recieve a signal IN
# ultrasonic sensor pins for TRIG and ECHO
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
#Allowing sensor to settle
GPIO.output(TRIG, False)
time.sleep(2)
# TRIG HIGH
GPIO.output(TRIG, True)
# TRIG LOW after 0.01ms
time.sleep(0.00001)
GPIO.output(TRIG, False)
# Define start time
while GPIO.input(ECHO) == 0:
StartTime = time.time()
# Define end time
while GPIO.input(ECHO) == 1:
StopTime = time.time()
# Calculate the difference between the StartTime and the StopTime
TimeElapsed = StopTime - StartTime
# Multiply by speed of sound
# Divide by 2 for one direction
distance = (TimeElapsed * 34300)/2
print(distance)`