Quantcast
Viewing all articles
Browse latest Browse all 5299

Beginners • Raspberry Pi 4 to 5 GPIO issue?

Hello,

I run the program below on my raspberry Pi 4 and it works. I run the program on the Pi 5 and it fails. It uses a HC SR-04 ulrasonic Transmitter / Receiver and it fails. I've been told that the GPIO chip is different from the Pi 4. Here is the program It fails with "import RPi.GPIO as GPIO"

import RPi.GPIO as GPIO
import time

#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)

#set GPIO Pins
GPIO_TRIGGER = 18
GPIO_ECHO = 24
#Steve added next for warning/error message
GPIO.setwarnings(False)
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

def distance():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)

# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)

StartTime = time.time()
StopTime = time.time()

# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()

# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()

# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# or multiply with sonic speed (1125.33 ft/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 1125.33) / 2

return distance

if __name__ == '__main__':
try:
while True:
dist = distance()
print ("Measured Distance = %.1f ft" % dist)
time.sleep(1)

# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()

Any help would be appreciated.

Statistics: Posted by sismith1 — Fri Aug 23, 2024 8:15 pm



Viewing all articles
Browse latest Browse all 5299

Trending Articles